EasyManuals Logo

Freenove Ultimate Starter Kit User Manual

Freenove Ultimate Starter Kit
286 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Page #250 background imageLoading...
Page #250 background image
Chapter 23 Infrared Motion Sensor
250
www.freenove.com
support@freenove.com
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#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){

Table of Contents

Questions and Answers:

Question and Answer IconNeed help?

Do you have a question about the Freenove Ultimate Starter Kit and is the answer not in the manual?

Freenove Ultimate Starter Kit Specifications

General IconGeneral
BrandFreenove
ModelUltimate Starter Kit
CategorySingle board computers
LanguageEnglish