1. Chapter LED
We will still start from Blink LED in this chapter, and also learn the usage of some commonly used functions of Processing Software.
1.1. Project Blink
In this project, we will use RPi to control blinking a common LED.
1.1.1. Component List
Raspberry Pi x 1 |
||
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. Circuit
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
CAUTION: Avoid any possible short circuits (especially connecting 5V or GND, 3.3V and GND)!
Warning
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
Because the numbering of the GPIO Extension Shield is the same as that of the RPi GPIO, future hardware connection diagrams will only show that part of breadboard and GPIO Extension Shield.
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 1 red ring is 220Ω
The one with 1 red ring is 1KΩ
Note
Future hardware connection diagrams will only show that part of breadboard and GPIO Extension Shield.
1.1.3. Component knowledge
1.1.3.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.3.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.3.3. Resistor
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.3.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.4. Sketch
1.1.4.1. Sketch Blink
Because the resource folder name is too long, for convenience, the folder will be named as “Freenove_Kit”.
If you have already renamed it, skip this command. Assume the absolute path is “/ home / pi” or “~ /”, execute the following command in the user directory.
$ mv Freenove_Ultimate_Starter_Kit_for_Raspberry_Pi/ Freenove_Kit/
First, observe the result after running the sketch, and then learn about the code in detail.
Use Processing to open the file Sketch_01_1_1_Blink. (The following is only one line of command. There is a Space after Processing.)
$ processing ~/Freenove_Kit/Processing/Sketches/Sketch_01_1_1_Blink/Sketch_01_1_1_Blink.pde
Before using this command, please set the command, otherwise Processing cannot be opened.
Click on “RUN” to run the code.
You can also open it as follows.
Click Raspberry Pi file manager. Find the file under path:
/home/pi/Freenove_Kit/Processing/Sketches/Sketch_01_1_1_Blink
And then right-click it and select Processing.
After the program is executed, LED will start Blinking and the background of Display window will change with the change of LED state.
The following is program code:
1/*****************************************************
2 * Filename : Sketch_01_1_1_Blink
3 * Description : Make an led blinking.
4 * auther : www.freenove.com
5 * modification: 2024/09/04
6 *****************************************************/
7import freenove.processing.io.*;
8
9int ledPin = 17; //define ledPin
10boolean ledState = false; //define ledState
11
12void setup() {
13 size(100, 100);
14 frameRate(1); //set frame rate
15 GPIO.pinMode(ledPin, GPIO.OUTPUT); //set the ledPin to output mode
16}
17
18void draw() {
19 ledState = !ledState;
20 if (ledState) {
21 GPIO.digitalWrite(ledPin, GPIO.HIGH); //led on
22 background(255, 0, 0); //set the fill color of led on
23 } else {
24 GPIO.digitalWrite(ledPin, GPIO.LOW); //led off
25 background(102); //set the fill color of led off
26 }
27}
Processing code usually have two functions: setup() and draw(), where the function setup() is only executed once while the function draw() will be executed repeatedly. In the function setup(), size(100, 100) specifies the size of the Display Window to 100x100pixl. FrameRate(1) specifies the refresh rate of Display Window to once per second, which means the draw() function will be executed once per second. GPIO.pinMode (ledPin, GPIO.OUTPUT) is used to set ledPin to output mode.
1void setup() {
2 size(100, 100);
3 frameRate(1); //set frame rate
4 GPIO.pinMode(ledPin, GPIO.OUTPUT); //set the ledPin to output mode
5}
In draw() function, each execution will invert the variable “ledState”. When “ledState” is true, LED is turned ON, and the background color of display window is set to red. And when the “ledState” is false, the LED is turned OFF and the background color of display window is set to gray. Since the function draw() is executed once per second, the background color of Display Window and the state of LED will also change once per second. This process will repeat in an endless loop to achieve the effect of blinking.
1void draw() {
2 ledState = !ledState;
3 if (ledState) {
4 GPIO.digitalWrite(ledPin, GPIO.HIGH); //led on
5 background(255, 0, 0); //set the fill color of led on
6 } else {
7 GPIO.digitalWrite(ledPin, GPIO.LOW); //led off
8 background(102); //set the fill color of led off
9 }
10}
The following is brief descriptions of some functions:
- setup()
The setup() function is run once when the program starts.
- draw()
It is called directly after the setup() function. The draw() function continuously executes the lines of code within its block until the program stops or noLoop() is called. draw() is called automatically and should never be called explicitly.
- size()
Defines width and height of the display window in pixels.
- framerate()
Specifies the number of frames to be displayed every second.
- background()
Set the color of the background of the display window.
- GPIO.pinMode()
Configures a pin to act either as input or output.
- GPIO.digitalWrite()
Sets an output pin to be either high or low.
All functions used in this code can be found in the Reference of Processing Software, in which built-in functions are described in details, and there are some sample programs. It is recommended that beginners learn more about usage and function of those functions. The localization of Reference can be opened with the following steps: click the menu bar “Help”->”Reference”.
Then the following page will be displayed in the web browser:
Or you can directly access to the official website for reference:http://processing.org/reference/
1.2. Project MouseLED
In this project, we will use the mouse to control the state of LED.
The components and circuits of this project are the same as the previous section.
1.2.1. Sketch
1.2.1.1. Sketch MouseLED
First, observe the result after running the sketch, and then learn the code in detail.
Use Processing to open the file Sketch_01_2_1_MouseLED.
$ processing ~/Freenove_Kit/Processing/Sketches/Sketch_01_2_1_MouseLED/Sketch_01_2_1_MouseLED.pde
Click on “RUN” to run the code.
After the program is executed, the LED is in OFF-state, and background color of Display window is gray. Click the grey area of the Display Window with the mouse, LED is turned ON and Display window background color becomes red. Click on the Display Window again, the LED is turned OFF and the background color becomes gray, as shown below.
The following is program code:
1/*****************************************************
2 * Filename : Sketch_01_2_1_MouseLED
3 * Description : Use the mouse to control the LED ON OFF
4 * auther : www.freenove.com
5 * modification: 2024/09/04
6 *****************************************************/
7import freenove.processing.io.*;
8
9int ledPin = 17;
10boolean ledState = false;
11void setup() {
12 size(100, 100);
13 GPIO.pinMode(ledPin, GPIO.OUTPUT);
14 background(102);
15}
16
17void draw() {
18 if (ledState) {
19 GPIO.digitalWrite(ledPin, GPIO.HIGH);
20 background(255,0,0);
21 } else {
22 GPIO.digitalWrite(ledPin, GPIO.LOW);
23 background(102);
24 }
25}
26
27void mouseClicked() { //if the mouse Clicked
28 ledState = !ledState; //Change the led State
29}
The function mouseClicked() in this code is used to capture the mouse click events. Once the mouse is clicked, the function will be executed. We can change the state of the variable “ledState” in this function to realize controlling LED by clicking on the mouse.
1void mouseClicked() { //if the mouse Clicked
2 ledState = !ledState; //Change the led State
3}
1.2.2. 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





