PIR Motion Sensor With Arduino in Tinkercad




Introduction: PIR Motion Sensor With Arduino in Tinkercad

About: Learn electronics and Arduino with Tinkercad Circuits!

Let's learn to sense movement in a room with a PIR motion sensor and Arduino's digital input. We'll connect up a circuit using a breadboard and use some simple Arduino code to control a single LED. We'll use Tinkercad Circuits to simulate the circuit so you can follow along even without any components, and show you how to build the physical circuit too.

So far you've likely 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.

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.

You can follow along virtually using Tinkercad Circuits. You can even view this lesson from within Tinkercad (free login required)! Explore the sample circuit and build your own right next to it. Tinkercad Circuits is a free browser-based program that lets you build and simulate circuits. It's perfect for learning, teaching, and prototyping.

Step 1: Build the Circuit

Explore the sample circuit here in the embedded circuit below by starting the simulation and clicking on the round 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. The LED will turn on for a short time when movement is detected.

The free-wired version of this circuit is pictured above. If needed, take a moment to refresh your breadboard knowledge. You could load up a new Tinkercad Circuits window and build your own version of this circuit along side the sample.

Identify the PIR motion sensor, LED, resistor, and wires connected to the Arduino.

Drag an Arduino Uno and breadboard from the components panel to the workplane.

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). 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.

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.

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 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.

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. set built-in LED should be HIGH when the sensor state is HIGH - otherwise, set built-in LED should be LOW.

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.

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);

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.

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.

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.

    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.

    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.

      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!

      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.

        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.

        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.

          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 how to monitor your Arduino's digital and analog inputs through the computer using the Serial Monitor.

          You can also learn more electronics skills with the free Instructables classes on Arduino, Basic Electronics, LEDs & Lighting, 3D Printing, and more.



            • Tiny Home Contest

              Tiny Home Contest
            • Creative Misuse Contest

              Creative Misuse Contest
            • Metalworking Contest

              Metalworking Contest


            Thanks for sharing, I'd like to learn more about Arduino!