Raspberry Pi Blink LED
This tutorial with Python code shows how to make a light emitting diode (LED) blink using a general-purpose I/O (GPIO) pin of the Raspberry Pi to control it. This is a very simple "hello world" style tutorial, which will show how to build the circuit on a breadboard, how to calculate the value of the current limiting series resistance, and how to control it through code. Once you have learned to blink one LED you could then graduate to pulse-width modulation (PWM) to make it fade and control its brightness.
Python is a wonderful language for learning purposes, and just one-step away from ADA. Before you know it, your little whippersnapper will be designing military avionics and building aircraft systems. Since I plan to be around a little longer, I figured I had better show the adults how to design it properly.
GPIO Current
There is a limit to how much current you can draw from the GPIO pin. The absolute maximum is 16 mA. If you draw more current, then there is a serious risk of causing damage. Luckily, you can get high efficiency LEDs that will light even when the current is as little as 2 mA. Therefore, a suitable current limiting series resistor is used, and in the following sections of this article, you can see how its value is calculated.
Overview
These tutorial examples are for beginners. Tutorial 1 shows how to find the correct LED, and calculate the value of the series resistor. Tutorial 2 shows how to make this LED blink with some simple code examples, in Python.
LED
For this project I am using the commonly available 5 mm red LED, which has a forward voltage of 1.7 V, and a forward current of 0.002 A. These are very inexpensive and you can get them in a bag of 100 for a pound on eBay, and shops listed on Google. Whilst you are shopping you could also grab a bag of assorted resistors.
If you get other LEDs such as green or blue, then they will have a different forward voltage, and forward current rating. Therefore, you will need to recalculate the value of the series resistance.
Series Resistance Calculation
As you can see from the diagram shown above, I intend to draw only 0.002 A which is the absolute minimum the LED requires to light. The voltage across the LED is the forward voltage parameter, which is the amount required to light it. The voltage across the resistor is the amount the resistor needs to drop.
The logic 1 signal from the GPIO pin produces 3.3 V to power the LED, however, the LED requires only 1.7 V to light. Therefore, the series resistor has to drop the voltage by 1.6 V. Using Ohm’s Law the value of the resistor was found to be 800 Ω. Using the standard resistor values chart, the closest value was 680 Ω, which is blue, grey, brown, and gold in a 4-band colour code system.
LED Brightness
Drive Current | Series Resistance R |
2 mA | 800 Ω |
3 mA | 533 Ω |
4 mA | 400 Ω |
5 mA | 320 Ω |
If you wish to experiment to make the LED brighter then you will need different resistor values. In this table, I have calculated the values required for 3 mA, 4 mA, and 5 mA drive currents. The LED lights brighter when the drive current increases.
You can use the Current Limiting Series Resistance Calculator for LEDs article if you wish to calculate different values. However, you should remember not to draw any more than 16 mA to avoid damaging the GPIO pin.
Testing the Circuit
To test the circuit the simplest way is to connect it between pin 1 (+3.3 V) and pin 6 (ground) on the GPIO port. If it lights, then you have a good circuit that you can play with further. If you would like to increase its brightness, then you could experiment with different resistor values shown in the table above to see how bright it becomes. However, I would keep the resistor value between 400 Ω to 800 Ω, maintaining a wide margin of safety.
Test Circuit Breadboard Build
To build the test circuit on breadboard, click on the diagram above to see how to connect the wires to the GPIO pins. The breadboard build is very simple and should take less than five minutes to build. It shows you where pin 1 and pin 6 are located on the GPIO port.
Tutorial 2: Raspberry Pi Blink LED
In this tutorial, we make a slight change to our test circuit by removing the wire connected to pin 1 on the GPIO port and connecting it to pin 11. I have chosen this pin arbitrarily, and you could use almost any GPIO pin.
According to the Broadcom convention, Pin 11 is GPIO17. This is the same on both versions of the Raspberry Pi boards, the first version, and revision 2. However, in the event you have a newer board version 3, then please consult the manual.
Tutorial 2: Breadboard Layout
Please click on the image above to see the breadboard layout. It also shows how to connect the wires to the GPIO pins. If the LED lights up in Tutorial 1, then all you need to do in this section is to connect the red jumper wire to Pin 11. Everything else remains the same.
Installing Python
If you have not already installed the software, then this section shows how to do that. You will need to type the commands shown within the quotes. There are many ways to do this however; I am going to choose the easiest way possible for beginners. If your system has Python already installed then go to the next stage.
Firstly, you have to install pip using the following command.
"sudo curl https://raw.github.com/pypa/pip/master/contrib/get-pip.py | python"
Secondly, Obtain the Python module RPi.GPIO 0.5.2a form their website at
"https://pypi.python.org/pypi/RPi.GPIO"
Finally, install the Python module. Since the program accesses the physical GPIO hardware, it has to execute from root as "superuser". Hence, use sudo, which allows you to execute pip and install commands as shown below.
"sudo pip install rpi.gpio"
Python Configuration
Start the Python interpreter by using "sudo" because Python needs "superuser" permissions to use the GPIO hardware:
"sudo python"
Then you have to import the GPIO library by using this statement.
"import RPi.GPIO as GPIO"
Python has two different pin numbering schemes. This statement sets the numbering scheme you will be using.
The BCM scheme signifies the Broadcom pin number system, which uses the pin designations on the SoC chip. This system is more complicated because you have to figure out how the pin numbers translate to GPIO pin numbers.
The BOARD scheme uses pin numbers marked on the GPIO header connector. It is better to use this scheme because you can simply count off the pin numbers on the header. The Raspberry Pi Pin Numbers – Python article has more information regarding this. Therefore, type the following configuration command.
"GPIO.setmode(GPIO.BOARD)"
Set Pin 11 to Output Mode
Before you can actually use the GPIO pin, you need to tell the processor that you will be using it for output. Hence, the following command sets pin 11 to output mode. This has to be set only once.
"GPIO.setup(11, GPIO.OUT)"
Python Blink Commands
The next step is to focus on the software commands to make pin 11 produce 3.3 V. Sending logic 1 output to pin 11 will make it produce 3.3 V, because in computer language logic 1 means 3.3 V at the hardware level.
In the Python language, true signifies logic 1, and false logic 0. Therefore, the following command will make the LED light.
"GPIO.output(11, True)"
Notice that the LED remains lit until you send a command to remove the 3.3 V supply. You can accomplish this by sending logic 0 signal, or false in the Python language. Therefore, the following command will make pin 11 produce 0 V and the LED will switch OFF.
"GPIO.output(11, False)"
Have Fun!
The next step is to place those commands into a loop so that the LED blinks automatically. You will also need to place a delay within the loop. The delay will determine how fast the LED blinks.
Related Articles
Raspberry Pi Blink LEDTest Circuit Breadboard Build
Raspberry Pi Blink LED Tutorial 2
Raspberry Pi Pin Numbers - Python
Current Limiting Series Resistance Calculator for LEDs