Raspberry Pi Blinking an LED

Final output

In the Raspberry Pi Blinking an LED Blinking an LED project , we will learn how to connect an LED to raspberry pi and create a Python script that will do LED Blinking. We will also learn how to connect an LED to the GPIO Pin.

The funny thing is, if you know how to control an LED, you can control any load or output. Such as LED, Bulb, Motor, etc. whatever. 


≡Necessary equipment:


To complete our raspberry pi blink on board led project   we need the following components    

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

GPIO pin introduction of Raspberry pi:

The full meaning of GPIO is General Purpose Input / Output Pins (General Purpose Input / Output (GPIO) pins). This GPIO PIN can be used to connect a variety of electronics hardware, such as LEDs or sensors. The GPIO pin is used to exchange real-world data with the Raspberry Pi. The Raspberry Pi 3B model has 40 GPIO pins in two rows. Which is shown in Figure 1. This layout is similar to the Raspberry Pi 2 model and Raspberry Pi B 1 model B +. However, the Pi1 is slightly different from models A and B. Which has only the first 26 pins.

Raspberry Pi GPIO layout
Figure 1  Raspberry Pi GPIO layout

There are two ways to declare a GPIO PIN. One is by name (known as GPIO number or Broadcom numbering) and the other by pin number (known as physical number).

For example, GPIO 25 (name) pin 22 (number) single pin has been identified in two ways. Which is shown in the figure below.

raspberry pi intout

 the names of the GPIO pins as well as the GPIO pins will be mentioned. Note that GPIO pins can be set to HIGH or LOW. If a pin HIGH is set, it will provide 3.3 voltage at the output. Which turns on the LED Similarly, if pin LOW is set, 0 voltage can be found at its output.

Warning:

GPIO pins are designed to operate at 3.3 volts DC. So if you connect the pin to high voltage, it will permanently damage the raspberry pi.

As shown in the table, none of the gray pins are associated with pins 27 and 28. The Raspberry Pi has 8 (eight) GND (ground) pins for ground connection to the circuit, highlighted in black on the table.

In the table above, there are four power pins for power supply. Two 5 V pins and two 3.3 V are highlighted in red and orange, respectively.

Also, several pins have special features. For example, yellow highlighted pins are used for serial peripheral interfaces (SPIs) and green highlighted pins are used for inter-integrated circuit (I2C) communication.


≡LED Introduction:


LED stands for Light Emitting Diode and emits light. LEDs can come in a variety of shapes, sizes and colors. In this project we will use a simple 5 mm, red LED.

We know that a diode is an electronics component and has polarity (positive and negative edges). It flows current in one direction only. That is, only current flows from positive to negative.

 Pin out of the LED
  Figure 1-2 Pin out of the LED

 

The LED has a positive connection edge. Which is known as the anode and has a negative connection edge. Which is known as the cathode. By looking at the length of the two legs or the internal structure of an LED, you can tell which is negative and which is positive. Which is shown in Figure 1-2. The long leg or slender inner part is the anode (+) and the short leg or thick inner part is the cathode (-).

                                                                                                    

Ways to choose the right register:

If excess current flows through the LED or raspberry pi board, both the raspberry pi and the LED are likely to be damaged.

To prevent this, we must always connect the LED to a register in series. This causes a limited amount of current to flow through the component. What kind of resistor and what kind of resistor you will use depends on the use of your LED.

For the LED of this project, one can take a register of any value between 220 to 470. Because of the relatively low resistance quality register, the LED light will be brighter.

NOTE:

We recommend that you purchase a 1/4 W resistor assortment that spans a wide range of resistance values. These will meet most of your needs.

The resistance value of the register depends on the color band of the register. The register usually has four bands. Which is shown in Figure 3.

 330 Ω resistor
Figure 3 A 330 Ω resistor

The first two colors of the register determine the value of the band. The third is a multiplier that represents the number of zeros after the first two digits i.e. 10 n. Here, n is the value of the third number.  

The fourth is the tolerance of the register. The percentage of which indicates how much the actual register may be above or below the given value.


≡Circuit Wiring:


Now you are ready to make your raspberry pi 4 blinking led circuit. All the projects in this book we will create using solderless breadboard, where you can easily make and test your socket without using solder.

Follow the steps below to complete the LED Blinking projectand  how to raspberry pi blinking led assembly.

  1. Connect one of the GND pins of the Raspberry Pi to a breadboard rail (pins 6, 9, 14, 20, 25, 30, 34 and 39 provide GND).
  2. Connect the LED to the breadboard.
  3. Connect a 330 ohm resistor to the positive side of the LED.
  4. Now connect the other end of the register to GPIO 25 (pin 22) of Raspberry Pi.
  5. Connect the negative edge of the LED to the ground.

Wiring an LED to the Raspberry Pi
Figure 4 Wiring an raspberry pi blinking an led


≡Script creation: 


Create a folder called your desktop environment project to save all your project scripts.

 Inside the newly created folder, create a new folder called LED. Where you will save the projects in this book.

Here’s how an LED blinking project works:

  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 will continue until it is closed manually.

The pin bar to control the Python is a library to use his name, gpiozero.


≡Script Writing


Now extract the python IDE image of Raspberry Pi and write the raspberry pi blink led python code project  program and save it as blinking_led.py.

#importing necessary libraries
➊ from gpiozero import LED
➋ from time import sleep
  #create an object called led that refers to GPIO 25
➌ led = LED(25)
  #create variable called delay that refers to delay time in seconds
➍ delay = 1
➎ while True:
      #set led to on for the delay time
    ➏ led.on()
      print('LED set to on')
    ➐ sleep(delay)
      #set led to off for the delay time
    ➑ led.off()
      print('LED set to off')
      sleep(delay)


≡Script Analysis:


➊ from  gpiozero import  LED
➋ 
from  time import  sleep

Here LEDs are imported from a library called gpiozero to control the LEDs  . At the same time sleep  has been imported from the time library to control the time   .

Led = LED ( 25 )

Here an object named led has been created where GPIO will store the value of 25 pins.

➍ delay = 1

Here a  variable named delay has been created where time will store. Here you can specify how often you want to turn the LED on or off.

➎ while True : 

Loops are used in programming to repeat specific code blocks. If the value of the while loop  is True the code inside the while loop is executed and this process continues until the value of the loop is false (0)


➏ led.on ()
print ( 
‘LED set to on’ )

Here the output of the LED is set to high mode. The LED is raised using the led.on () function and if the LED is high then the message LED set to on will be displayed on the serial monitor .


➐ Sleep (delay)
 Here is the time you want to keep the LED high. You can give different delay values ​​to control the time if you want.    ➑ led.off ()      print ( ‘LED set to off’ )

 

Here the output of the LED is set to low mode The LED is lowered using the led.off () function and if the LED is low the LED set to the off message will be displayed on the serial monitor.
sleep (delay)

Here’s how long you want to keep the LEDs low.


≡Ways to run the Raspberry Pi program:


To run, first, you have to save or save the above program, then you have to run Run ▸ Run current script or press F5 from the keyboard then the program will run.

Your circuit should look like Figure 5 with your LED on and off every second. To close the running program, press CTRL + F2.

 Final output
Figure 5 Final output Raspberry Pi Blinking an LED

Congratulations! You have successfully run your Raspberry Pi Blinking an LED project.

The best way to learn electronics and programming is to experiment. Here are two general ideas you can try:

  • You change the car time in the script and try to understand the change by looking at the output.
  • You try to control more than one LED by changing the circuit and script.

RASPBERRY PI TUTORIALS FOR BEGINNERS

 29 total views,  1 views today