PIR motion sensing


Step 1 /6

Introduction

PIR sensors are great for detecting motion and they are easy to use with CodeBug.


Step 2 /6

Setting up digital inputs

To read the value of a PIR sensor, we need to set up CodeBug to read a digital value.

  • First click on the blue cog on the start block and tick the checkbox next to extension pins input/output.

  • Click the cog again to close this window.

  • Select digital input from the leg 6 as dropdown menu on the start block.


Step 3 /6

Looping

  • Now from the Loops menu, drag in a repeat while true loop and snap it below your start block.

This will allow you to read PIR sensor more than once.


Step 4 /6

Sensing movement

  • From the If menu, drag in an if block and snap it inside your repeat while true loop.

  • From the Logic menu, drag in an equals (=) block and snap it to the puzzle piece of your if block.

  • From the Inputs/Outputs menu drag in a get leg/pin block and snap it into the first gap of your equals block.

  • Click on the number block inside your get leg/pin block and change the number to 6.

  • From the Maths menu drag in a number 1 block and snap it into the second gap of your equals block.

This will code will check if the PIR sensor has registered movement.


Step 5 /6

Displaying a message

When motion is sense, we need to display a message

  • From the Sprite menu drag in a scroll sprite block and snap it inside your if block.

  • Again from the Sprite menu drag in a get string sprite block and snap it to the puzzle piece of your scroll sprite block.

  • Click on the Hello world text of your get string sprite block and change it to "Intruder".


Step 6 /6

Download your code

  • Click Download to download your code.

  • Load your code onto your CodeBug.