Maker Advent Calendar Day #7: Monitoring Motion!

Maker Advent Calendar Day #7: Monitoring Motion!

Welcome to day seven of your 12 Projects of Codemas Advent Calendar. Today we're going to be using a sensor that detects motion, allowing our Pico to react to things like people and animals when they are near our project.

This is a digital sensor, giving us a HIGH signal if movement is detected. We can code this in a similar way to the buttons from day #3 as a trigger for our program.

Let's go!

Box #7 Contents

In this box you will find:

  • 1x Mini PIR Sensor
  • 3x Male to male jumper wires

Day 7 parts

Today’s Project

The sensor in your kit is a PIR sensor, which stands for Passive Infra-Red. PIR sensors detect movement via the heat energy (Infra-Red radiation) given off by humans or animals. It won't work on snowmen!

These sensors are commonly used in home alarm systems - if your home has an alarm, have a look in the corners of the ceilings and you may see something similar.

Today we're going to be setting up our motion sensor then testing it by moving our bodies. Then, similar to some of the previous boxes, we'll combine this with other components to create fun little projects including our own little alarm system with a new function to learn!

Construct the Circuit

You know how this starts...unplug your Pico from the USB port on your computer.

Now remove the light sensor parts from yesterday, leaving the LEDs and buzzer in place with the Pico on the main breadboard. Your circuit should look like this to start with:

Day 7 starting point

Now to add your motion sensor. Just to note, the diagrams below also use a generic motion sensor part from Fritzing as there is currently no part for the mini version in your box. The wiring is the same.

Please hold the sensor at the edges of the green PCB when fitting as it is a delicate component!

Important! Sensor Orientation!

The orientation of the PIR pins is very important! Please see the image below with the orientation of the angled pins showing you which side is 3.3V and which is GND (the middle pin is for our GPIO pin):

PIR orientation

Now fit your PIR to the top of the mini breadboard in the orientation shown in the image above (with the 3.3V pin to the left) as shown below:

PIR in mini breadboard

Now connect the three pins to your circuit using the jumper wires:

  • Connect the left pin to the 3.3V pin (physical pin 36)
  • Connect the right pin to the blue GND lane
  • Connect the middle pin to GPIO 26 (physical pin 31)

Your circuit should look like this.

PIR circuit complete

Activity 1: Basic Motion Sensing

Let's get this sensor up and running with a minimal starter program. The code example below uses things we've covered in previous boxes so you should be quite comfortable with how it works.

The Code

As always we add our imports, then set up the GPIO pin for the sensor.

We're using a pull down here to ensure the sensor is LOW unless triggered, because we don't want that pin floating between the two (as we covered in day #3).

Something different you'll spot next is where we wait for the sensor to settle or 'warm up' before jumping into our while loop.

Warm Up

This is good practice with PIR sensors, especially if you have a project designed to run as soon as the microcontroller turns on. It gives the sensor a chance to baseline its surroundings before trying to detect changes. Some more advanced sensors even perform initial self-testing which can take a minute or two.

We allow 10 seconds which seems to be enough for this sensor.

Next we use a while loop to check for a HIGH signal on the PIR sensor, which then prints "I SEE YOU", waits 5 seconds, then continues the loop again.

Copy it over to Thonny and give it a whirl. We find that if you sit perfectly still, then wave your hands around, you should be able to test the program without having to leave the room! 

# Imports
from machine import Pin
import time
 
# Set up PIR pin with pull down
pir = Pin(26, Pin.IN, Pin.PULL_DOWN)

print("Warming up...")

time.sleep(10) # Delay to allow the sensor to settle

print("Sensor ready!")

while True: # Run forever
    
    time.sleep(0.01) # Delay to stop unnecessary program speed
    
    if pir.value() == 1: # If PIR detects movement
        
        print("I SEE YOU!")
    
        time.sleep(5) # Wait 5 seconds before looking for more movement
        
        print("Sensor active") # Let us know that the sensor is active

Activity 2: Motion Alarm

This is a great project for motion sensors, especially if you have a pesky sibling who keeps "borrowing" things from your room...

We can use our PIR sensor to trigger our buzzer and LEDs, making our unwelcome guests jump and hopefully scare them off!

The Code

The example below adds our LEDs and buzzer back in, so we define those pins again ready to be used.

We also don't want you forgetting how to make functions (which we covered on day #5 with the buzzer), so you'll notice that we've made a function called alarm() which we call whenever we want to trigger the LEDs and buzzer. A function wasn't strictly necessary here, but it's good practice!

The function makes our LEDs and buzzer sound/light exactly 5 times, but we're not using a counter this time - instead we're going to introduce the range function...

Range Function

The range function is really handy when you want to repeat something a certain number of times.

Let's say we wanted to print "Not another function!" twenty times. Well, we could add twenty lines of print, or even run a counter adding +1 each time, but another (nicer/shorter) way to do it is using range - and this is how it would look:

for i in range(20):
    print("Not another function!")

Don't get hung up on why the 'i' is an 'i' - change it to a 't' or a 'v' and see what happens - it's the same outcome! Traditionally 'i' has always been used so most programmers stick with that. Consider that the 'i' means iteration or index and it starts to make more sense..."for every iteration within the range of 20"...make sense?

We didn't introduce this earlier when we used counters instead, as we think the range function can be a little more intimidating to new coders. You can also add values within the brackets to set the range/steps, but we'll stick to the simplest usage for now.

We turn the buzzer volume up before the range function, and we turn it back down afterwards - notice the placement and indentation of these lines, keeping them outside of the range function. We also have a line at the start of our program to make sure the buzzer is always off before we begin.

OK, now that we understand what range does, copy the code below over to Thonny, run it and sit very still or leave the room for 20 seconds. When you come back in, you should set off the alarm!

# Imports
from machine import Pin, PWM
import time

# Set up the LED pins
red = Pin(18, Pin.OUT)
amber = Pin(19, Pin.OUT)
green = Pin(20, Pin.OUT)

# Set up the Buzzer pin as PWM
buzzer = PWM(Pin(13))

# Set PWM duty to 0% at program start
buzzer.duty_u16(0)
 
# Set up PIR pin with pull down
pir = Pin(26, Pin.IN, Pin.PULL_DOWN)

# Warm up/settle PIR sensor
print("Warming up...")
time.sleep(10) # Delay to allow the sensor to settle
print("Sensor ready!")

def alarm(): # Our alarm function
    
    # Set PWM duty (volume up)
    buzzer.duty_u16(10000)

    for i in range(5): # Run this 5 times
                
        buzzer.freq(5000) # Higher pitch
        
        red.value(1) # Red ON
        amber.value(1) # Amber ON
        green.value(1) # Green ON
        
        time.sleep(1)
        
        buzzer.freq(500) # Lower pitch
        
        red.value(0) # Red OFF
        amber.value(0) # Amber OFF
        green.value(0) # Green OFF
        
        time.sleep(1)

    # Set PWM duty (volume off)
    buzzer.duty_u16(0)
        
while True: # Run forever
    
    time.sleep(0.01) # Delay to stop unnecessary program speed
    
    if pir.value() == 1: # If PIR detects movement
        
        print("I SEE YOU!")
    
        alarm() # Call our function
        
        print("Sensor active") # Let us know that the sensor is active again

Day #7 Complete!

That last activity included a lot of detail so we're going to leave it there for today and not overload your fresh coder brains!

The PIR sensor can be so much fun to use and they come in all different shapes and sizes too, you'll be making your own home security system in no time.

So what did we cover on day #7? Today you have:

  • Built a circuit with a PIR sensor
  • Learnt how to use a PIR sensor with MicroPython and the Pico
  • Created a mini alarm system!
  • Learnt about the range function
  • Revisited functions

As always, keep your circuit safe somewhere until tomorrow (don't take anything apart just yet) and we'll see you after breakfast!

Featured Products

12 comments

The Pi Hut

The Pi Hut

@Peter – If you’ve double-checked your wiring and the little adjusters on the back and still no joy, please get in touch via support.thepihut.com and we’ll help with that

@Peter – If you’ve double-checked your wiring and the little adjusters on the back and still no joy, please get in touch via support.thepihut.com and we’ll help with that

Peter

Peter

Hi,
I see I am VERY late to the party but very much enjoying the projects. However, I can’t get the PIR to work, even by leaving the room! I can send a picture of the wiring if it will help.

Hi,
I see I am VERY late to the party but very much enjoying the projects. However, I can’t get the PIR to work, even by leaving the room! I can send a picture of the wiring if it will help.

James Smith

James Smith

The Maker Advent Calendar was awesome on the run up to Christmas and I’ve enjoyed playing with new components. Components are not just for Christmas however, and this PIR sensor is now nestled inside my Bird Nesting Box. It will be the trigger to record 10 seconds of IR video if a small feathered friend comes to investigate. A great way for me to check if I have a lodger.

The Maker Advent Calendar was awesome on the run up to Christmas and I’ve enjoyed playing with new components. Components are not just for Christmas however, and this PIR sensor is now nestled inside my Bird Nesting Box. It will be the trigger to record 10 seconds of IR video if a small feathered friend comes to investigate. A great way for me to check if I have a lodger.

The Pi Hut

The Pi Hut

@Scott If I understand your issue correctly, it could be a slightly out-of-position internal connection on the breadboard. A quick way to test this would be to fit the Pico into the other end of the breadboard and see if the issue persists.

@Scott If I understand your issue correctly, it could be a slightly out-of-position internal connection on the breadboard. A quick way to test this would be to fit the Pico into the other end of the breadboard and see if the issue persists.

Scott

Scott

Finished this day, 7, and in each of the previous days, I have to place the jumper in Pin 39 or 40 in order to get the code to work. All of the instruction call for this jumper to be placed from Pin 38 and over to the “-”, blue, rail. The Pico is flush and I’ve checked the other wires numerous times with all appearing to be correct.
Any ideas?
Thanks. Great project.

Finished this day, 7, and in each of the previous days, I have to place the jumper in Pin 39 or 40 in order to get the code to work. All of the instruction call for this jumper to be placed from Pin 38 and over to the “-”, blue, rail. The Pico is flush and I’ve checked the other wires numerous times with all appearing to be correct.
Any ideas?
Thanks. Great project.

Jonathan

Jonathan

I did electronics as part of my GNVQ back in the day (a long time ago), but while programming was a major part of my job, electronics are something I’ve done little with, so this calendar has been amazing fun and has whetted my appetite for more, (now have a second pico, and even dug out my ten-year-old Pi model B).

The PIR sensor is a lot of fun – it’s now a jingle bells alarm using code from the other day, though I’ve expanded the notes to cover more of the song.

I can’t wait to see what tomorrow brings :D

Thank you

I did electronics as part of my GNVQ back in the day (a long time ago), but while programming was a major part of my job, electronics are something I’ve done little with, so this calendar has been amazing fun and has whetted my appetite for more, (now have a second pico, and even dug out my ten-year-old Pi model B).

The PIR sensor is a lot of fun – it’s now a jingle bells alarm using code from the other day, though I’ve expanded the notes to cover more of the song.

I can’t wait to see what tomorrow brings :D

Thank you

Julie

Julie

Hooray. Finally got the motion sensor to work and made an alarm with Xmas music and LEDs. I am having a slight problem with the pico losing contact with the breadboard. I think it is the usb connection slightly weights it down and lifts the end but any other thoughts welcome…

Hooray. Finally got the motion sensor to work and made an alarm with Xmas music and LEDs. I am having a slight problem with the pico losing contact with the breadboard. I think it is the usb connection slightly weights it down and lifts the end but any other thoughts welcome…

The Pi Hut

The Pi Hut

@Virginie – The range varies depending on position, interference, the temperature of the moving ‘thing’ and more – but generally this little version is good to watch a single room. I might not catch you at the other end of a long hallway for example.

We can use the ADC pins just like GPIO pins, they have dual abilities like that. So there’s no issue using them as normal GPIO pins, which is great as you don’t lose those GPIO pins if you’re not using ADC in a project.

In terms of knowing what a sensor needs, it usually starts with looking at datasheets for the component, but these days we “just know” after tinkering with these kinds of things for years. We don’t have any documentation on that as it would cover lots of different things, but there are some good resources/videos out there on how to read datasheets.

@Virginie – The range varies depending on position, interference, the temperature of the moving ‘thing’ and more – but generally this little version is good to watch a single room. I might not catch you at the other end of a long hallway for example.

We can use the ADC pins just like GPIO pins, they have dual abilities like that. So there’s no issue using them as normal GPIO pins, which is great as you don’t lose those GPIO pins if you’re not using ADC in a project.

In terms of knowing what a sensor needs, it usually starts with looking at datasheets for the component, but these days we “just know” after tinkering with these kinds of things for years. We don’t have any documentation on that as it would cover lots of different things, but there are some good resources/videos out there on how to read datasheets.

Virginie

Virginie

Thank you for this amazing advent calendar and projects. You can’t imagine how much joy you give me!
I love that we learn about sensors, GPIO.. PMW was a blast for me…
Now, few questions : what is the distance range of the PIR (motion sensor) to detect us?
it is plugged on a ADC pin but you don’t use ADC function, why ? How do you know which sensor needs what, where to plug them, if they need pull down or setting time and such ? Do you have a documentation on that ? it would be awesome.

My advice @Julie try with another wire, sometimes they are deficient… it happened to me in the past and a multimeter help me understand it. (Hey, that would be a nice thing to do how to use multimeter to debug and check if the material works…)
thanks,
Vi

Thank you for this amazing advent calendar and projects. You can’t imagine how much joy you give me!
I love that we learn about sensors, GPIO.. PMW was a blast for me…
Now, few questions : what is the distance range of the PIR (motion sensor) to detect us?
it is plugged on a ADC pin but you don’t use ADC function, why ? How do you know which sensor needs what, where to plug them, if they need pull down or setting time and such ? Do you have a documentation on that ? it would be awesome.

My advice @Julie try with another wire, sometimes they are deficient… it happened to me in the past and a multimeter help me understand it. (Hey, that would be a nice thing to do how to use multimeter to debug and check if the material works…)
thanks,
Vi

The Pi Hut

The Pi Hut

@Julie – This is usually just a wire in the wrong place somewhere, or the breadboard not quite making contact. Best to remove everything and start again when this happens. If you’re still having trouble, shoot our support guys a message via support.thepihut.com (with a picture if possible) and they’ll check it for you.

@Julie – This is usually just a wire in the wrong place somewhere, or the breadboard not quite making contact. Best to remove everything and start again when this happens. If you’re still having trouble, shoot our support guys a message via support.thepihut.com (with a picture if possible) and they’ll check it for you.

Julie Hunt

Julie Hunt

My missing day three buttons arrived and I had a lot of fun with them. Thank You. Now I am trying to get my PIR sensor to sense anything and it is not working at all. Any tips? It is wired up OK.

My missing day three buttons arrived and I had a lot of fun with them. Thank You. Now I am trying to get my PIR sensor to sense anything and it is not working at all. Any tips? It is wired up OK.

James

James

Friendly reminder to everyone else, measure twice, cut once..
Check your wires are the correct way around for the PIR links.. I managed to cook mine :D smells like hot glue in here

Friendly reminder to everyone else, measure twice, cut once..
Check your wires are the correct way around for the PIR links.. I managed to cook mine :D smells like hot glue in here

Leave a comment

All comments are moderated before being published.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.