- Rokit Smart Tutorials Home
- Programming tutorials
- All Components
- Programming the RadioShack Ultrasonic Range Sensor
- Programming the Top IR Sensor
- Programming the Top IR Sensor
- Programming the PIR Sensor
- Programming the Bottom IR Sensor
- Programming the Sumo Robot with the Ultrasonic Sensor
- Programming the Sumo Robot with the Ultrasonic Sensor
- Programming Serial Read old
- Programming the Buzzer
- Programming the Remote Control
- Programming the DC Motors
- Programming the LEDs
- Programming the Serial Monitor
- Basics of Programming
- Programming Robots
- All Components
- Building tutorials
- Competition tutorials
How to setup the PIR Sensor
This lesson will demonstrate how to use the passive Infrared sensor PIR sensor (Please note: this item is not include in the kit and is a tutorial on how to program it should you purchase one)
Wiring setup
You will need three female to female jumper cables and of course the PIR sensor.
The red wire is connected to VCC or 5V depending on what PIR sensor you have gotten. The orange wire in this case is connected to the signal pin which is connected to the smart inventor board on port D pin 2. The yellow wire is connected to the ground “GND” pin.
How to program the PIR sensor
Programming the PIR sensor
First, begin the code by including the smart inventor library.
Enter the following before the [crayon-670b11fe32a4b247006880-i/] method.
[crayon-670b11fe32d0a970857593/]
Learn More: What’s a library?
A library is a saved set of [crayon-670b11fe32d13331634169-i/] and variables that provide users with improved functionality when coding.
Any code that starts with [crayon-670b11fe32d14872112188-i/] is a function in the SmartInventor library. Any variable that highlights ORANGE, is an initialized variable from an included library.
Second we will assign the pin number the PIR sensor will be connected to.
[crayon-670b11fe32d15404824125/]
Now we will create the boolean variable, [crayon-670b11fe32d16712008203-i/] it will either be [crayon-670b11fe32d17585440404-i/] or [crayon-670b11fe32d18484023087-i/] .
[crayon-670b11fe32d1a659042338/]
Next, inside [crayon-670b11fe32d1b388076502-i/] , enter the following.
[crayon-670b11fe32d1c059199210/]
This line of code will allow the SmartInventor Board to send data back to the computer at a rate of 9600 bits per second.
Serial.begin( )
Serial.Begin( BAUD rate);
This function will establish a high transfer BAUD connection between the remote and the board. The higher the BAUD number the faster the board will process information, but may yield a high chance for errors. The lower the BAUD number the slower the board will process information, but may yield a low chance for errors.
Acceptable BAUD rates range from 300 to 250000. Make sure that the BAUD rate is a number that is found within the lower right corner of the Serial Monitor.
Next, use pinMode() to assign the PIR Pin
[crayon-670b11fe32d1e836533451/]
Next, inside [crayon-670b11fe32d1f178285875-i/] , we will now set the bodyPresent boolean variable.
[crayon-670b11fe32d20092475097/]
The [crayon-670b11fe32d21077645924-i/] reads the pin value which is either HIGH or LOW.
The PIR sensor is a passive infrared sensor it detects infrared heat which activates the sensors circuit. The sensor is actually underneath the white lens which helps redirect infrared to the sensor.
Next, type the following.
[crayon-670b11fe32d22390976641/]
This if statement will check if [crayon-670b11fe32d23036458385-i/] is TRUE or FALSE and will print to the serial monitor and buzz. If the boolean statement is not true then print out that no movement is detected.
[crayon-670b11fe32d24428013807/]
and you are done coding.
Activity
Make the buzzer increase its tone every time the PIR sensor is activated.
Final Code
[crayon-670b11fe32d25851329290/]
Viewing the Serial Monitor
Once you have uploaded your program, keep the upload cable connected and turn off all dip switches. Now click on the the serial monitor eyeglass icon on the top right.
Make sure you have the baud rate selected at 9600.