hardware gpio   3   2795
Programming on Raspberry Pi with Python: Controlling LED

In the first and second articles of the series 'Programming on Raspberry Pi with Python' we learned how to setup Raspberry Pi, configure WIFI and enable SSH.

In third article, we created a python script to send the IP address of Raspberry Pi on reboot to a telegram channel and scheduled it in crontab.

Now is the time to start exploring hardware side of Raspberry Pi. In this article we will control the LED (light emitting diode) via GPIO pins of Raspberry Pi by Python program.

Programming on Raspberry Pi with Python: Raspberry Pi Setup

Programming on Raspberry Pi with Python: WIFI and SSH configuration

Programming on Raspberry Pi with Python: Sending IP address on Telegram channel on Raspberry Pi reboot

We are using Raspberry Pi 3 B+ model.


 - Raspberry Pi 3B+ 
 - Jumper wires
 - Breadboard
 - LED 
 - Resistors

Understanding GPIO pins:

I strongly recommend you to visit https://www.raspberrypi.org/documentation/usage/gpio/ for better understandig the GPIO pins of your Raspberry Pi.

Alternatively you can go to command prompt of your Raspberry Pi and run the command pinout which will present you a handy reference of GPIO pins.

gpio pins pinout command

You can refer the instruction card received along with canakit.

gpio pins pinout command


Lets connect the components together.

- Connect pin 36 ( we will refer pins by literal sequence number, here pin 36 is GPIO pin 16 in above pic) with the live/voltage row on breadboard.
- Connect pin 6 (ground) with ground row on breadboard.
- Now connect a 220 ohm resistor with live row and positive terminal of LED.
- Connect the negative/ground terminal with ground row of breadboard.
- Use jumper wires for above connections. Refer my hand drawn diagram below for connections.

breadboard connections raspberry pi

L and G stands for live and ground respectively. 

Python Program:

Use below python code to control the LED.

import RPi.GPIO as GPIO
import time

PIN = 36

GPIO.setup(PIN, GPIO.OUT)         #LED output pin

while True:
    GPIO.output(PIN, 0)  #Turn OFF LED
    GPIO.output(PIN, 1)  #Turn ON LED

First we imported the RPi.GPIO module.
Since we will be dealing with pin number 36, lets define it in a variable PIN.

Set the mode to GPIO.BOARD, which means we are referring pin number by their position on board.
Set the pin 36 to receive output.

Until loop is terminal, set the output to false/0/low on pin 36 and then sleep for 1 second. In next lines, set the output to true/1/high and then sleep for 1 second. 

I clicked the below pictures to show LED in two states i.e. on and off. Look closely and try to figure out the connections.

LED Turned Off:

led controll raspberry pi

LED Turned On:

breadboard connections raspberry pi

Try it out and if you are stuck anywhere feel free to comment.

Purchase Raspberry Pi Starter Kit Here.


hardware gpio   3   2795

Related Articles:
Programming On Raspberry Pi With Python: Activate LED and Buzzer on Motion Detection
Turn on LED and activate the buzzer when motion is detected using PIR Motion detection sensor. Using PIR motion detection sensor along with buzzer with Raspberry PI. Python Program to detect motion and activating the alarm system using Raspberry Pi....

3 thoughts on 'Programming On Raspberry Pi With Python: Controlling Led'
Neil :
Awesome article liked it.

Anil :
This blog post is clear and really helpful

Piopi :
beautiful explanation with diagrams

Leave a comment:

*All Fields are mandatory. **Email Id will not be published publicly.

Please subscribe to get the latest articles in your mailbox.

Recent Posts:

© pythoncircle.com 2018-2019
Contact Us: code108labs [at] gmail.com
Address: 3747 Smithfield Avenue, Houston, Texas