PIR Motion Sensor (Digital Input)
by circuits in Circuits > Arduino
3700 Views, 6 Favorites, 0 Comments
PIR Motion Sensor (Digital Input)
![Screen Shot 2018-07-20 at 6.31.55 PM.png](/proxy/?url=https://content.instructables.com/FV8/QZCD/JJT6O35I/FV8QZCDJJT6O35I.png&filename=Screen Shot 2018-07-20 at 6.31.55 PM.png)
Let's learn to sense movement in a room with a PIR motion sensor and Arduino. We'll connect up a circuit using a breadboard and use some simple Arduino code to control three LEDs.
So far you've already learned to read a pushbutton with Arduino's digital input, so we'll build on those skills in this lesson. Although the motion sensor may seem complex with its dedicated circuit board, it is configured to send a HIGH or LOW signal in much the same way a pushbutton would.
PIR stands for Passive InfraRed, which describes the technology inside—it passively detects infrared light levels (unlike an infrared camera that may also emit infrared light in order to capture its reflection). The white dome is a lens that expands the IR detector's field of vision. The sensor reports a LOW signal by default, reads the amount of ambient infrared light coming in, and then triggers a HIGH signal for a certain period of time when the light levels change, indicating movement. It can tell you whether or not there is movement in a scene, but cannot detect distance— for that you might consider a type of analog input sensor called an ultrasonic rangefinder.
- Explore the sample circuit here in the workplane by starting the simulation and clicking on the motion sensor. This will activate a highlighted area in front of the sensor with a circle "object" inside. You may need to resize the view if the circle is off screen.
- Click and drag the "object" circle in front of the sensor to represent movement.
- In this lesson, you'll build this simulated circuit yourself along side the sample. To optionally build the physical circuit, gather up your Arduino Uno board, USB cable, solderless breadboard, an LED, resistor (any value from 100-1K), PIR motion sensor, and breadboard wires.
- Continue to the next step.
Build the Circuit
![Copy of PIR Motion Sensor (Blocks).png](/proxy/?url=https://content.instructables.com/F9P/KZ46/JIEU106O/F9PKZ46JIEU106O.png&filename=Copy of PIR Motion Sensor (Blocks).png)
Take a look at the breadboard circuit in the workplane. It can be useful to look at a free-wired version of this sample circuit for comparison, pictured. In this step, you will build your own version of this circuit along side the sample in the workplane.
- Identify the PIR motion sensor, LED, resistor, and wires connected to the Arduino in the Tinkercad Circuits workplane.
- Drag an Arduino Uno and breadboard from the components panel to the workplane, next to the existing circuit.
- Connect breadboard power (+) and ground (-) rails to Arduino 5V and ground (GND), respectively, by clicking to create wires.
- Extend power and ground rails to their respective buses on the opposite edge of the breadboard by creating a red wire between both power buses and a black wire between both ground buses.
- Plug the LED into two different breadboard rows so that the cathode (negative, shorter leg) connects to one leg of a resistor (anywhere from 100-1K ohms is fine).
HINT:
The resistor can go in either orientation because resistors aren't polarized, unlike LEDs, which must be connected in a certain way to function.
- Connect other resistor leg to ground.
- Wire up the LED anode (positive, longer leg) to Arduino pin 13.
- Drag a PIR motion sensor from the components panel to your breadboard, so its legs plug into three different rows.
- Click to create a wire connecting the rightmost leg to power.
- Connect the center leg to ground.
- Create a wire connecting the leftmost leg to Arduino analog pin A0.
- Continue to the next step.
Code With Blocks
![Screen Shot 2018-06-15 at 12.50.05 PM.png](/proxy/?url=https://content.instructables.com/F54/144V/JIEU18EY/F54144VJIEU18EY.png&filename=Screen Shot 2018-06-15 at 12.50.05 PM.png)
Let's use the Blocks coding interface to listen to the PIR motion sensor, then make a decision to light up an LED based on the sensor's state: activated or not activated.
- Click the "Code" button to open the code editor.
- Click on the Variables category in the code editor. Create a new variable called
sensorState
. - Drag out a "set" block.
- We’ll store the state of our PIR motion sensor to our variable
sensorState
. Click on the Input block category, drag out the the “read digital pin” block, and place it into the “set” block after the word “to”. - Since our sensor is connected to the Arduino on Pin 2, change the dropdown of the “read digital pin” block to 2. Now your blocks should read “set sensorState to read digital pin 2” which stores a digital reading of the sensor pin into our sensorState variable!
- Click the Control category and drag out an "if then" block.
- Configure it to evaluate whether
sensorState
is equal toHIGH
using a Math comparator block. Drag out the Math comparator block into your if statement to check whether our variablesensorState
is equal toHIGH
. - We want to turn our LED on if the sensor is activated - otherwise, we want our LED to be off. Under the Output block category, find the “set built-in LED to HIGH” block. Try adding two of these blocks to our if statement so that the LED will only be on when the sensor is activated.
HINT:
set built-in LED should be HIGH when the sensor state is
HIGH
- otherwise, set built-in LED should beLOW
. - Continue to the next step.
PIR Motion Sensor Arduino Code Explained
When the code editor is open, you can click the dropdown menu on the left and select "Blocks + Text" to reveal the Arduino code generated by the code blocks. Follow along as we explore the code in more detail.
-
int sensorState = 0;
Before the
setup()
, we create a variable to store the current state of the sensor. It’s calledint
because it’s an integer, or any whole number (although we will only be using values 0 and 1, LOW and HIGH).void setup() { pinMode(2, INPUT); pinMode(13, OUTPUT); Serial.begin(9600); }
Inside the setup, pins are configured using the
pinMode()
function. Pin 2 is configured as an input, so we can "listen" to the electrical state of the sensor. Pin 13 is configured as an output to control the LED. To be able to send messages, the Arduino opens a new serial communication channel withSerial.begin()
, which takes a baud rate argument (what speed to communicate), in this case 9600 bits per second. -
void loop() { // read the state of the sensor/digital input sensorState = digitalRead(2);
Anything after a set of slashes
//
is a comment, just for us humans to read, and is not included in the program when the Arduino runs it. In the main loop, a function calleddigitalRead();
checks the state of pin 2 (which will be either 5V aka HIGH or ground aka LOW), and stores that state in thesensorState
variable we created at the top.// check if sensor pin is HIGH. if it is, set the // LED on. if (sensorState == HIGH) { digitalWrite(13, HIGH); Serial.println("Sensor activated!"); } else { digitalWrite(13, LOW); } delay(10); // Delay a little bit to improve simulation performance }
Below two more comment rows is an if statement that checks to see if
sensorState
is HIGH (==
is a comparison operator, not to be confused with=
, which is an assignment operator). If the condition is met, the built-in LED is set HIGH (on). If not, the code contained inside theelse {
is executed instead: the built-in LED is set LOW (off). If statements can exist alone, or with one or more else statements. - Continue to the next step.
PIR Motion Sensor Setup
![PIR-motion-sensor-optimized-01.jpg](/proxy/?url=https://content.instructables.com/FDM/BDO7/JI4U7D7C/FDMBDO7JI4U7D7C.jpg&filename=PIR-motion-sensor-optimized-01.jpg)
![PIR-motion-sensor-optimized-00.jpg](/proxy/?url=https://content.instructables.com/FPH/I61D/JI4U7D7B/FPHI61DJI4U7D7B.jpg&filename=PIR-motion-sensor-optimized-00.jpg)
![PIR-motion-sensor-optimized-03.jpg](/proxy/?url=https://content.instructables.com/F90/PMXH/JI4U7D7X/F90PMXHJI4U7D7X.jpg&filename=PIR-motion-sensor-optimized-03.jpg)
If you're building a physical circuit, you'll need to do a little setup with your PIR motion sensor.
- Identify the row of three headers on the circuit board. They will be towards the center of one edge and are labeled GND, OUT, and +5v (or something similar).
- Plug in the included wired connector to the three header pins, with the black wire lined up with GND.
- Double check the connector is firmly seated.
- Alternatively, you can connect three individual female-to-male prototyping wires to the header pins.
- Continue to the next step.
Build a Physical Arduino Circuit (Optional)
![PIR-motion-sensor-optimized-07.jpg](/proxy/?url=https://content.instructables.com/FOT/FQYJ/JI4U7D8V/FOTFQYJJI4U7D8V.jpg&filename=PIR-motion-sensor-optimized-07.jpg)
To program your physical Arduino Uno, you'll need to install the free software (or plugin for the web editor), then open it up.
- Wire up the Arduino Uno circuit by plugging in components and wires to match the connections shown here in Tinkercad Circuits. For a more in-depth walk-through on working with your physical Arduino Uno board, check out the free Instructables Arduino class.
- Copy the code from the Tinkercad Circuits code window and paste it into an empty sketch in your Arduino software, or click the download button (downward facing arrow) and open the resulting file using Arduino. You can also find this example in the Arduino software by navigating to File -> Examples -> 02.Digital -> Button (with a different variable name but it's otherwise the same).
- Plug in your USB cable and select your board and port in the software’s Tools menu.
- Upload the code and watch your LED light up when you move in front of the sensor!
- Continue to the next step.
PIR Motion Sensor Adjustments
![PIR-motion-sensor-optimized-09.jpg](/proxy/?url=https://content.instructables.com/FDA/EDYC/JI4U7D90/FDAEDYCJI4U7D90.jpg&filename=PIR-motion-sensor-optimized-09.jpg)
Some PIR motion sensors come with two adjustable potentiometers for changing the sensitivity and the duration of the activation signal. The PIR motion sensor here in Tinkercad Circuits does not simulate these adjustments.
- Optionally use a small screwdriver to adjust the sensitivity and time dials on the circuit board side of your PIR motion sensor. Experiment to see the effect on the circuit's behavior.
- Continue to the next step.
Next, Try...
![PIR-motion-sensor-optimized-06.jpg](/proxy/?url=https://content.instructables.com/FBL/1L3G/JI4U7D81/FBL1L3GJI4U7D81.jpg&filename=PIR-motion-sensor-optimized-06.jpg)
Now that you’ve learned to detect a PIR motion sensor's signal and use if statements to evaluate its state, you're ready practice more coding and even build your sensor into a finished project.
- Can you replace the LED with a servo motor, and code up a program to wave the servo when the sensor is triggered?
- Try the 3D printing side of Tinkercad to build an electronics enclosure with an opening for your PIR motion sensor.
- Try swapping out your PIR motion sensor for other digital inputs such as a pushbutton or tilt switch.
- Learn more about how to monitor your Arduino's digital and analog inputs through the computer using the Serial Monitor.