Blinking LED python program using Raspberry Pi

ARNOLD SACHITH A HANS
Analytics Vidhya
Published in
2 min readNov 11, 2019

Let’s start with a very tiny program of blinking an LED using Raspberry Pi. Before we begin with the programming and execution part of the Raspberry Pi. We need to install some packages which serves as the bases for running the program. Kindly open the terminal window of Raspberry Pi and run the following commands. [Please remember to run each code line by line do not run all the codes at a time]

The following code updates and upgrades any existing packages:

Let’s install Python 3 header files which will be needed during compilation process:

Installing pip using following commands:

Installation of GPIO [General Purpose Input/Output] this package is exclusively used to play with sensors and actuators using Raspberry Pi. This package helps to accept external data from sensors as input and it is also helpful in sending signals out from Raspberry Pi.

To install the GPIO library package we need to execute the following command:

sudo apt-get install python-rpi.gpio python3-rpi.gpio

With the library installed using terminal window it’s time for you to type some code and get your favorite color LED blinking. Open your favorite Python IDE [I recommend Thonny Python IDE]

import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library

from time import sleep # Import the sleep function from the time module

GPIO.setwarnings(False) # Ignore warning for now

GPIO.setmode(GPIO.BOARD) # Use physical pin numbering

GPIO.setup(8, GPIO.OUT, initial=GPIO.LOW) # Set pin 8 to be an output pin and set initial value to low (off)

while True: # Run forever

GPIO.output(8, GPIO.HIGH) # Turn on

sleep(1) # Sleep for 1 second

GPIO.output(8, GPIO.LOW) # Turn off

sleep (1) # Sleep for 1 second

Compile the program and check for any errors and please be careful about the spelling mistakes if you are typing the code in Thonny. Assuming that there are no errors let’s proceed to know the pin diagram in Raspberry Pi [GPIO]

The connections between LED and Raspberry Pi is as follows:

The positive terminal of LED — — — — — Pin number 8

Negative terminal of LED — — — — — — — Pin number 6

[You can make use of an breadboard to perform these connections]

Now you can run the code in Thonny Python IDE and observe that the LED blinks with a delay of 1 second, this delay can be increased or decreased according to your requirement.

If I have missed out anything kindly keep me informed [email id: arnoldautomatic@rediffmail.com].

Hoping to see you building exciting projects using Raspberry Pi. #raspberrypi #blinkingLED #projectsusingraspberrypi #LEDblinking #raspberrypiLEDblinking

Cheers

Arnold Sachith

--

--

ARNOLD SACHITH A HANS
Analytics Vidhya

An Aspiring AI engineer|M.Tech (Artificial Intelligence)|B.E (Mechatronics Engineering)| Writer| Robots Rule| AI for the betterment of the society|