|Project Name: Blinking LED
Language: Python 3
Date: 29 Mar 2020
Hardware: Raspberry Pi 4
Library: RPi GPIO
This article provides the steps to connect a red Light-Emitting-Diode (LED) and have it turn on and off using the Python programming language. The tutorial will provide the steps to setup the circuit, write the code to make the electrical current flow and stop which makes the LED blink on and off.
Adding the OS
Open an internet browser and go to the Raspberry PI website and download the Raspberry Pi operating system called Raspbian.
This same website also has helpful ways and links to programs that will install the Raspbian Operating System on an SD card.
Install the SD card and perform updates by clicking on the Terminal icon in the upper-right-corner of the Raspberry Pi screen and type the following commands. It may take time for the Raspberry Pi to download and install the updates.
|sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade
With the library added and verified that it is installed, the next step is to wire an LED to the Raspberry Pi.Table 1. Breadboard and Schematic Diagrams
Components needed are an LED, and a resistor (1 kilo Ohm) and a couple of wires. Wiring this circuit with a breadboard helps prevent mistakes that can short out your motherboard, which keeps the wires separate and makes it easier to follow the circuit. I personally like the Fritzing App (fritzing.org) to create schematics and breadboard diagrams. Fritzing is free and I suggest that you make a donation to help with their noble cause. The resistor can be connected with the current flow in any direction, and the LED fill need to have the long-leg side connected to the positive (GPIO 5, or pin 29). The long leg of the LED is positive, and the short leg of the LED is connected to ground.
When pin #29 is turned-on then 3.3 volts is sent through the resistor to the LED where the circuit is complete when the other side of the LED is connected to ground and the LED lights up.
Write Python Code
The following code is written in the latest Python 3 language. This code initializes the two libraries called time and RPi.GPIO. Time is a library that has commands for the length of time that certain code can be delayed or monitored. RPi.GPIO is the library with the commands that allow Python to interact with the GPIO (General Purpose Input Output) pins on the Raspberry Pi. Setmode is the command used to determine the pin numbering type which is used in this example. There are only two types BCM or BOARD and you can choose only one and BCM is the most common.
Code 1. Python 3 Blink Code (Example1-1)
|import RPi.GPIO as GPIO
There is an option to allow warnings to be viewed which is not needed with this simple example. For more advanced examples and to help with troubleshooting code then this option may be set to True. The setup call is to be usually only called a few times throughout the program, and this is to set Pin #5 to an output pin, meaning that voltage and current are going-out of Pin #5.
A while-loop is used to have Pin #5 set to +3.3 volts with the command high to turn on the LED, and then waits for half of a second (0.5) and the turns the volts to 0.0 with the command low, which turns off the LED. Another half second occurs and the loop is repeated.
That’s it and congratulations! You have created your first Python-Raspberry Pi-LED blinking example.