0% found this document useful (0 votes)
7 views13 pages

Control An LED Using An Arduino So That It Turns ON For 1 Second

The document provides detailed instructions for controlling various electronic components using Arduino and Raspberry Pi, including LEDs, AC devices via relays, servo motors, DC motors, and stepper motors. It includes wiring diagrams, code examples in both Arduino and Python, and essential components needed for each project. Each section emphasizes the importance of using resistors and proper connections to ensure safe and effective operation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views13 pages

Control An LED Using An Arduino So That It Turns ON For 1 Second

The document provides detailed instructions for controlling various electronic components using Arduino and Raspberry Pi, including LEDs, AC devices via relays, servo motors, DC motors, and stepper motors. It includes wiring diagrams, code examples in both Arduino and Python, and essential components needed for each project. Each section emphasizes the importance of using resistors and proper connections to ensure safe and effective operation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Control an LED using an Arduino so that it turns ON for 1 second, then OFF for 1 second — and keeps

repeating.

Components Used:

Component Purpose

Arduino Board Acts like the "brain" that sends control signals

LED Light-emitting diode (the component we're controlling)

220Ω Resistor Limits the current to protect the LED from burning out

Breadboard Used to easily connect components without soldering

Jumper Wires Used to make the electrical connections between components

USB Cable Connects Arduino to your computer for uploading the code

Circuit Explanation:

LED has two legs:

 Anode (long leg) → Connects to Arduino digital pin (pin 13 in this case)

 Cathode (short leg) → Connects to GND through a resistor

The resistor is necessary to prevent too much current from flowing through the LED and burning it
out.

Arduino Code Explained:

void setup() {

pinMode(13, OUTPUT); // Tells Arduino that pin 13 will send signals (not receive)

void loop() {

digitalWrite(13, HIGH); // Sends 5V to pin 13 — LED turns ON

delay(1000); // Waits for 1000 milliseconds = 1 second

digitalWrite(13, LOW); // Sends 0V to pin 13 — LED turns OFF

delay(1000); // Waits again for 1 second

Code Breakdown:

 void setup() → Runs once when the Arduino is powered on or reset

o pinMode(13, OUTPUT); → Configures digital pin 13 to act as an output

 void loop() → Runs forever after setup

o digitalWrite(13, HIGH); → Turns the LED ON


o delay(1000); → Waits for 1 second

o digitalWrite(13, LOW); → Turns the LED OFF

o delay(1000); → Waits for 1 second again

The loop keeps repeating, making the LED blink.

Uploading the Code:

1. Connect Arduino to your PC with the USB cable.

2. Open Arduino IDE.

3. Select your board (e.g., Arduino Uno) and the correct COM port in Tools > Port.

4. Paste the code.

5. Click the Upload button (right-arrow icon).

6. LED will start blinking!

Objective:

Turn an LED on and off using a GPIO pin on the Raspberry Pi and a Python script.

What You Need:

Component Purpose

Raspberry Pi (any model with GPIO) Main controller

LED Output device you’ll control

330Ω or 220Ω Resistor Limits current to protect the LED

Breadboard Easy wiring

Jumper Wires For connections

Optional: GPIO Extension Board (T-Cobbler) For organized wiring

Wiring the LED:

Raspberry Pi GPIO Pinout Reference:

 We'll use GPIO 17 (physical pin 11) for the signal

 GND (physical pin 6) for ground

Circuit:

GPIO17 (pin 11) → LED anode (long leg)

LED cathode (short leg) → Resistor → GND (pin 6)

You can Google “Raspberry Pi GPIO pinout” to get a visual layout for your Pi model.
Python Code to Control LED:

import RPi.GPIO as GPIO

import time

# Set up GPIO

GPIO.setmode(GPIO.BCM) # Use Broadcom pin numbering

GPIO.setup(17, GPIO.OUT) # Set GPIO 17 as output

# Blink loop

try:

while True:

GPIO.output(17, GPIO.HIGH) # Turn LED on

time.sleep(1) # Wait 1 second

GPIO.output(17, GPIO.LOW) # Turn LED off

time.sleep(1) # Wait 1 second

except KeyboardInterrupt:

print("Exiting program")

finally:

GPIO.cleanup() # Reset GPIO settings

How to Run:

1. Boot up your Raspberry Pi.

2. Open a terminal.

3. Save your script (e.g., led_blink.py)

4. Run it with:

5. python3 led_blink.py

Important Notes:

 Always use a resistor with an LED.

 GPIO.setmode(GPIO.BCM) refers to BCM numbering, not physical pin numbers.

 GPIO.cleanup() resets the GPIO pins so they're safe to use again.


Objective:

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.

What You Need:

Component Use

Microcontroller (Arduino or Raspberry Pi) Main controller

Relay Module (5V for Arduino / 3.3V or 5V for Pi) For switching AC load

AC Appliance (bulb, fan, etc.) Load you’re controlling

Jumper Wires Wiring

Power source (and USB cable) For the controller

Relay Module Basics:

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

 COM (Common) → Common terminal

 NC (Normally Closed) → Only used if you want it ON by default

Example: Arduino + Relay to Control AC with Delay

Wiring (low-voltage side):

 Relay IN pin → Arduino digital pin (e.g., pin 7)

 Relay VCC → Arduino 5V

 Relay GND → Arduino GND

AC Side:

 Live wire → COM

 NO → Live wire to bulb/fan

 Neutral wire → directly to bulb/fan


Arduino Code (with relay):

int relayPin = 7;

void setup() {

pinMode(relayPin, OUTPUT);

digitalWrite(relayPin, LOW); // Start with relay off

void loop() {

digitalWrite(relayPin, HIGH); // Turn ON relay (AC ON)

delay(5000); // Wait 5 seconds

digitalWrite(relayPin, LOW); // Turn OFF relay (AC OFF)

delay(5000); // Wait 5 seconds

Same Thing with Raspberry Pi (Python + Relay):


Wiring:

 Relay IN → GPIO17 (pin 11)

 VCC → 5V

 GND → GND

Python Code:

import RPi.GPIO as GPIO

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)

GPIO.output(relay_pin, GPIO.LOW) # Turn AC OFF

time.sleep(5)

except KeyboardInterrupt:

print("Exiting...")

finally:

GPIO.cleanup()

What is a Servo Motor?

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:

 Signal (Orange/Yellow) – connects to a control pin (PWM signal)

 VCC (Red) – connects to 5V

 GND (Brown/Black) – connects to Ground

Components Needed:

Component Purpose

Microcontroller (Arduino or Raspberry Pi) Sends control signal

Servo Motor (e.g., SG90) The motor you're controlling

Jumper Wires For connections

Optional: External Power Source For more torque (esp. multiple servos)

Servo Motor Wiring:

Servo Wire Connects To (Arduino) Connects To (Pi)

VCC (Red) 5V 5V

GND GND GND

Signal Digital Pin (e.g., 9) GPIO pin (e.g., 17)

Arduino Example Code (Rotate Servo)


#include <Servo.h>

Servo myServo; // Create a servo object

void setup() {

myServo.attach(9); // Attach to pin 9

void loop() {

myServo.write(0); // Move to 0°

delay(1000);

myServo.write(90); // Move to 90°

delay(1000);

myServo.write(180); // Move to 180°

delay(1000);

Raspberry Pi Python Code (with gpiozero)

from gpiozero import Servo

from time import sleep

servo = Servo(17) # GPIO pin 17

while True:

servo.min() # Rotate to 0°

sleep(1)

servo.mid() # Rotate to 90°

sleep(1)

servo.max() # Rotate to 180°

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.

What You Need:

Component Use

DC Motor Your load (wheel, fan, etc.)

Motor Driver (like L298N or L293D) Controls power to the motor

Arduino / Raspberry Pi Sends control signals

Power Supply (Battery or Adapter) To power the motor

Jumper Wires Connections

Breadboard (optional) For easy wiring

Motor Driver (L298N or L293D)

You can’t power a DC motor directly from Arduino or Pi — it draws too much current. You need a
motor driver to:

 Protect the controller

 Control speed (via PWM)

 Change direction

DC Motor Control with Arduino + L298N

Wiring:

L298N Pin Connects To

IN1, IN2 Arduino Digital Pins (e.g., 8, 9)

ENA (Enable A) Arduino PWM Pin (e.g., 10)

GND Arduino GND

12V / 5V External Motor Power

OUT1, OUT2 DC Motor

Arduino Code:

int motorIn1 = 8;
int motorIn2 = 9;

int enablePin = 10;

void setup() {

pinMode(motorIn1, OUTPUT);

pinMode(motorIn2, OUTPUT);

pinMode(enablePin, OUTPUT);

void loop() {

// Move Forward

digitalWrite(motorIn1, HIGH);

digitalWrite(motorIn2, LOW);

analogWrite(enablePin, 200); // PWM value (0 to 255) — controls speed

delay(3000);

// Stop

digitalWrite(motorIn1, LOW);

digitalWrite(motorIn2, LOW);

delay(1000);

// Move Reverse

digitalWrite(motorIn1, LOW);

digitalWrite(motorIn2, HIGH);

analogWrite(enablePin, 150); // Slower reverse speed

delay(3000);

// Stop

digitalWrite(motorIn1, LOW);

digitalWrite(motorIn2, LOW);

delay(1000);

Raspberry Pi + L298N (Python)

Wiring (same as Arduino):


Use GPIO pins for IN1, IN2, and ENA.

Python Code:

import RPi.GPIO as GPIO

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 = GPIO.PWM(enablePin, 1000) # 1 kHz frequency

pwm.start(0)

try:

# Move Forward

GPIO.output(motorIn1, GPIO.HIGH)

GPIO.output(motorIn2, GPIO.LOW)

pwm.ChangeDutyCycle(75) # Speed control (0 to 100%)

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.

There are two main types:

 Unipolar (5 or 6 wires)

 Bipolar (4 wires – most common today)

Components You’ll Need:

Component Description

Stepper Motor (e.g., 28BYJ-48 or NEMA17) The motor you’re controlling

Motor Driver (ULN2003 for 28BYJ-48 or A4988/DRV8825 for NEMA17) Controls the motor

Arduino or Raspberry Pi Sends control signals

External power supply Needed for more torque

Jumper wires, Breadboard For connections

Option 1: 28BYJ-48 + ULN2003 + Arduino

Wiring:

 Connect the 4 IN pins on ULN2003 to Arduino pins 8, 9, 10, 11

 Connect motor to ULN2003 driver board

 Connect ULN2003 to 5V and GND

Arduino Code:

#include <Stepper.h>
const int stepsPerRevolution = 2048; // For 28BYJ-48 in half-step mode

Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11); // IN1-IN4 pins

void setup() {

myStepper.setSpeed(10); // Set speed in RPM

void loop() {

myStepper.step(stepsPerRevolution); // 1 full rotation clockwise

delay(1000);

myStepper.step(-stepsPerRevolution); // 1 full rotation counter-clockwise

delay(1000);

Raspberry Pi + Stepper (28BYJ-48 + ULN2003)

import RPi.GPIO as GPIO

import time

# GPIO pins for IN1 to IN4

control_pins = [17, 18, 27, 22]

GPIO.setmode(GPIO.BCM)

for pin in control_pins:

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:

for i in range(512): # ~1 full rotation

for step in half_step_seq:

for pin in range(4):

GPIO.output(control_pins[pin], step[pin])

time.sleep(0.001)

except KeyboardInterrupt:

GPIO.cleanup()

You might also like