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.2.3. WiringPi GPIO Numbering
Different from the previous two types of GPIO serial numbers, RPi GPIO serial number of the WiringPi are numbered according to the BCM chip use in RPi.
See also
For more details, please refer to https://projects.drogon.net/raspberry-pi/wiringpi/pins/
You can also use the following command to view their correlation.
$ gpio readall
1.1.3. 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.4. Component knowledge
1.1.4.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
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.4.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.4.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.4.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.5. 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 both C code to achieve the target.
1.1.5.1. C Code Blink
First, enter this command into the Terminal one line at a time. Then observe the results it brings on your project, and learn about the code in detail.
If you want to execute it with editor, please refer to section Code Editor to configure.
Hint
If you have any concerns, please contact us via: support@freenove.com
It is recommended that to execute the code via command line.
If you did not update wiring pi, please execute following commands one by one.
$ sudo apt-get update
$ git clone https://github.com/WiringPi/WiringPi
$ cd WiringPi
$ ./build
Use
cdcommand to enter 01.1.1_Blink directory of C code.
$ cd ~/Freenove_Kit/Code/C_Code/01.1.1_Blink
Use the following command to compile the code “Blink.c” and generate executable file “Blink”.
$ gcc Blink.c -o Blink -lwiringPi
Then run the generated file “blink”.
$ sudo ./Blink
Now your LED should start blinking! CONGRATUALTIONS! You have successfully completed your first RPi circuit!
You can also use the file browser. On the left of folder tree, right-click the folder you want to enter, and click “Open in Terminal”.
You can press Ctrl+C to end the program. The following is the program code:
1/**********************************************************************
2* Filename : Blink.c
3* Description : Basic usage of GPIO. Let led blink.
4* auther : www.freenove.com
5* modification: 2019/12/26
6**********************************************************************/
7#include <wiringPi.h>
8#include <stdio.h>
9
10#define ledPin 0 //define the led pin number
11
12void main(void)
13{
14 printf("Program is starting ... \n");
15
16 wiringPiSetup(); //Initialize wiringPi.
17
18 pinMode(ledPin, OUTPUT);//Set the pin mode
19 printf("Using pin%d\n",ledPin); //Output information on terminal
20 while(1){
21 digitalWrite(ledPin, HIGH); //Make GPIO output HIGH level
22 printf("led turned on >>>\n"); //Output information on terminal
23 delay(1000); //Wait for 1 second
24 digitalWrite(ledPin, LOW); //Make GPIO output LOW level
25 printf("led turned off <<<\n"); //Output information on terminal
26 delay(1000); //Wait for 1 second
27 }
28}
In the code above, the configuration function for GPIO is shown below as:
-
void pinMode(int pin, int mode);
This sets the mode of a pin to either INPUT, OUTPUT, PWM_OUTPUT or GPIO_CLOCK. Note that only wiringPi pin 1 (BCM_GPIO 18) supports PWM output and only wiringPi pin 7 (BCM_GPIO 4) supports CLOCK output modes.
This function has no effect when in Sys mode. If you need to change the pin mode, then you can do it with the gpio program in a script before you start your program
-
void digitalWrite(int pin, int value);
Writes the value HIGH or LOW (1 or 0) to the given pin, which must have been previously set as an output.
See also
For more related wiringpi functions, please refer to https://github.com/WiringPi/WiringPi
GPIO connected to ledPin in the circuit is GPIO17 and GPIO17 is defined as 0 in the wiringPi numbering. So ledPin should be defined as 0 pin. You can refer to the corresponding table in Chapter 0.
#define ledPin 0 //define the led pin number
GPIO Numbering Relationship
In the main function main(), initialize wiringPi first.
1wiringPiSetup(); //Initialize wiringPi.
After the wiringPi is initialized successfully, you can set the ledPin to output mode and then enter the while loop,
which is an endless loop (a while loop).
That is, the program will always be executed in this cycle,
unless it is ended because of external factors. In this loop,
use digitalWrite (ledPin, HIGH) to make ledPin output high level, then LED turns ON.
After a period of time delay, use digitalWrite(ledPin, LOW) to make ledPin output low level,
then LED turns OFF, which is followed by a delay. Repeat the loop, then LED will start blinking.
1pinMode(ledPin, OUTPUT);//Set the pin mode
2printf("Using pin%d\n",ledPin); //Output information on terminal
3while(1){
4 digitalWrite(ledPin, HIGH); //Make GPIO output HIGH level
5 printf("led turned on >>>\n"); //Output information on terminal
6 delay(1000); //Wait for 1 second
7 digitalWrite(ledPin, LOW); //Make GPIO output LOW level
8 printf("led turned off <<<\n"); //Output information on terminal
9 delay(1000); //Wait for 1 second
10}
1.1.6. Other Code Editors (Optional)
If you want to use other editor to edit and execute the code, you can learn them in this section.
1.1.6.1. nano
Use the nano editor to open the file “Hello.c”, then press “ Ctrl+X “ to exit.
$ nano Hello.c
As is shown below:
Use the following command to compile the code to generate the executable file “Hello”.
$ gcc Hello.c -o Hello
Use the following command to run the executable file “Hello”.
$ sudo ./Hello
After the execution, “Hello, World!” is printed out in terminal.
1.1.6.2. geany
Next, learn to use the Geany editor. Use the following command to open the Geany in the sample file “Hello.c” file directory path.
$ geany Hello.c
Or find and open Geany directly in the desktop main menu, and then click FileOpen to open the “Hello.c”, Or drag “Hello.c” to Geany directly.
If you want to create a new code, click File -> New -> File -> Save as (name.c or name.py). Then write the code.
Generate an executable file by clicking menu bar Build -> Build.
Then execute the generated file by clicking menu bar Build -> Execute.
After the execution, a new terminal window will output the characters “Hello, World!”, as shown below:
You can click Build -> Set Build Commands to set compiler commands. In later projects, we will use various compiler command options. If you choose to use Geany, you will need change the compiler command here. As is shown below:
Here we have identified three code editors: vi, nano and Geany. There are also many other good code editors available to you, and you can choose whichever you prefer to use.
In later projects, we will only use terminal to execute the project code. This way will not modify the code by mistake.
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





