Code
In this project, we use infrared motion sensor to control LED, and take infrared motion sensor as a switch, so
the code very similar to front project "Button&LED" in logic. The difference is that, when infrared motion
sensor detects change, it will output high level; when button is pressed, it will output low level. When the
sensor output high level, the LED will be turned on, or it will be turned off.
C Code 23.1.1 SenseLED
First observe the project result, and then analyze the code.
1. Use cd command to enter 23.1.1_SenseLED directory of C code.
cd ~/Freenove_Ultimate_Starter_Kit_for_Raspberry_Pi/Code/C_Code/23.1.1_SenseLED
2. Use following command to compile "SenseLED.c" and generate executable file "SenseLED".
gcc SenseLED.c -o SenseLED -lwiringPi
3. Run the generated file "SenseLED".
sudo ./SenseLED
After the program is executed, try to leave away from or get closed to the Motion Sensor Infrared and observe
whether the LED will be turned on or off. The terminal window will print out the state of LED constantly. As is
shown below:
The following is the program code:
#include <wiringPi.h>
#include <stdio.h>
#define ledPin 1 //define the ledPin
#define sensorPin 0 //define the sensorPin
int main(void)
{
if(wiringPiSetup() == -1){ //when initialize wiring failed, print message to screen
printf("setup wiringPi failed !");
return 1;
}
pinMode(ledPin, OUTPUT);
pinMode(sensorPin, INPUT);
while(1){
if(digitalRead(sensorPin) == HIGH){