## Tutorial on how to build a movement notifier
by Daniel Johansson (dj222ev)
In this tutorial you'll learn how to build a movement notifier, sending data to pybytes where you'll visualize your data on a dashboard.
Time estimation: 2-3 h
---
### Objective
I chose this project since I'm a total beginner in the field of IoT and this felt like a good entry level. At first I wanted to use the PIR to trigger audio, but realized quite fast that I had to skip the audio part to be able to finish before deadline.
Original purpose was triggering random audio when movement was detected in a room.
This project has given me lots of insights. I've been brushing up on my knowledge of electronic circuits, searching for and reading part specs, connecting stuff, trial and error until something works, etc. I really hope to find the time after the course to develop the audio part of my initial idea.
---
### Material
| Item | Price | Link | Comment|
| -------- | -------- | -------- | -------- |
| Lopy and sensors bundle | 92,95€ | [Electrokit]() | A bundle containing a Lopy4, an expansion board + some sensors, cables, LEDs etc. |
| Dupont cables (female) | 8,72€ | [Kjell & co](https://www.kjell.com/se/produkter/el-verktyg/elektronik/elektroniklabb/luxorparts-delbar-kopplingskabel-40-pol-hona-hona-p87906) | Bundle didn't include female dupont cables, so had to buys these separate. |
| PIR HC-SR501 | 4,80€ | [Electrokit](https://www.electrokit.com/produkt/pir-rorelsedetektor-hc-sr501/) | This is the motion sensor |
---
### Computer setup
This tutorial is based on using a Lopy4 and an Expansion Board 3.0.
I've been using Atom on a Mac.
#### Steps
1. Connect your Lopy4 to your expansion board, and then to your computer via USB. Follow this [tutorial](https://docs.pycom.io/updatefirmware/device/) to update your firmware.
2. Install node.js on your computer following [this link](https://nodejs.org/en/).
3. Install [Atom](https://flight-manual.atom.io/getting-started/sections/installing-atom/).
4. Setup [Pymakr](https://docs.pycom.io/gettingstarted/software/atom/).
---
### Putting everything together
The wiring is fairly simple.
1. HC-SR501 needs 5V to operate, so make sure to connect your power rail to the pin marked VIN, not the one that says 3V3.
2. Ground to pin marked GND.
3. Signal to P13.

For a thorough pinout diagram of the lopy4, check [this](https://docs.pycom.io/gitbook/assets/lopy4-pinout.pdf).
---
### Platform
I chose to use Pybytes as my platform. Pybytes is free, cloudbased, and felt like the easiest platform to start with. I've glanced at Datacake, but decided to stay on Pybytes for now.
---
### The code
```python=
import pycom
import time
from machine import Pin
from machine import Timer
pycom.heartbeat(False) #Remove blinking LED on lopy4.
pir = Pin('P13',mode=Pin.IN) #Define which pin that recieves signal from the sensor.
chrono = Timer.Chrono() #Instantiate timer.
chrono.start()
print("Detection active!")
while True:
if pir()==1:
print(chrono.read(), "Motion Detected!") #If P13 recieves High, there's motion in the room.
pybytes.send_signal(1, 1)
if pir()==0:
print(chrono.read())
pybytes.send_signal(1, 0)
time.sleep(1.0) #Sleep for a second before checking again.
```
---
### Transmitting the data / connectivity
The Data is sent every second, and it's sent via WiFi. I'm using the function pybytes.send_signal(signal_number, value) to send the data, where signal_number defines which channel (in our case 1) and value defines the message (in our case integer 1 for "motion detected" and 0 for "no motion").
---
### Presenting the data
Detected movement is presented in a bar chart (See below). Data is sent from our Lopy4 to Pybytes every second, and preserved for a month.

---
### Finalizing the design
This is the current state of my movement notifier. I would like to continue getting the audio part of my initial idea incorporated as well. An amplifier and some speakers together with some nice sounds would make this a bit more exciting than it is right now. Still, happy that I got it to work while learning a lot of new things along the way. Thanks for a great course!
