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-6605e2e6b0aa2504962391-i/] method.
[crayon-6605e2e6b0aa8411293648/]

Learn More: What’s a library?

A library is a saved set of [crayon-6605e2e6b0aaa533578048-i/]  and variables that provide users with improved functionality when coding.

Any code that starts with [crayon-6605e2e6b0aab114619460-i/]  is a function in the SmartInventor library.  Any variable that highlights ORANGE, is an initialized variable from an included library.

Learn More

Second we will assign the pin number the PIR sensor will be connected to.
[crayon-6605e2e6b0aac990299438/]
Now we will create the boolean variable, [crayon-6605e2e6b0aad232030256-i/]  it will either be [crayon-6605e2e6b0aae160286182-i/] or [crayon-6605e2e6b0aaf466641552-i/] .
[crayon-6605e2e6b0ab1983753429/]

Next, inside  [crayon-6605e2e6b0ab2507260983-i/] , enter the following.
[crayon-6605e2e6b0ab3252479868/]
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-6605e2e6b0ab4404029697/]

Next, inside  [crayon-6605e2e6b0ab5810263456-i/] , we will now set the bodyPresent boolean variable.
[crayon-6605e2e6b0ab6701415253/]
The [crayon-6605e2e6b0ab7873111439-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-6605e2e6b0ab8352233300/]
This if statement will check if [crayon-6605e2e6b0ab9005692019-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-6605e2e6b0aba523862665/]
and you are done coding.

 

Activity

Make the buzzer increase its tone every time the PIR sensor is activated.

Final Code
[crayon-6605e2e6b0abb253228364/]

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.