Complete Guide Blinking an LED with Raspberry Pi

Wiring an LED to the Raspberry Pi. The LED lead with a bend in it is the anode.

In this 1st project, you’ll be Blinking a light-emitting diode(LED) to your Raspberry Pi and create it blink with an easy python Code. And Learning how to blink light-emitting diode(LED) using the GPIO pins is an And also you recognize the way to control a light-emitting diode(LED).


≡ Parts Required


  1. Raspberry Pi
  2. Breadboard
  3. 5 mm LED
  4. 330 Ω resistor
  5. Jumper wires

≡Circuit Diagram Blinking LED Raspberry Pi


Now you’re able to build your LED circuit.  To wire an LED to the RaspberryPi, follow these next steps. Use the wiring diagram in Figure 1.

  1. Connect a blue breadboard rail to one of the Raspberry Pi GND pins (pins 6, 9, 14, 20, 25, 30, 34, and 39 all provide GND).
  2. Insert the LED into the breadboard.
  3. Insert the 330 Ω resistor into the breadboard so that one side is connected to the LED anode (the longer, positive lead).
  4. Connect the other side of the resistor to GPIO 25 (pin 22) with a jumper wire.
  5. Connect the LED cathode (the shorter, negative lead) to the GND power rail.
Wiring an LED to the Raspberry Pi. The LED lead with a bend in it is the anode.
Figure 1 Wiring an LED to the Raspberry Pi. The LED lead with a bend in it is the anode.

We’ve provided you instructions on where to place the resistor, but actually, it doesn’t matter whether it’s joined to the anode or cathode as long as it is attached to one.  we didn’t just join the LED cathode directly to pin 6 (GND) since we only require the GND rail for that one connection. The reason is that its good work to use the breadboard GND rails, which will grow more useful in more advanced circuits.


≡  Code Blinking LED Raspberry Pi


To keep all your projects organized, create a folder called PiProjects in your RaspberryPI desktop environment to save all your projects’ Code.create a new folder called LEDs where you’ll save the RaspberryPI LED projects.

In simple conditions, the blinking LED project works as follows:

  1. The LED turns on for 1 second—GPIO 25 set to HIGH.
  2. The LED turns off for 1 second—GPIO 25 set to LOW.
  3. The LED is on again for 1 second—GPIO 25 set to HIGH.
  4. The LED is off again for 1 second—GPIO 25 set to LOW.

This program continues until you tell the program to stop.

we’ll control the RaspberryPi GPIOs using a Python library called gpiozero. The gpiozero library gives a collection of interfaces for everyday components like LEDs, buttons, potentiometers, sensors, and much more.

Open RaspberryPi Python 3 (IDLE) and go to File ▸ New File to create a new file. Now Copy this code to the Python Editor and save this as blinking_led.py inside the LEDs folder.

from gpiozero import LED
from time import sleep
led = LED(25)

delay = 1

while True:
    led.on()
    print("LED set to on")
    sleep(delay)
    led.off()
    print("LED set to off")
    sleep(delay)

≡ Code Analysis Blinking LED Raspberry Pi


from gpiozero import LED 
from time import sleep
Importing Libraries

At you import LED from the gpiozero library for control the GPIO that the LED is connected to. and import the sleep() function from the time module.

led = LED(25)
Declaring the Pin

Now you create an LED object named led that refers to GPIO 25, which is the GPIO the LED is attached to. If you create and apply this LED object Python code knows GPIO 25 is output and thus should be set to HIGH or LOW. After this code, you can use led to refer to your GPIO 25.

while True:
Starting the while Loop

At you start a while loop with the state True, which means this loop will run forever until you stop the program yourself. The lines of code that follow the loop code are indented, telling Python that this is the content of the loop to be run as long as the while condition is met.

led.on()
print("LED set to on") 
sleep(delay)
led.off()
print("LED set to off") 
sleep(delay)
Setting the Digital Output

Now you require to set the digital output for the LED. You utilize the led.on() function to set GPIO 25 to HIGH, turning the LED on, and the led.off() function to set GPIO 25 to LOW, turning the LED off. There is a pause of 1 second within each LED state using the sleep() function, which creates the blinking effect. The code stops where it is and waits for the amount of time specified in the delay variable before proceeding to the next line of code. This allows you to keep the LED on or off for a given period of time.


≡ Hardware Analysis Blinking LED Raspberry Pi


Overview of GPIO Pins

The General Purpose Input/Output (GPIO) pins provide you to connect electronic hardware, like LEDs and sensors, to your RspberryPi. They can be utilized to both read and send information, allowing your RaspberryPi to interact with the real world.

The Raspberry Pi 3 Model B board has a double row of 40 GPIO pins, shown. Here this layout is the same for the Pi 2 Model B and Pi 1 Model B+, but lightly different from the Pi 1 Model A and B, which have only the first 26 pins.

Raspberry Pi GPIO layout
Raspberry Pi GPIO layout

There are two methods to refer to a GPIO pin: its name or by its identical pin number (. For example, GPIO 25 corresponds to pin 22.  GPIO pins can be set to HIGH, which outputs 3.3 V and turns a part on, or LOW, which outputs 0 V and turns the part off.

 FUNCTION

NAME

NUMBER

NUMBER

NAME

FUNCTION

DC power

3.3 V

1

2

5 V

DC power

SDA1, I2C

GPIO 2

3

4

5 V

DC power

SCL1, I2C

GPIO 3

5

6

GND

GPIO_GCLK

GPIO 4

7

8

GPIO 14

TXD0

GND

9

10

GPIO 15

RXD0

GPIO_GEN0

GPIO 17

11

12

GPIO 18

GPIO_GEN1

GPIO_GEN2

GPIO 27

13

14

GND

GPIO_GEN3

GPIO 22

15

16

GPIO 23

GPIO_GEN4

DC power

3.3 V

17

18

GPIO 24

GPIO_GEN5

SPI_MOSI

GPIO 10

19

20

GND

SPI_MISO

GPIO 9

21

22

GPIO 25

GPIO_GEN6

SPI_CLK

GPIO 11

23

24

GPIO 8

SPI_CE0_N

GND

25

26

GPIO 7

SPI_CE1_N

I2C ID EEPROM

DNC

27

28

DNC

I2C ID EEPROM

GPIO 5

29

30

GND

GPIO 6

31

32

GPIO 12

GPIO 13

33

34

GND

GPIO 19

35

36

GPIO 16

GPIO 26

37

38

GPIO 20

GND

39

40

GPIO 21

 

Overview of  LEDs

The LEDs come in a wide variety of sizes, shapes, and colors, and some can even mix colors to create almost any color. In this project, you’ll use a simple 5 mm red LED.

 Diodes are electronic components that have polarity, meaning they allow current to flow in only one direction, from positive to negative. LEDs, like all diodes, have a positive connection known as an anode, and a negative connection known as a cathode. The two legs, or leads, on LEDs, are of different lengths to help you identify which is positive and which is negative,  The longer lead is the anode (+) and the shorter lead is the cathode (–).

A 5 mm red LED
A 5 mm red LED

How to Finding The Right Resistor 

LEDs can work only so much current before they overload and burn out, which can possibly damage the LED and even the Raspberry Pi board. To prevent this, you must always connect LEDs in series with a resistor: a small component that limits the amount of current passing through it.

The Resistors get in all sorts of values, and you need one that’s strong sufficient to protect your component without being so strong that it limits the element capabilities. For example, a stronger resistor can dull the light an LED provides off. The appropriate resistance value depends on the LED you’re using—most LEDs you’ll use in electronics can handle a maximum current rating of 20 mA. For the LED in this project, picking up a resistor of any value between 220 Ω and 470 Ω is fine, and within this range, a lower resistance value will result in a slightly brighter LED.

The resistance value is shown by the color bands on the resistor. Resistors usually have four bands. The first two represent the first two digits of the value. The third is a multiplier representing the number of zeros after the first two digits. The fourth is the tolerance of the resistance, which notes the rate that the actual resistance might be above or under the given value. For example, a 330 Ω resistor with a 5 percent tolerance might be any value between 313.5 Ω and 346.5 Ω.

A 330 Ω resistor
A 330 Ω resistor

RASPBERRY PI TUTORIALS FOR BEGINNERS

 203 total views,  1 views today