# Step-by-step Python tutorial : Use a pir sensor with your Gamebuino Meta

**URL:** <https://community.gamebuino.com/t/step-by-step-python-tutorial-use-a-pir-sensor-with-your-gamebuino-meta/318>\
**Category:** \[EN\] Programming in Python\
**Created:** [6 September 2021 15:07 UTC](https://community.gamebuino.com/t/step-by-step-python-tutorial-use-a-pir-sensor-with-your-gamebuino-meta/318 "2021-09-06T15:07:42Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tombuino](https://community.gamebuino.com/user_avatar/community.gamebuino.com/tombuino/32/64_2.png) [@Tombuino](https://community.gamebuino.com/u/Tombuino)\
**Post date:** [6 September 2021 15:07 UTC](https://community.gamebuino.com/t/step-by-step-python-tutorial-use-a-pir-sensor-with-your-gamebuino-meta/318/1 "2021-09-06T15:07:42Z")

</div>

another Python tutorial dedicated to the [accessory pack](https://shop.gamebuino.com/en/accessoires/73-pack-accessoires-pour-gamebuino.html).  
This time we will look into the pir sensor. The program will be fairly simple, such as the one made in the C++ tutorial [here](https://community.gamebuino.com/t/step-by-step-tutorial-use-an-infrared-sensor-with-your-gamebuino-meta/240)

![hline_orange](https://community.gamebuino.com/uploads/default/original/1X/c338b375ef4f0c5b6fd0ee77e3b4da6b90ef4e26.png)

### Getting started : Tools requirement

This tutorial is made with Mu IDE for CircuitPython.  
If you haven’t installed those yet please refer to the [installation guide](https://gamebuino.com/en/academy/workshop/discover-python-programming/installation-and-prerequisites) on our website.

![hline_orange](https://community.gamebuino.com/uploads/default/original/1X/c338b375ef4f0c5b6fd0ee77e3b4da6b90ef4e26.png)

## Wiring

 ![pir_pluggin](https://community.gamebuino.com/uploads/default/original/1X/bb312eeff2d8360e072477839747affeffa8067f.jpeg)

 ![dessin_pir](https://community.gamebuino.com/uploads/default/original/1X/805c221ed8193d64c29047672d1dea27c5ed6027.png)

![hline_orange](https://community.gamebuino.com/uploads/default/original/1X/c338b375ef4f0c5b6fd0ee77e3b4da6b90ef4e26.png)

## Step 1 : Program’s basis

Start by opening Mu and creating a new file.

 ![capture_mu](https://community.gamebuino.com/uploads/default/original/1X/1cf1d93b94fdace88fdccad9924f61002e488158.png)

Copy the following code into this file :

```python
from gamebuino_meta import begin, waitForUpdate, display, buttons, color
import board
import digitalio

while True:
    waitForUpdate()
    display.clear()

```

This will import the required Gamebuino libraries into the program, as well as a “digitalio” library needed to work with our sensor.

The part beginning by “while True:” is our main program loop, without it nothing would ever happen.

![hline_orange](https://community.gamebuino.com/uploads/default/original/1X/c338b375ef4f0c5b6fd0ee77e3b4da6b90ef4e26.png)

## Step 2 : Reading from the sensor

We will first configure our sensor input into the console to read the signal it sends.  
For this a couple lines of code will do :

```python
pir_pin = digitalio.DigitalInOut(board.D8)
pir_pin.direction = digitalio.Direction.INPUT

```

We define the pin dedicated to the sensor as the D8 pin, then define this pin as an entry, from which we will be able to read data.

For this reading we will store the information into a variable called “old\_value” such as :

```python
old_value = pir_pin.value

```

This will allow us to have an initial reading at the start of the program that we will use as a reference point.

Once this reference is established the following steps are fairly simple and everything happens in the main loop.  
We add a local variable “pir\_value” in which we store the read value exactly like “old\_value” :

```python
while True:
    waitForUpdate()
    pir_value = pir_pin.value

```

The we just need to compare the two variables to determine whether our sensor picked up something or not.  
This comparison is made in two parts :

```python
while True:
    waitForUpdate()
    pir_value = pir_pin.value
    if (pir_value):
        if not (old_value):
            display.clear()
            display.print('Motion detected!')
    else:
        if (old_value):
            display.clear()
            display.print('No Motion')
    old_value = pir_value

```

In the first part we check if the sensor detects anything, if it does we check whether it was already detecting by checking if old\_value if null, in which case we clear the screen before displaying the message “Motion detected !”

In the second part, having detected nothing we check if old value contains something, if it does it means we no longer detect movement, we then clear the screen before displaying the message “No Motion”.

At the end we replace the content of old\_value by pir\_value to establish continuity within our loop.

![hline_orange](https://community.gamebuino.com/uploads/default/original/1X/c338b375ef4f0c5b6fd0ee77e3b4da6b90ef4e26.png)

Congratulations ! You survived this (very short) Python tutorial.

Your program should look like this :

```python
from gamebuino_meta import begin, waitForUpdate, display, buttons, color
import board
import digitalio

pir_pin = digitalio.DigitalInOut(board.D8)
pir_pin.direction = digitalio.Direction.INPUT

old_value = pir_pin.value

while True:
    waitForUpdate()
# display.clear()
    pir_value = pir_pin.value
    if (pir_value):
        if not (old_value):
            display.clear()
            display.print('Motion detected!')
    else:
        if (old_value):
            display.clear()
            display.print('No Motion')
    old_value = pir_value

```

You can now upload it to your console using CircuitPython, don’t forget to save it locally on your computer to not lose it though.
