Blinking LED on Raspberry Pi 3 using GPIO via Python
In this tutorial you will learn how to connect a LED light to your Raspberry Pi’s on-board general purpose input-output (GPIO) pin and make it blink using Python. Before proceeding with the tutorial, let’s get familiar with GPIO pins.
GPIO is a generic pin on an integrated circuit (In case of Pi its BCM2837 SoC) whose behavior (including whether it is an input or output pin) can be controlled (programmed) by the user at run time. By making connections to these pins (also known as header pins) the Raspberry Pi can interface to the electronic world consisting of sensors and indicators and actuators.
The simplest thing you can do with an Raspberry Pi to see an actual physical output is to blink an LED! It’s more like an electronics version of the classical hello world program that we write when learning a new software language.
For this tutorial, you will be needing some passive electronics components which you can easily find in your local hardware store.
- Jumper Wires
- LED’s, preferably Red ones
- 470 Ohm resistor
- Latest ver. of Raspbian
We’re going to use pin 9 and 11 as you can see in the picture below. You can refer to this link for Raspberry Pi GPIO numbers. Make sure to setup the circuit exactly as the below schematic diagram and take note of the LED polarity. The negative lead of the LED is the shorter of the two leads.The bottom of the LED next to the positive lead is usually curved. This is useful if you cut the leads off shorter first and then forget which lead was the shorter one. A series resistor of 470 ohm is required to limit the current in the LED to a safe value.
To make the LED blink we will be using RPi.GPIO: a popular Python based library used on Raspberry Pi platforms to control GPIO pins.The RPi.GPIO module is already installed by default in Raspbian. But to make sure that it is at the latest version connect your Raspberry Pi to the internet using either a LAN cable or a WiFi adapter and type in the below commands in terminal.
$ sudo apt-get update $ sudo apt-get install python-rpi.gpio python3-rpi.gpio
Now we need to create a Python script to make it blink! The Python source code for flashing the LED is listed below:
Create new python file, blink.py copy the code below and save it.
import RPi.GPIO as GPIO import time LedPin = 11 # pin11 def setup(): GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location GPIO.setup(LedPin, GPIO.OUT) # Set LedPin's mode is output GPIO.output(LedPin, GPIO.HIGH) # Set LedPin high(+3.3V) to turn on led def blink(): while True: GPIO.output(LedPin, GPIO.HIGH) # led on time.sleep(1) GPIO.output(LedPin, GPIO.LOW) # led off time.sleep(1) def destroy(): GPIO.output(LedPin, GPIO.LOW) # led off GPIO.cleanup() # Release resource if __name__ == '__main__': # Program start from here setup() try: blink() except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed. destroy()
To run the code from the command prompt on the Raspberry Pi, enter the following:
sudo python blink.py
You must be in the same directory as the blink.py file. If everything went according to plan, you should see the LED blink with a delay of 1 second. Press Ctrl + C to terminate the program.