Control An LED Using An Arduino So That It Turns ON For 1 Second
Control An LED Using An Arduino So That It Turns ON For 1 Second
repeating.
Components Used:
Component Purpose
Arduino Board Acts like the "brain" that sends control signals
220Ω Resistor Limits the current to protect the LED from burning out
USB Cable Connects Arduino to your computer for uploading the code
Circuit Explanation:
Anode (long leg) → Connects to Arduino digital pin (pin 13 in this case)
The resistor is necessary to prevent too much current from flowing through the LED and burning it
out.
void setup() {
pinMode(13, OUTPUT); // Tells Arduino that pin 13 will send signals (not receive)
void loop() {
Code Breakdown:
3. Select your board (e.g., Arduino Uno) and the correct COM port in Tools > Port.
Objective:
Turn an LED on and off using a GPIO pin on the Raspberry Pi and a Python script.
Component Purpose
Circuit:
You can Google “Raspberry Pi GPIO pinout” to get a visual layout for your Pi model.
Python Code to Control LED:
import time
# Set up GPIO
# Blink loop
try:
while True:
except KeyboardInterrupt:
print("Exiting program")
finally:
How to Run:
2. Open a terminal.
4. Run it with:
5. python3 led_blink.py
Important Notes:
Control an AC device (like a light bulb or fan) by turning it ON/OFF with a relay, using a relay module
and either Arduino or Raspberry Pi.
Component Use
Relay Module (5V for Arduino / 3.3V or 5V for Pi) For switching AC load
A relay is an electrically operated switch. When the microcontroller sends a signal, it "clicks" and
switches the AC circuit on or off.
Relay terminals:
NO (Normally Open) → Connect here for devices that stay OFF until triggered
AC Side:
int relayPin = 7;
void setup() {
pinMode(relayPin, OUTPUT);
void loop() {
VCC → 5V
GND → GND
Python Code:
import time
relay_pin = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(relay_pin, GPIO.OUT)
try:
while True:
GPIO.output(relay_pin, GPIO.HIGH) # Turn AC ON
time.sleep(5)
time.sleep(5)
except KeyboardInterrupt:
print("Exiting...")
finally:
GPIO.cleanup()
A servo motor is a type of motor that can rotate to a specific angle between 0° and 180° (sometimes
more, depending on the model). It has 3 wires:
Components Needed:
Component Purpose
Optional: External Power Source For more torque (esp. multiple servos)
VCC (Red) 5V 5V
void setup() {
void loop() {
myServo.write(0); // Move to 0°
delay(1000);
delay(1000);
delay(1000);
while True:
servo.min() # Rotate to 0°
sleep(1)
sleep(1)
sleep(1)
Perfect — controlling a DC motor is a classic electronics project, and it’s a great way to learn about
speed control, direction control, and even robotics!
Let’s break it down for Arduino and Raspberry Pi, with wiring, code, and tips. 🧠⚙️
What is a DC Motor?
A DC motor runs when voltage is applied across it — simple as that. But to control speed and
direction, you need more than just a wire — like a motor driver.
Component Use
You can’t power a DC motor directly from Arduino or Pi — it draws too much current. You need a
motor driver to:
Change direction
Wiring:
Arduino Code:
int motorIn1 = 8;
int motorIn2 = 9;
void setup() {
pinMode(motorIn1, OUTPUT);
pinMode(motorIn2, OUTPUT);
pinMode(enablePin, OUTPUT);
void loop() {
// Move Forward
digitalWrite(motorIn1, HIGH);
digitalWrite(motorIn2, LOW);
delay(3000);
// Stop
digitalWrite(motorIn1, LOW);
digitalWrite(motorIn2, LOW);
delay(1000);
// Move Reverse
digitalWrite(motorIn1, LOW);
digitalWrite(motorIn2, HIGH);
delay(3000);
// Stop
digitalWrite(motorIn1, LOW);
digitalWrite(motorIn2, LOW);
delay(1000);
Python Code:
import time
motorIn1 = 17
motorIn2 = 27
enablePin = 22
GPIO.setmode(GPIO.BCM)
GPIO.setup(motorIn1, GPIO.OUT)
GPIO.setup(motorIn2, GPIO.OUT)
GPIO.setup(enablePin, GPIO.OUT)
pwm.start(0)
try:
# Move Forward
GPIO.output(motorIn1, GPIO.HIGH)
GPIO.output(motorIn2, GPIO.LOW)
time.sleep(3)
# Stop
GPIO.output(motorIn1, GPIO.LOW)
GPIO.output(motorIn2, GPIO.LOW)
pwm.ChangeDutyCycle(0)
time.sleep(1)
# Reverse
GPIO.output(motorIn1, GPIO.LOW)
GPIO.output(motorIn2, GPIO.HIGH)
pwm.ChangeDutyCycle(50)
time.sleep(3)
finally:
GPIO.cleanup()
What is a Stepper Motor?
A stepper motor moves in precise steps — typically 1.8° per step (200 steps for 1 full revolution).
Unlike DC motors, it doesn’t spin freely — it steps into position, which makes it ideal for precision
tasks.
Unipolar (5 or 6 wires)
Component Description
Motor Driver (ULN2003 for 28BYJ-48 or A4988/DRV8825 for NEMA17) Controls the motor
Wiring:
Arduino Code:
#include <Stepper.h>
const int stepsPerRevolution = 2048; // For 28BYJ-48 in half-step mode
void setup() {
void loop() {
delay(1000);
delay(1000);
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, 0)
# Half-step sequence
half_step_seq = [
[1,0,0,0],
[1,1,0,0],
[0,1,0,0],
[0,1,1,0],
[0,0,1,0],
[0,0,1,1],
[0,0,0,1],
[1,0,0,1]
]
try:
while True:
GPIO.output(control_pins[pin], step[pin])
time.sleep(0.001)
except KeyboardInterrupt:
GPIO.cleanup()