Introduction: PIR Motion Sensor (Digital Input)

About: Learn electronics with Tinkercad Circuits!

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.

  1. 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.
  2. Click and drag the "object" circle in front of the sensor to represent movement.
  3. 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.
  4. Continue to the next step.

Step 1: Build the Circuit

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.

  1. Identify the PIR motion sensor, LED, resistor, and wires connected to the Arduino in the Tinkercad Circuits workplane.
  2. Drag an Arduino Uno and breadboard from the components panel to the workplane, next to the existing circuit.
  3. Connect breadboard power (+) and ground (-) rails to Arduino 5V and ground (GND), respectively, by clicking to create wires.
  4. 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.
  5. 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.

  6. Connect other resistor leg to ground.
  7. Wire up the LED anode (positive, longer leg) to Arduino pin 13.
  8. Drag a PIR motion sensor from the components panel to your breadboard, so its legs plug into three different rows.
  9. Click to create a wire connecting the rightmost leg to power.
  10. Connect the center leg to ground.
  11. Create a wire connecting the leftmost leg to Arduino analog pin A0.
  12. Continue to the next step.

Step 2: Code With Blocks

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.

  1. Click the "Code" button to open the code editor.
  2. Click on the Variables category in the code editor. Create a new variable called sensorState.
  3. Drag out a "set" block.
  4. 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”.
  5. 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!
  6. Click the Control category and drag out an "if then" block.
  7. Configure it to evaluate whether sensorState is equal to HIGH using a Math comparator block. Drag out the Math comparator block into your if statement to check whether our variable sensorState is equal to HIGH.
  8. 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 be LOW.

  9. Continue to the next step.

Step 3: 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.

  1. int sensorState = 0;
    

    Before the setup(), we create a variable to store the current state of the sensor. It’s called int 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 with Serial.begin(), which takes a baud rate argument (what speed to communicate), in this case 9600 bits per second.

  2. 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 called digitalRead(); checks the state of pin 2 (which will be either 5V aka HIGH or ground aka LOW), and stores that state in the sensorState 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 the else { is executed instead: the built-in LED is set LOW (off). If statements can exist alone, or with one or more else statements.

  3. Continue to the next step.

Step 4: PIR Motion Sensor Setup

If you're building a physical circuit, you'll need to do a little setup with your PIR motion sensor.

  1. 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).
  2. Plug in the included wired connector to the three header pins, with the black wire lined up with GND.
  3. Double check the connector is firmly seated.
  4. Alternatively, you can connect three individual female-to-male prototyping wires to the header pins.
  5. Continue to the next step.

Step 5: Build a Physical Arduino Circuit (Optional)

To program your physical Arduino Uno, you'll need to install the free software (or plugin for the web editor), then open it up.

  1. 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.
  2. 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).
  3. Plug in your USB cable and select your board and port in the software’s Tools menu.
  4. Upload the code and watch your LED light up when you move in front of the sensor!
  5. Continue to the next step.

Step 6: PIR Motion Sensor Adjustments

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.

  1. 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.
  2. Continue to the next step.

Step 7: Next, Try...

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.

  1. Can you replace the LED with a servo motor, and code up a program to wave the servo when the sensor is triggered?
  2. Try the 3D printing side of Tinkercad to build an electronics enclosure with an opening for your PIR motion sensor.
  3. Try swapping out your PIR motion sensor for other digital inputs such as a pushbutton or tilt switch.
  4. Learn more about how to monitor your Arduino's digital and analog inputs through the computer using the Serial Monitor.