Lighting LED with Raspberry Pi & Python script

As we have completed the setup of Raspberry Pi now & we are ready for running the projects using Raspberry pi. Let us proceed with our first simplest project. In this tutorial we will light a LED light which is connected to Raspberry Pi GPIO Pin using python programming.

What do you need:

  • Raspberry Pi
  • Bread Board
  • LED
  • Resistor -220 ohm
  • Jumper wires - Male to Female

Using a breadboard is the best way to do prototyping the electronic circuits rather soldering the components together on a PCB.We use breadboard to test circuit design & it is easy to make the changes , as we just need to re-plug the wires.

Below is the picture of breadboard & I have marked how the holes in the breadboard are interconnected. The top & bottom row holes are interconnected in the row & not in columns. However in the middle section in between, they are not interconnected in row hole , they are interconnected in the column holes.

Breadboard

We are also using a LED ( Ligh emitting diode), if you notice the LED one of the leg of LED is longer that the other. The longer leg is positive or called as Anode & shorter leg will negative or connected to ground which is called as Cathode. LED will glow when current is passed through it.

LED

We will be using 220 Ohm resistor in the circuit, we must always use the resistor to control the current flow between Raspberry Pi & LED. Resistor controls the current flow, Raspberry can only give a limited amount of current. The resistor value are calculated based on the color coding on the resistor, you can google around to find more details.

Resistor

We are using male to female jumper wires, as it is easy to connect to GPIO pins in Raspberry Pi & other end to breadboard.

Male to female Jumper wire

The details for GPIO pins I have described in my previous post, refer the post my IoT devices .

You can test the LED by connecting the LED to a power source like battery through a resistor.
I have used a 9V battery for testing, connect the positive to LED anode( Long led) , the LED cathode (shorter leg) is connected to 220 Ohm resistor & other end of resistor is connected to the negative point of battery. Refer the below pic.

Testing LED with Battery

Now in actual circuit with Raspberry pi ( I have used Raspberry Pi zero W), I have used the GPIO pin 7 for the circuit. Connect the GPIO PIN 7 to the anode of LED & the GPIO PIN 6 which the GND(ground) connect to the other end of resistor. Refer the below screenshot. Once we have the circuit setup & the raspberry pi is powered on, it is now time to write the python code.

LED circuit with Pi

To write the python code you can use python IDLE or you can even use text editor & save the file with extension .py. I prefer python IDLE as it comes with Raspbian libraries & it is easy to debug, also the errors in syntax can be easily identified.

Lets create a file LED.py & start our python program. I will start with a simple program to turn on the LED when we run the script.

Script 1 : Turn on LED

import RPi.GPIO as GPIO
# We are importing the library , which will be telling the python interpreter how to work with Raspberry PI GPIO Pins.
GPIO.setmode(GPIO.BCM)
# This will inform on how pin numbering on board to be used.
GPIO.setup(7, GPIO.OUT)
# we are setting GPIO Pin 7 as an Out Pin
GPIO.output(7,True)
# Setting the GPIO Pin 7 as True will make the GPIO pin 7 to High

Save the file & run the LED.py file. We need to run the script in superuser. To run the code

sudo python LED.py

The LED will be lit & will always on until we close the program.

Script 2: Keep the LED lit for 5 seconds & then turn off.

import RPi.GPIO as GPIO
# We are importing the library , which will be telling the python interpreter how to work with Raspberry PI GPIO Pins.
import time
# We are improting the time library in this script to define the duration of the LED to be lit.
GPIO.setmode(GPIO.BCM)
# This will inform on how pin numbering on board to be used.
GPIO.setup(7,GPIO.OUT)
# we are setting GPIO Pin 7 as an Out Pin.
GPIO.output(7,GPIO.HIGH)
# Setting the GPIO Pin 7 to High
time.sleep(5)
# Setting the duration to 5 sec to keep the LED lit.
GPIO.output(7,GPIO.LOW)
# Setting the GPIO Pin 7 to Low.
GPIO.cleanup()
# This is used to cleanup the GPIO Pin state.

Run the script using sudo python LED.py

Script 3: Blinking the LED for 10 times with delay of 5 sec in between.

import RPi.GPIO as GPIO
# We are importing the library , which will be telling the python interpreter how to work with Raspberry PI GPIO Pins.
import time
# We are improting the time library in this script to define the duration of the LED to be lit.
GPIO.setmode(GPIO.BCM)
# This will inform on how pin numbering on board to be used.
GPIO.setup(7,GPIO.OUT)
# we are setting GPIO Pin 7 as an Out Pin
for i in range(0,10):
# Run loop 10 times
         GPIO.output(7,GPIO.HIGH)
         # Setting the GPIO Pin 7 to High.
         time.sleep(5)
         # Setting the duration to 5 sec to keep the LED ON.
         GPIO.output(7,GPIO.LOW)
         # Setting the GPIO Pin 7 to Low.
         time.sleep(5)
         # Setting the duration to 5 sec to keep the LED OFF.
GPIO.cleanup()
# This is used to cleanup the GPIO Pin state.

We have successfully completed the first projects with LED in multiple variations. I hope you guys have enjoyed this.

2 comments

Leave a Reply

Your email address will not be published. Required fields are marked *