## Tutorial on how to build vibration detector
###### By Robin Larsson (rl222jk)
This tutorial will explain how to create a vibration detector using a vibration sensor and a lopy4. This will be a very simple project that should only take about 3-4 hours to complete for someone familiar with computers.
### Objective
My initial idea with this project was to create a device that, if placed on the floor, could react to the vibrations of someone walking by. It turns out that the sensor used is not sensitive for this to be feasible so we have to adjust the purpose of the device a bit. Instead we will detect whenever the front door is opened or closed by attaching the device to it. This could be useful for alarming you if someone enters your home uninvited.
Unfortunately as I am writing this I am still waiting for my battery pack to arrive. This forces me to power my device through a cable which is why I do not actually use my door-opener-detector to detect door operations, but instead it just sends off an alarm whenever I slam my fist into my desk.
### Material
In this section I will present the exact parts that I used.
* [lopy4](https://pycom.io/product/lopy4/) (other pycom boards should also work fine)
* [expansion board 3.0](https://pycom.io/product/expansion-board-3-0/)
* [A few cables](https://www.electrokit.com/produkt/labsladd-1-pin-hane-hane-150mm-10-pack/) *
* [Vibration Shake Sensor](https://www.electrokit.com/produkt/vibrationssensor/)
* [RGB LED Module](https://www.electrokit.com/produkt/led-modul-rgb/) (optional)
* [Breadboard](https://www.electrokit.com/produkt/kopplingsdack-400-anslutningar/) (optional)
\* _If you use a breadboard make sure you get cables with male/male connectors, else get male/female._
I bought all my parts from Swedish electrokit.com. The sensor come from the [Sensor Kit - 25 moduler](https://www.electrokit.com/produkt/sensor-kit-26-moduler/) bundle which was 299 SEK, while all the other parts were included in the [LNU – 1DT305 Tillämpad IoT – LoPy4 and sensors bundle](https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-lopy4-and-sensors-bundle/) which was 995 SEK.
### Computer setup
We will be using Visual Studio Code for our IDE. Pycom has released an extension for VSC which makes it really easy to work with the pycom board. Steps to set up the development environment:
1. (optional) You might consider flashing your expansion board before doing anything. Follow the official instructions [here](https://docs.pycom.io/pytrackpysense/installation/firmware/) if you want to do this.
2. Install [Visual Studio Code](https://code.visualstudio.com/download)
3. Create a project folder somewhere. Open this folder in VSC.
4. In VSC, go to the extensions tab and install the _Pymakr_ extension. Some new buttons should appear on the bottom bar
5. Connect your device via USB. A new Pymakr Console should open up. Any code you type into this console will be executed on your pycom device!
6. Press _Download_ on the bottom bar to get all files currently on the device. A _main.py_, a _boot.py_ and a _pybytes_config.json_ should appear in your project folder. We will only be using main.py in this project.
You're all set!
### Putting everything together

The image above is a crude sketch of how the sensors should be connected. Also take a look at the image in the last section to see how it looks for real. Note that not all pins on the board are equal, so take care when selecting them and refer to the [official documentation](https://docs.pycom.io/datasheets/development/lopy4/) for specifics.
### Platform
We will use pycom's own [_Pybytes_](https://pybytes.pycom.io/) platform for handling and presenting the data. Pybytes is really easy to set up and use, which is why we go with it. It is also possible to set up webhooks from your pybytes account if you want to take the project further. Let's set up our Pybytes account and connect our device!
1. Create an account at [Pybytes](https://pybytes.pycom.io/)
2. Go to the [network settings](https://pybytes.pycom.io/settings/device) page
3. Add your WiFi network by pressing _ADD WIFI_ and entering your network's SSID and password. Note that the lopy4 does not support the 5Ghz band!
4. Now add your device. Make sure your device is connected and go to the [devices](https://pybytes.pycom.io/devices) page. Select _Connect via USB_ and follow the instructions. Make sure you select WiFi as your cennectivity method and then select the WiFi you registered in the previous step.
5. You should be taken to your device's page. Here you can find a box saying _Activate your device with Pymakr plugin_. Follow these steps and your device should be connected!
6. Select your device in Pybytes. Go to the _Signals_ tab and press _Define new signal_. Set _Signal_ to 0 and _Name_ to _Alarm_. Save. When we trigger an alarm we will be sending a signal using channel 0, which we now named Alarm.
Your device is now all set up! Time to upload the code to your device.
### The code
```python=
import utime
import machine
import pycom
# Constants
PIN_RGB_RED = 23
PIN_SENSOR_VIBRATION = 16
SLEEP_TIME_MS = 50
ALARM_TRIGGER_TRESHOLD = 1000
ALARM_TIMEOUT_MS = 3000
ALARM_CHANNEL = 0
ALARM_MESSAGE = 1
# Set up sensors
adc = machine.ADC()
sensor_vibration = adc.channel(pin='P{}'.format(PIN_SENSOR_VIBRATION))
rgb_red = machine.Pin('P{}'.format(PIN_RGB_RED), mode=machine.Pin.OUT)
alarm_timer = 0
alarm_start = 0
# Disable the built-in pulsing LED
pycom.heartbeat(False)
# Main loop
while True:
if sensor_vibration.voltage() < ALARM_TRIGGER_TRESHOLD:
# Alarm triggered!
print('Alarm!')
pybytes.send_signal(ALARM_CHANNEL, ALARM_MESSAGE)
rgb_red(True)
alarm_timer = ALARM_TIMEOUT_MS
alarm_start = utime.ticks_ms()
if utime.ticks_ms() - alarm_start > ALARM_TIMEOUT_MS:
# Alarm timed out
rgb_red(False)
utime.sleep_ms(SLEEP_TIME_MS)
```
This code will constantly monitor the voltage given by the vibration sensor and trigger an alarm if it drops below a certain treshold. The alarm is set to last a few seconds before turning off.
To upload this code to your device edit the main.py in your project's root folder to contain the above code, then press _Upload_ on the bottom bar in VSC. This will take a little while but when done you can try it out by bashing your fist on your desk. If it works the LED should light up for a few seconds and _Alarm!_ should be printed to the console. You should also see that some data has been received on your Pybytes device page.
### Transmitting the data / connectivity
The data will be transmitted to the Pybytes platform via WiFi. To actually perform the transmission we have to link our device to Pybytes and then call _pybytes.send_signal()_. How to do this is explained in other sections.
### Presenting the data
Your data should now be transmitting to Pybytes but we don't have a nice way of displaying it yet. Let's fix that.
1. On Pybytes, navigate to your device, select the _Signals_ tab and then select the _Alarm_ signal that we created earlier.
2. Now press _Create new display_ in the upper left.
3. Select _Bar chart_ and save the new display. You can also select to add the chart to your device's dashboard for easier display.
This chart will now display a bar whenever your alarm is triggered! Note that without any further configuration data here is only saved for one month.

### Finalizing the design

This is the finished device! If only we had a case and a battery pack we could make the device much more practical. Maybe they will arrive soon.