Chapter 5 RGBLED
84
Code
Since this project requires 3 PWM, but in RPi, only one GPIO has the hardware capability to output PWM, we
need to use the software to make the ordinary GPIO output PWM.
C Code 5.1.1 ColorfulLED
First observe the project result, and then analyze the code.
1.
Use cd command to enter 05.1.1_ColorfulLED directory of C code.
cd ~/Freenove_Ultimate_Starter_Kit_for_Raspberry_Pi/Code/C_Code/05.1.1_ColorfulLED
2.
Use following command to compile “ColorfulLED.c” and generate executable file “ColorfulLED”. Note: in
this project, the software PWM uses a multi-threading mechanism. So “-lpthread” option need to be add
the compiler.
gcc ColorfulLED.c -o ColorfulLED -lwiringPi -lpthread
3.
And then run the generated by “ColorfulLED”.
sudo ./ColorfulLED
After the program is executed, you will see that the RGBLED shows light of different color randomly.
The following is the program code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <wiringPi.h>
#include <softPwm.h>
#include <stdio.h>
#define ledPinRed 0
#define ledPinGreen 1
#define ledPinBlue 2
void
ledInit
(
void
)
{
softPwmCreate
(
ledPinRed
,
0
,
100
);
softPwmCreate
(
ledPinGreen
,
0
,
100
);
softPwmCreate
(
ledPinBlue
,
0
,
100
);
}
void
ledColorSet
(
int
r_val
,
int
g_val
,
int
b_val
)
{
softPwmWrite
(
ledPinRed
,
r_val
);
softPwmWrite
(
ledPinGreen
,
g_val
);
softPwmWrite
(
ledPinBlue
,
b_val
);
}
int
main
(
void
)
{
int
r
,
g
,
b
;
if
(
wiringPiSetup
()
==
-
1
){
//when initialize wiring failed,print message to screen
printf
(
"setup wiringPi failed !"
);
Summary of Contents for Ultimate Starter Kit
Page 1: ...Free your innovation Freenove is an open source electronics platform www freenove com ...
Page 117: ...117 Chapter 9 Potentiometer RGBLED www freenove com support freenove com Hardware connection ...
Page 155: ...155 Chapter 14 Relay Motor www freenove com support freenove com Hardware connection OFF 3 3V ...
Page 173: ...173 Chapter 16 Stepping Motor www freenove com support freenove com Hardware connection ...
Page 239: ...239 Chapter 22 Matrix Keypad www freenove com support freenove com Circuit Schematic diagram ...
Page 240: ...Chapter 22 Matrix Keypad 240 www freenove com support freenove com Hardware connection ...