Arduino Education Lab Pro
Learn electronics and programming with interactive Arduino projects. Click on a category below to get started!
Blinking an LED
Explanation
Components Needed
- Arduino UNO
- LED (any color)
- 220Ω Resistor
- Breadboard and Jumper Wires
Wiring Guide
Connect the Arduino's 5V pin to the positive power rail of the breadboard, and GND to the negative rail. Place the LED on the breadboard. Connect the long leg (anode) of the LED to one end of the 220Ω resistor. Connect the other end of the resistor to Arduino digital pin ~9. Connect the short leg (cathode) of the LED to the GND rail.
How It Works
This is the "Hello, World!" of physical computing. The Arduino acts as a simple switch. The `pinMode(ledPin, OUTPUT)` command configures pin 9 to send out voltage. `digitalWrite(ledPin, HIGH)` sends 5V, which flows through the resistor, lights up the LED, and returns to GND. `digitalWrite(ledPin, LOW)` sends 0V, turning the LED off. The `delay()` function pauses the program, creating the blinking effect.
Code Breakdown
pinMode(ledPin, OUTPUT); - Sets pin 9 as an output.
digitalWrite(ledPin, HIGH); - Turns the LED on.
delay(1000); - Pauses for 1000 milliseconds (1 second).
digitalWrite(ledPin, LOW); - Turns the LED off.
Arduino Code
const int ledPin = 9; // Use a PWM pin for flexibility
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
}
Reading a Push Button
Explanation
Components Needed
- Arduino UNO
- Push Button
- 10kΩ Resistor (for pull-down)
- Breadboard and Jumper Wires
Wiring Guide
Connect one leg of the push button to Arduino 5V. Connect the diagonally opposite leg to both Arduino digital pin 2 and one end of the 10kΩ resistor. Connect the other end of the resistor to Arduino GND. This is a "pull-down" configuration, ensuring the pin reads LOW when the button is not pressed.
How It Works
A digital input pin reads either HIGH (5V) or LOW (0V). However, when not connected, it "floats" and gives random readings. The pull-down resistor solves this. When the button is not pressed, the pin is connected to GND through the resistor, so it reads LOW. When you press the button, it creates a direct path to 5V, which overrides the pull-down resistor, and the pin reads HIGH. The code checks this state to determine if the button is pressed.
Code Breakdown
pinMode(buttonPin, INPUT); - Sets pin 2 as an input.
buttonState = digitalRead(buttonPin); - Reads the voltage on pin 2 (HIGH or LOW).
if (buttonState == HIGH) - Checks if the button is pressed and executes the code inside the `if` block.
Arduino Code
const int buttonPin = 2;
const int ledPin = 13; // Use built-in LED for feedback
int buttonState = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}
LDR (Light Sensor)
Explanation
Components Needed
- Arduino UNO
- LDR (Light Dependent Resistor)
- 10kΩ Resistor
- Breadboard and Jumper Wires
Wiring Guide
Create a voltage divider. Connect 5V to one leg of the LDR. Connect the other leg of the LDR to both Arduino analog pin A0 and one end of the 10kΩ resistor. Connect the other end of the 10kΩ resistor to GND.
How It Works
An LDR's resistance changes with light. In bright light, its resistance is low (e.g., 1kΩ). In darkness, it's high (e.g., 1MΩ). In a voltage divider, the output voltage at the middle point depends on the ratio of the two resistors. The Arduino's `analogRead()` measures this voltage. As light changes, the LDR's resistance changes, which changes the voltage at A0, giving you a reading from 0 (dark) to 1023 (bright).
Code Breakdown
ldrValue = analogRead(ldrPin); - Reads the voltage at A0 and converts it to a number (0-1023).
Serial.print(ldrValue); - Sends the value to your computer for viewing in the Serial Monitor.
Arduino Code
const int ldrPin = A0;
int ldrValue = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
ldrValue = analogRead(ldrPin);
Serial.print("Light Level: ");
Serial.println(ldrValue);
delay(200);
}
DHT11 (Temp & Humidity)
Explanation
Components Needed
- Arduino UNO
- DHT11 Sensor
- Breadboard and Jumper Wires
Wiring Guide
Connect the DHT11's VCC or (+) pin to Arduino 5V. Connect GND or (-) to Arduino GND. Connect the DATA or OUT pin to Arduino digital pin 2. (Note: Some DHT11 modules require a 10kΩ pull-up resistor between the DATA and VCC pins, but most breakout boards have this built-in.)
How It Works
The DHT11 is a digital sensor that measures temperature and humidity. It uses a proprietary one-wire protocol to send data. The Arduino sends a start signal, and the sensor responds with a stream of 40 bits of data representing the humidity and temperature. Manually coding this timing-critical protocol is difficult, so we use the "DHT sensor library" by Adafruit, which handles all the complex communication for us.
Code Breakdown
#include "DHT.h" - Includes the necessary library.
DHT dht(DHTPIN, DHTTYPE); - Creates a DHT object.
dht.readHumidity() - Reads humidity from the sensor.
dht.readTemperature() - Reads temperature in Celsius.
Arduino Code
#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
delay(2000);
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" % | Temperature: ");
Serial.print(t);
Serial.println(" °C");
}
Ultrasonic Sensor (HC-SR04)
Explanation
Components Needed
- Arduino UNO
- HC-SR04 Ultrasonic Sensor
- Breadboard and Jumper Wires
Wiring Guide
Connect HC-SR04 VCC to Arduino 5V and GND to GND. Connect the Trig (Trigger) pin to Arduino digital pin 9. Connect the Echo pin to Arduino digital pin 10.
How It Works
The sensor works like a bat. It sends out an 8-cycle ultrasonic sound burst from the Trig pin. The Echo pin then goes HIGH and waits for the sound to bounce off an object and return. When the echo is received, the Echo pin goes LOW. By measuring the duration that the Echo pin was HIGH, we can calculate the distance. The formula is: Distance (cm) = (Duration (µs) / 2) / 29.1. We divide by 2 because the sound travels there and back.
Code Breakdown
digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); - Sends a 10µs pulse to start the measurement.
duration = pulseIn(echoPin, HIGH); - Measures the length of the pulse on the Echo pin in microseconds.
distance = duration * 0.034 / 2; - A simplified formula to convert duration to centimeters.
Arduino Code
const int trigPin = 9;
const int echoPin = 10;
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
}
PIR Motion Sensor
Explanation
Components Needed
- Arduino UNO
- PIR Motion Sensor (e.g., HC-SR501)
- Breadboard and Jumper Wires
Wiring Guide
Connect the PIR sensor's VCC pin to Arduino 5V and GND to GND. Connect the OUT pin to Arduino digital pin 2. Most PIR sensors have two potentiometers to adjust sensitivity and trigger time.
How It Works
A PIR (Passive Infrared) sensor detects changes in infrared radiation. All objects with a temperature above absolute zero emit heat energy in the form of infrared radiation. The sensor has two pyroelectric sensors that detect small changes in IR levels. When a warm body (like a person) moves in front of the sensor, it creates a positive differential change between the two sensors, causing the output pin to go HIGH for a set period of time.
Code Breakdown
pirState = digitalRead(pirPin); - Reads the output of the PIR sensor (HIGH if motion is detected, LOW otherwise).
if (pirState == HIGH) - This block runs whenever motion is detected, allowing you to trigger an alarm, light, or other action.
Arduino Code
int pirPin = 2;
int pirState = LOW;
void setup() {
pinMode(pirPin, INPUT);
Serial.begin(9600);
}
void loop() {
if (digitalRead(pirPin) == HIGH) {
if (pirState == LOW) {
Serial.println("Motion detected!");
pirState = HIGH;
}
} else {
if (pirState == HIGH) {
Serial.println("Motion ended!");
pirState = LOW;
}
}
}
IR Receiver & Remote
Explanation
Components Needed
- Arduino UNO
- VS1838B IR Receiver Diode
- Any standard IR Remote Control
- Breadboard and Jumper Wires
Wiring Guide
The IR receiver has 3 pins. Looking at the bulb, the left pin is OUT (Data), the middle is GND, and the right is VCC. Connect VCC to 5V, GND to GND, and OUT to Arduino digital pin 11.
How It Works
An IR remote sends signals using modulated infrared light. It flashes an IR LED on and off at a high frequency (e.g., 38kHz) in a specific pattern for each button. The IR receiver detects this modulated signal, demodulates it, and outputs a clean digital signal. We use the "IRremote" library to decode this signal and get a unique hexadecimal code for each button pressed.
Code Breakdown
#include - Includes the IRremote library.
IRrecv irrecv(RECV_PIN); - Creates an IR receiver object.
irrecv.decode(&results)) - Checks if a signal has been successfully decoded.
results.value - Contains the hexadecimal code of the pressed button.
Arduino Code
#include <IRremote.h>
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup() {
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
}
void loop() {
if (irrecv.decode(&results)) {
Serial.println(results.value, HEX);
irrecv.resume(); // Receive the next value
}
delay(100);
}
Joystick Module
Explanation
Components Needed
- Arduino UNO
- Joystick Module (similar to KY-023)
- Breadboard and Jumper Wires
Wiring Guide
Connect the module's VCC to 5V and GND to GND. The X-axis (VRx) pin connects to Arduino A0, the Y-axis (VRy) pin to A1, and the SW (switch) pin to digital pin 2.
How It Works
A joystick module is essentially two 10kΩ potentiometers and a push button. Moving the joystick left/right adjusts the resistance of the X-axis potentiometer, which the Arduino reads on pin A0. Moving it up/down adjusts the Y-axis potentiometer, read on A1. Pressing the joystick down closes the internal switch, connecting the SW pin to GND. The `analogRead()` function gives a value from 0 to 1023 for the axes, and `digitalRead()` gives LOW when pressed and HIGH when not (due to an internal pull-up resistor).
Code Breakdown
xValue = analogRead(VRx); - Reads the position of the X-axis (0-1023).
yValue = analogRead(VRy); - Reads the position of the Y-axis (0-1023).
swState = digitalRead(SW); - Reads the state of the button (LOW when pressed).
Arduino Code
int VRx = A0;
int VRy = A1;
int SW = 2;
int xValue = 0;
int yValue = 0;
int swState = 0;
void setup() {
pinMode(SW, INPUT_PULLUP); // Enable internal pull-up resistor
Serial.begin(9600);
}
void loop() {
xValue = analogRead(VRx);
yValue = analogRead(VRy);
swState = digitalRead(SW);
Serial.print("X: ");
Serial.print(xValue);
Serial.print(" | Y: ");
Serial.print(yValue);
Serial.print(" | Switch: ");
Serial.println(swState);
delay(100);
}
Automatic Night Light
Explanation
Components Needed
- Arduino UNO, LDR, 10kΩ Resistor, LED, 220Ω Resistor
- Breadboard and Jumper Wires
Wiring Guide
Combine the LDR circuit and the LED circuit. The LDR voltage divider connects to A0. The LED, with its 220Ω resistor, connects to digital pin 9. Both circuits share the Arduino's 5V and GND.
How It Works
This project is a simple automated system. The Arduino continuously reads the light level from the LDR. It uses an `if-else` statement to compare this reading against a pre-set `threshold`. If the reading is below the threshold (meaning it's dark), the `if` condition is true, and the Arduino turns the LED on. If the reading is above the threshold (it's bright), the `else` condition runs, and the LED is turned off. You can adjust the `threshold` variable to set the darkness level at which the light turns on.
Code Breakdown
int threshold = 400; - The light level that triggers the LED.
if (ldrValue < threshold) - Checks if the room is dark.
digitalWrite(ledPin, HIGH); - Turns the LED on if it's dark.
else { digitalWrite(ledPin, LOW); } - Turns the LED off if it's bright.
Arduino Code
const int ldrPin = A0;
const int ledPin = 9;
int ldrValue = 0;
int threshold = 400;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
ldrValue = analogRead(ldrPin);
if (ldrValue < threshold) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
delay(200);
}
LCD Weather Station
Explanation
Components Needed
- Arduino UNO, DHT11 Sensor, 16x2 LCD Screen (with I2C module)
- Breadboard and Jumper Wires
Wiring Guide
Connect the DHT11 as before (VCC, GND, data to pin 2). For the I2C LCD, connect VCC and GND. Connect the SDA (Serial Data) pin to Arduino A4 and the SCL (Serial Clock) pin to Arduino A5. These are the default I2C pins on an Arduino UNO.
How It Works
This project combines input from a sensor and output to a display. The Arduino first reads the temperature and humidity from the DHT11. Then, it uses the I2C protocol to send this data to the LCD. I2C is a two-wire communication bus that allows multiple devices to connect using just SDA (data) and SCL (clock) lines. The "LiquidCrystal_I2C" library simplifies sending commands and text to the screen. The Arduino reads the data, formats it into strings, and then tells the LCD what to display on each line.
Code Breakdown
#include - Includes the I2C LCD library.
LiquidCrystal_I2C lcd(0x27, 16, 2); - Sets the LCD I2C address and dimensions.
lcd.setCursor(0, 0); - Moves the cursor to the first column of the first row.
lcd.print("Temp: "); lcd.print(t); - Prints the text and the temperature variable to the screen.
Arduino Code
#include <LiquidCrystal_I2C.h>
#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address to 0x27
void setup() {
lcd.init();
lcd.backlight();
dht.begin();
}
void loop() {
delay(2000);
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) { lcd.print("Sensor Error!"); return; }
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(t);
lcd.print(" C");
lcd.setCursor(0, 1);
lcd.print("Humid: ");
lcd.print(h);
lcd.print(" %");
}
Controlling a Servo Motor
Explanation
Components Needed
- Arduino UNO
- Servo Motor (e.g., SG90)
- Breadboard and Jumper Wires
Wiring Guide
Servos have three wires. The darkest (usually brown or black) is GND. The red is VCC (connect to 5V). The orange or yellow is the Signal wire; connect it to Arduino digital pin ~9.
How It Works
A servo motor is a rotary actuator that allows for precise control of angular position. It is controlled by Pulse Width Modulation (PWM). The Arduino sends a pulse of electricity every 20 milliseconds. The width of this pulse determines the angle of the servo. A 1.0ms pulse moves it to 0°, a 1.5ms pulse to 90°, and a 2.0ms pulse to 180°. The built-in `Servo.h` library handles all this complex timing for us, allowing us to simply command the motor to move to a specific angle with `servo.write()`.
Code Breakdown
#include - Includes the Servo library.
myServo.attach(9); - Tells the library that the servo is connected to pin 9.
myServo.write(90); - Commands the servo to move to the 90-degree position.
for (pos = 0; pos <= 180; pos += 1) - A loop to sweep the servo from 0 to 180 degrees.
Arduino Code
#include <Servo.h>
Servo myServo;
int servoPin = 9;
void setup() {
myServo.attach(servoPin);
}
void loop() {
myServo.write(0);
delay(1000);
myServo.write(90);
delay(1000);
myServo.write(180);
delay(1000);
}
Relay Module (Control High Power)
Explanation
Components Needed
- Arduino UNO
- 5V Relay Module
- LED and 220Ω Resistor (to simulate a high-power device)
- External 5V Power Supply (for demonstration)
- Breadboard and Jumper Wires
Wiring Guide
Connect the relay module's VCC and GND to Arduino's 5V and GND. Connect the IN (Signal) pin to Arduino digital pin 7. For the load side, connect the external 5V power supply's positive terminal to the relay's COM (Common) terminal. Connect the NO (Normally Open) terminal to the resistor, then to the LED's anode. Connect the LED's cathode to the external power supply's negative terminal. **Important:** The Arduino and the external power supply must share a common GND connection.
How It Works
A relay is an electrically-operated switch. It uses a small current from the Arduino to control a much larger current for another device. Inside, the Arduino's signal energizes an electromagnet, which physically pulls a switch closed. The module has three main terminals: COM (Common), NO (Normally Open), and NC (Normally Closed). In the NO state, the circuit is open until the relay is activated. This allows the Arduino to safely control high-voltage or high-current devices like lamps, fans, or motors, providing electrical isolation between the low-voltage Arduino and the high-voltage load.
Code Breakdown
pinMode(relayPin, OUTPUT); - Sets the relay pin as an output.
digitalWrite(relayPin, LOW); - De-energizes the relay, turning the switch OFF. (Note: Some relay modules are active LOW, meaning LOW turns them ON).
digitalWrite(relayPin, HIGH); - Energizes the relay, turning the switch ON.
Arduino Code
int relayPin = 7;
void setup() {
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW); // Assume LOW is OFF
Serial.begin(9600);
Serial.println("Relay Control Initialized");
}
void loop() {
Serial.println("Turning Relay ON");
digitalWrite(relayPin, HIGH); // Turn Relay ON
delay(2000);
Serial.println("Turning Relay OFF");
digitalWrite(relayPin, LOW); // Turn Relay OFF
delay(2000);
}
RGB LED Mood Lamp
Explanation
Components Needed
- Arduino UNO
- RGB LED (Common Cathode type)
- 3x 220Ω Resistors
- Breadboard and Jumper Wires
Wiring Guide
An RGB LED has four legs: one for each color (Red, Green, Blue) and one common cathode (longest leg). Connect the common cathode to Arduino GND. Connect the Red leg through a 220Ω resistor to pin ~9, the Green leg through a resistor to pin ~10, and the Blue leg through a resistor to pin ~11.
How It Works
An RGB LED contains three separate LEDs (Red, Green, Blue) in one package. By varying the brightness of each of these three primary colors, you can create any other color. We control the brightness of each LED using PWM (Pulse Width Modulation) with the `analogWrite()` function. `analogWrite()` takes a value from 0 (off) to 255 (full brightness). By mixing different values for R, G, and B (e.g., `analogWrite(redPin, 255); analogWrite(greenPin, 0); analogWrite(bluePin, 0);`), you get pure red. The code smoothly fades between colors by changing these values over time.
Code Breakdown
analogWrite(redPin, r); - Sets the brightness of the red component (0-255).
for (int i = 0; i < 256; i++) - A loop to gradually increase brightness.
setColor(i, 0, 0); - Calls a custom function to set the color, fading the red LED on.
Arduino Code
int redPin = 9;
int greenPin = 10;
int bluePin = 11;
void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop() {
setColor(255, 0, 0); // Red
delay(1000);
setColor(0, 255, 0); // Green
delay(1000);
setColor(0, 0, 255); // Blue
delay(1000);
setColor(255, 255, 0); // Yellow
delay(1000);
setColor(80, 0, 80); // Purple
delay(1000);
setColor(0, 255, 255); // Aqua
delay(1000);
}
void setColor(int red, int green, int blue) {
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}