1. Chapter LED
This chapter is the Start Point in the journey to build and explore RPi electronic projects. We will start with simple “Blink” project.
1.1. Project Blink
In this project, we will use RPi to control blinking a common LED.
1.1.1. Component List
Raspberry Pi (Recommended: Raspberry Pi 5 / 4B / 3B+ / 3B) (Compatible: 3A+ / 2B / 1B+ / 1A+ / Zero W / Zero) |
||
Breadboard x1 |
||
GPIO Extension Board & Ribbon Cable |
Resistor 220Ω x1 |
|
Jumper Specific quantity depends on the circuit. |
LED x1 |
|
In the components list, 3B GPIO, Extension Shield Raspberry and Breadboard are necessary for each project. Later, they will be reference by text only (no images as in above).
1.1.2. GPIO
GPIO: General Purpose Input/Output. Here we will introduce the specific function of the pins on the Raspberry Pi and how you can utilize them in all sorts of ways in your projects. Most RPi Module pins can be used as either an input or output, depending on your program and its functions.
When programming GPIO pins there are 3 different ways to reference them: GPIO Numbering, Physical Numbering and WiringPi GPIO Numbering.
1.1.2.1. BCM GPIO Numbering
The Raspberry Pi CPU uses Broadcom (BCM) processing chips BCM2835, BCM2836 or BCM2837. GPIO pin numbers are assigned by the processing chip manufacturer and are how the computer recognizes each pin. The pin numbers themselves do not make sense or have meaning as they are only a form of identification. Since their numeric values and physical locations have no specific order, there is no way to remember them so you will need to have a printed reference or a reference board that fits over the pins.
Each pin’s functional assignment is defined in the image below:
See also
For more details about pin definition of GPIO, please refer to http://pinout.xyz/
1.1.2.2. PHYSICAL Numbering
Another way to refer to the pins is by simply counting across and down from pin 1 at the top left (nearest to the SD card). This is ‘Physical Numbering’, as shown below:
1.1.3. GPIO Numbering
You can use the following command to view their correlation.
$ Pinout
1.1.4. Circuit
First, disconnect your RPi from the GPIO Extension Shield. Then build the circuit according to the circuit and hardware diagrams. After the circuit is built and verified correct, connect the RPi to GPIO Extension Shield.
Caution
Avoid any possible short circuits (especially connecting 5V or GND, 3.3V and GND)!
Warning
A short circuit can cause high current in your circuit, create excessive component heat and cause permanent damage to your RPi!
Schematic diagram
Hardware connection
Tip
If you need any support, please contact us via: support@freenove.com
Attention
Do NOT rotate Raspberry Pi to change the way of this connection.
Please plug T extension fully into breadboard.
The connection of Raspberry Pi T extension board is as below. Don’t reverse the ribbon.
Note
If you have a fan, you can connect it to 5V GND of breadboard via jumper wires.
How to distinguish resistors?
There are only three kind of resistors in this kit.
The one with 1 red ring is 10KΩ
The one with 2 red ring is 220Ω
The one with 0 red ring is 1KΩ
Note
Future hardware connection diagrams will only show that part of breadboard and GPIO Extension Shield.
1.1.5. Component knowledge
1.1.5.1. LED
An LED is a type of diode. All diodes only work if current is flowing in the correct direction and have two Poles. An LED will only work (light up) if the longer pin (+) of LED is connected to the positive output from a power source and the shorter pin is connected to the negative (-) output, which is also referred to as Ground (GND). This type of component is known as “Polar” (think One-Way Street).
All common 2 lead diodes are the same in this respect. Diodes work only if the voltage of its positive electrode is higher than its negative electrode and there is a narrow range of operating voltage for most all common diodes of 1.9 and 3.4V. If you use much more than 3.3V the LED will be damaged and burnt out.
Note
LEDs cannot be directly connected to a power supply, which usually ends in a damaged component. A resistor with a specified resistance value must be connected in series to the LED you plan to use.
1.1.5.2. Resistor
Resistors use Ohms (Ω) as the unit of measurement of their resistance (R). 1MΩ=1000kΩ, 1kΩ=1000Ω.
A resistor is a passive electrical component that limits or regulates the flow of current in an electronic circuit.
On the left, we see a physical representation of a resistor, and the right is the symbol used to represent the presence of a resistor in a circuit diagram or schematic.
The bands of color on a resistor is a shorthand code used to identify its resistance value. For more details of resistor color codes, please refer to the card in the kit package.
With a fixed voltage, there will be less current output with greater resistance added to the circuit. The relationship between Current, Voltage and Resistance can be expressed by this formula: I=V/R known as Ohm’s Law where I = Current, V = Voltage and R = Resistance. Knowing the values of any two of these allows you to solve the value of the third.
In the following diagram, the current through R1 is:
Warning
Never connect the two poles of a power supply with anything of low resistance value (i.e. a metal object or bare wire) this is a Short and results in high current that may damage the power supply and electronic components.
Note
Unlike LEDs and Diodes, Resistors have no poles and re non-polar (it does not matter which direction you insert them into a circuit, it will work the same)
1.1.5.3. Breadboard
Here we have a small breadboard as an example of how the rows of holes (sockets) are electrically attached. The left picture shows the ways the pins have shared electrical connection and the right picture shows the actual internal metal, which connect these rows electrically.
1.1.5.4. GPIO Extension Board
GPIO board is a convenient way to connect the RPi I/O ports to the breadboard directly. The GPIO pin sequence on Extension Board is identical to the GPIO pin sequence of RPi.
1.1.6. Code
According to the circuit, when the GPIO17 of RPi output level is high, the LED turns ON. Conversely, when the GPIO17 RPi output level is low, the LED turns OFF. Therefore, we can let GPIO17 cycle output high and output low level to make the LED blink. We will use Python code to achieve the target.
1.1.6.1. Python Code Blink
Now, we will use Python language to make a LED blink.
First, observe the project result, and then learn about the code in detail.
Hint
If you have any concerns, please contact us via: support@freenove.com
Use cd command to enter 01.1.1_Blink directory of Python code.
$ cd ~/Freenove_Kit/Code/Python_GPIOZero_Code/01.1.1_Blink
Use python command to execute python code blink.py.
$ python Blink.py
The LED starts blinking.
You can press “Ctrl+C” to end the program. The following is the program code:
1#!/usr/bin/env python3
2########################################################################
3# Filename : Blink.py
4# Description : Basic usage of GPIO. Let led blink.
5# auther : www.freenove.com
6# modification: 2023/05/11
7########################################################################
8from gpiozero import LED
9from time import sleep
10
11led = LED(17) # define LED pin according to BCM Numbering
12#led = LED("J8:11") # BOARD Numbering
13'''
14# pins numbering, the following lines are all equivalent
15led = LED(17) # BCM
16led = LED("GPIO17") # BCM
17led = LED("BCM17") # BCM
18led = LED("BOARD11") # BOARD
19led = LED("WPI0") # WiringPi
20led = LED("J8:11") # BOARD
21'''
22def loop():
23 while True:
24 led.on() # turn on LED
25 print ('led turned on >>>') # print message on terminal
26 sleep(1) # wait 1 second
27 led.off() # turn off LED
28 print ('led turned off <<<') # print message on terminal
29 sleep(1) # wait 1 second
30
31if __name__ == '__main__': # Program entrance
32 print ('Program is starting ... \n')
33 try:
34 loop()
35 except KeyboardInterrupt: # Press ctrl-c to end the program.
36 print("Ending program")
Import the LED class from the gpiozero library.
1from gpiozero import LED
Create an LED assembly for controlling the LED.
1led = LED(17) # define LED pin according to BCM Numbering
Turn on LED device.
1led.on() # turn on LED
Turn off LED devices.
1led.off() # turn off LED
The main function turns on the LED for one second and then turns it off for one second, which repeats endless.
1def loop():
2 while True:
3 led.on() # turn on LED
4 print ('led turned on >>>') # print message on terminal
5 sleep(1) # wait 1 second
6 led.off() # turn off LED
7 print ('led turned off <<<') # print message on terminal
8 sleep(1) # wait 1 second
1.1.6.2. Reference
About GPIO Zero:
- GPIO Zero
A simple interface to GPIO devices with Raspberry Pi, Using the GPIO Zero library makes it easy to get started with controlling GPIO devices with Python. The library is comprehensively documented at
For more information about the methods used by the LED class in the GPIO Zero library,please refer to: https://gpiozero.readthedocs.io/en/stable/api_output.html#led
For more information about the methods used by the DigitalOutputDevice class in the GPIO Zero library,please refer to: https://gpiozero.readthedocs.io/en/stable/api_output.html#digitaloutputdevice
“import time” time is a module of python. https://docs.python.org/2/library/time.html?highlight=time%20time#module-time
In Python, libraries and functions used in a script must be imported by name at the top of the file, with the exception of the functions built into Python by default.
For example, to use the LED interface from GPIO Zero, it should be explicitly imported:
1from gpiozero import LED
1led = LED(17) # define LED pin according to BCM Numbering
2#led = LED("J8:11") # BOARD Numbering
Alternatively, the whole GPIO Zero library can be imported:
1import gpiozero
In this case, all references to items within GPIO Zero must be prefixed:
1led = gpiozero.LED(17) # define LED pin according to BCM Numbering
2#led = gpiozero.LED("J8:11") # BOARD Numbering
Pin Numbering
This library uses Broadcom (BCM) pin numbering for the GPIO pins, as opposed to physical (BOARD) numbering. Unlike in the RPi.GPIO library, this is not configurable. However, translation from other schemes can be used by providing prefixes to pin numbers (see below).
Any pin marked “GPIO” in the diagram below can be used as a pin number. For example, if an LED was attached to “GPIO17” you would specify the pin number as 17 rather than 11:
If you wish to use physical (BOARD) numbering you can specify the pin number as “BOARD11”. If you are familiar with the wiringPi pin numbers (another physical layout) you could use “WPI0” instead.
Finally, you can specify pins as “header:number”, e.g. “J8:11” meaning physical pin 11 on header J8 (the GPIO header on modern Pis). Hence, the following lines are all equivalent:
1led = LED(17)
2led = LED("GPIO17")
3led = LED("BCM17")
4led = LED("BOARD11")
5led = LED("WPI0")
6led = LED("J8:11")
Note
Note that these alternate schemes are merely translations. If you request the state of a device on thecommand line, the associated pin number will always be reported in the Broadcom (BCM) scheme:
1 led = LED("BOARD11")
2 led
3<gpiozero.LED object on pin GPIO17, active_high=True, is_active=False>
In this tutorial, we will use the default integer pin number in the Broadcom (BCM) layout.
GPIO Numbering Relationship
WingOi |
BCM(Extension) |
Physical |
Physical |
BCM(Extension) |
WingPi |
|---|---|---|---|---|---|
3.3V |
3.3V |
1 |
2 |
5V |
5V |
8 |
GPIO2/SDA1 |
3 |
4 |
5V |
5V |
9 |
GPIO3/SCL1 |
5 |
6 |
GND |
GND |
7 |
GPIO4 |
7 |
8 |
GPIO14/TXD0 |
15 |
GND |
GND |
9 |
10 |
GPIO15/RXD0 |
16 |
0 |
GPIO17 |
11 |
12 |
GPIO18 |
1 |
2 |
GPIO27 |
13 |
14 |
GND |
GND |
3 |
GPIO22 |
15 |
16 |
GPIO23 |
4 |
3.3V |
3.3V |
17 |
18 |
GPIO24 |
5 |
12 |
GPIO10/MOSI |
19 |
20 |
GND |
GND |
13 |
GPIO9/MOIS |
21 |
22 |
GPIO25 |
6 |
14 |
GPIO11/SCLK |
23 |
24 |
GPIO8/CE0 |
10 |
GND |
GND |
25 |
26 |
GPIO7/CE1 |
11 |
30 |
GPIO0/SDA0 |
27 |
28 |
BGPIO1/SCL0 |
31 |
21 |
GPIO5 |
29 |
30 |
GND |
GND |
22 |
GPIO6 |
31 |
32 |
GPIO12 |
26 |
23 |
GPIO13 |
33 |
34 |
GND |
GND |
24 |
GPIO19 |
35 |
36 |
GPIO16 |
27 |
25 |
GPIO26 |
37 |
38 |
GPIO20 |
28 |
GND |
GND |
39 |
40 |
GPIO21 |
29 |
In loop(), there is a while loop, which is an endless loop (a while loop). That is, the program will always be executed in this loop, unless it is ended because of external factors. In this loop, set LED output high level, then the LED turns ON. After a period of time delay, set LED output low level, then the LED turns OFF, which is followed by a delay. Repeat the loop, then LED will start blinking.
1def loop():
2 while True:
3 led.on() # turn on LED
4 print ('led turned on >>>') # print message on terminal
5 sleep(1) # wait 1 second
6 led.off() # turn off LED
7 print ('led turned off <<<') # print message on terminal
8 sleep(1) # wait 1 second
In gpiozero, at the end of your script, cleanup is run automatically, restoring your GPIO pins to the state they were found.To explicitly close a connection to a pin, you can manually call the close() method on a device object:
1 led = LED(17)
2 led.on()
3 led
4<gpiozero.LED object on pin GPIO17, active_high=True, is_active=True>
5 led.close()
6 led
7<gpiozero.LED object closed>
This means that you can reuse the pin for another device, and that despite turning the LED on (and hence, the pin high), after calling close() it is restored to its previous state (LED off, pin low).
In this tutorial, most projects have added an active run cleanup program to restore the GPIO pin to the found default state.
1.1.7. Freenove Car, Robot and other products for Raspberry Pi
We also have car and robot kits for Raspberry Pi. You can visit our website for details.
https://www.amazon.com/freenove
FNK0043–Freenove 4WD Smart Car Kit for Raspberry Pi
FNK0050–Freenove Robot Dog Kit for Raspberry Pi
FNK0052–Freenove_Big_Hexapod_Robot_Kit_for_Raspberry_Pi





