# Distance sensor using Pycom by Henrik Rosberg (hr222ks) This project is good to get started with a very easy example on how to get started with your Pycom device and get a basic understanding of how Internet of Things (IoT) works in practice. It contains of a Pycom expansion board, LoPy4 and a distance sensor that will continuously measure the distance and upload the distance on the internet. Since this helps on getting started and understanding the basics it will be easier to eighter continue this project or trying different sensors. Since this project is considered basic is should not take too much time to complete. If this is the first time using a Pycom this will take about 1-2 hours and hopefully when it is done the basics of the device will be clear. # Objective Without knowledge of how to program in Python or using a Pycom before it was important to choose a project that was not going to be to challenging from the beginning and therefor a distance sensor seemed in the right area. I also knew that if the project went smoothly then I could just try to add more challenging tasks. The purpose could have a range of different tasks, but I was thinking it could be placed on a security system at home. By placing this near the front door it will see if there is a person approaching the door when you are not home. Then this could be sent to you as a notification and if you have a security camera you could see in real time if is a friend or foe. The data could then be analysed to see how many people walked by the door and if there is a lot of foes then maybe it is time to get a new house alarmed installed, but hopefully this would not be needed. Although this project is far from that experience it is a step in that direction and the most important part is I have now both a better understanding of the Pycom and how IoT works in practice a little bit more. # Materials The prices down below are not per unit instead you are going to have to buy a bundle and this is what some of it costs. | Material | Cost | Quantity | | -------- | -------- | -------- | |Pycom expansion board 3.0| 16 Euro| 1 | | LoPy4 | 34.95 Euro | 1 | |Distance sensor| 59 kr | 1 | |Breadboard| 59 kr | 1| |Wires | 24.95 kr | 8 | | Resistors (1k Ohm) | 49 kr | 3 | Pycom expansion board and LoPy4 can be bought at pycom.io/webshop/ The others can be bought at electrokit.com Pycom expansion board 3.0 ![](https://i.imgur.com/Ih5JBw7.png =300x) The expansion board is compatible with all Pycom modules and has an easy plugin for code editors to write the MicroPython scripts. It also makes it easier for debugging and is accessible thorough the serial port. Pycom LoPy4 ![](https://i.imgur.com/mhEUYpH.png =300x) Can be used for multiple channels of connectivity and is programmed by MicroPython. Distance sensor ![](https://i.imgur.com/RQe4QPI.png =200x) Triggers a signal through one of the “eyes” and the other detects the echoed signal and measure the time between them. Breadboard ![](https://i.imgur.com/Fd9rMCI.jpg =200x) Construction base. Wires ![](https://i.imgur.com/dl4SUts.jpg =200x) Connects everything. Resistors ![](https://i.imgur.com/5S58ZDk.jpg =200x) Used for voltage divider. # Computer setup I have been using Atom as my IDE and is using Windows 10 but before starting to write the code it is important to make sure it is updated. All the important downloads could be found here https://docs.pycom.io/pytrackpysense/installation/firmware/. Download the Expansion Board DFU v3.1, DFU-util v0.9 and Zadig and make sure every file is in the same map on your computer (it can still be in Downloads). Open Zadig and under Options click on “List All Devices”. Also make sure that the driver that is supposed to be downloaded is libusbK (v3.0.7.0), then before plugging in the Pycom expansion board in the serial port hold down the S1 button and plug it in while holding it down. Then there will appear “Unknown Device #1” in Zadig and then click on “Install WCID Driver”, Done! It is now time to update the firmware on the Pycom. Install the firmware from https://docs.pycom.io/gettingstarted/installation/firmwaretool/ and choose Windows. Then open Pycom Firmware Update and plug in the expansion board to the serial port. Check the “Erase flash file system” box and continue. Now it is all done it you could start to code! Download Atom and start a new project together with a new file that must be inside the project map. The code can run by pressing alt+ctrl+r or choosing the play button in the terminal window down below. If you want to upload the code on to the device so it could run without being connected to the computer press the “upload project to device” down below in the terminal window. # Putting everything together ![](https://i.imgur.com/36IJukO.png) The resistors are all 1k Ohm and creates a voltage divider because the echo on the distance sensor can not be 5V. I think with this kind of simple circuit it is best to keep it at a development setup so that you later could add more things to it. # Platform My data is uploaded unto Pybytes. Pybytes is a cloud-based platform that is available for all Pycom devices. All you need is an account and it is free. You can store the data up to 5MB for free but if you want more it will be a monthly cost. This is however not something I will do. This is a good alternative to just getting started, however I think the bar representation was nice to get the visualisation of the distance of someone getting closer. Also, if your using multiple sensors it would be a good visualisation on which bar is going down, so you know where the person is. But it must be investigated further on what kind of platform would be the best one if you want a different visualisation. # The code import _thread from time import sleep import utime import pycom import machine from machine import Pin #initialise Ultrasonic Sensor pins echo = Pin('P21', mode=Pin.IN) trigger = Pin('P20', mode=Pin.OUT) trigger(0) #Ultrasonic distance measurment def distance_measure(): # trigger pulse LOW for 2us trigger(0) utime.sleep_us(2) # trigger pulse HIGH for 10us trigger(1) utime.sleep_us(10) trigger(0) #wait for the rising edge of the echo then start timer while echo() == 0: pass start = utime.ticks_us() #wait for end of echo pulse then stop timer while echo() == 1: pass finish = utime.ticks_us() #pause for 20ms to prevent overlapping echos utime.sleep_ms(20) #calculate distance by using time difference between start and stop #speed of sound 340m/s or .034cm/us. Time * .034cm/us = Distance sound #travelled there and back # divide by two for distance to object detected. distance = ((utime.ticks_diff(start, finish)) * .034)/2 return abs(distance) #Measure distance and sending sensor data every 5 seconds def send_env_data(): while True: dist = distance_measure() # send distance as signal 0 pybytes.send_signal(0, dist) sleep(5) #Start your thread _thread.start_new_thread(send_env_data, ()) # Transmitting the data / connectivity The data is sent every 5 second from the Pycom using WiFi. The transport protocol is MQTT, that is send through mqtt.pybytes.pycom.io. That is Pybytes own server and is initialized in pybytes_config.json. By first getting the Pycom device on the WiFi is there two ways that I was using. The first one was used in the beginning just to understand how the connection to WiFi was established. This is however a bad way to do it since you will have to run the code each time the device is going online. The code is giving below. import machine from network import WLAN #configure the WLAN wlan = WLAN(mode=WLAN.STA) nets = wlan.scan() for net in nets: if net.ssid == 'NETWORK': print('Network found!') wlan.connect(net.ssid, auth=(net.sec, PASSWORD), timeout=5000) while not wlan.isconnected(): machine.idle() # save power while waiting print('WLAN connection succeeded!') break It is therefor easier to open pybytes_config.json and there change the network name and password. Then it will automatically connect every time it starts. This is just to get the device connected to the WiFi. But sending data to the platform using the connection it is important to import Pycom and _thread and then _thread.start_new_thread(send_env_data, ()), where you could define the threads behaviour in the send_env_data. By actually sending data use pybytes.send_signal(0, value). I thought this was the easiest way to send my data. # Presenting the data To get a visualisation of the data in Pybytes you first need to create an account as stated before. Log in to pybytes.pycom.io and then “Add Device” via USB and choose LoPy4 with WiFi. Give it a fun name and a WiFi password that could be anything. The device is then added and could be found under “All devices”. Go to the specific device and under “Provisioning” choose “GENEREATE NEW ACTIVATION STRING” that could be activated using eighter Pycom Firmware Update or Atom. I think it is easy just to print into Pymakr pybytes.activate(“ACTIVATION CODE”) in Atom. The device is now connected to pybytes. It is now possible to send data to Pybytes which means it is time to visualise it. After selecting your device in Pybytes choose “Signals” and an undefined signal should have appeared. Choose “Define new signal” and choose an appropriate name with cm as unit. The signal is now defined and by clicking on the signal it is now possible to choose a new display. I have chosen a bar chart where the data is saved for 20 data points which means each datapoint is saved for 100 seconds. ![](https://i.imgur.com/DRYvNCd.png) This is how it looks in Pybytes. # Finalizing the design ![](https://i.imgur.com/EIgu5Fl.jpg) I would say this is an easy project just to get things started and is, as it has been for me, a completely new concept. A lot of time was spent to understanding the basics of everything and for troubleshooting. Looking at the result I think it looks very easy but since I know how long it took for me to finish, I am satisfied with everything I have learned. I am sure that I could have gotten started a lot quicker if I had asked others that knew more than me from the start but I am also determined to understand on my own but you live and you learn!