Introduction: Access Speed Detection IoT Device
Warning, this tutorial is not for a finished product, but the core module that can be adapted to many useful devices.
This is an Access speed detection device that is also an IoT device. The function of this device is to detect the frequency of the changing of the magnetic field around the sensor. For example, say that you present a magnet nearby the sensor, it would be able to sense it and notifying the user.
In the final IoT part of the instruction, we would be able to output four parameters: current speed, overall speed, best speed and total access.
With these outputs of our device, we are able to build a lot more: the best fit is to build a speedometer, especially of a spinning thing. We would fully discuss its scalability at the last step.
Step 1: Prepare the Componets
There are three key components of this device at least for this prototype I designed. They are a hall magnetic detector, a Raspberry Pi 3, and a magnet.
A hall magnetic detector detects changes in the magnetic fields:
For example, say that you present a magnet nearby the sensor; it would be able to sense it and put a low current on its pin to notify a micro-controller.
A Full description of the sensor is attached to the next step.
It is typically $5-6.
A Raspberry Pi 3 is a quad-core Linux machine where we run our python programs to figure out the signal from the sensor and to serve a website that displays our data. We would use its GPIO pins to connect the hall magnetic sensor.
It is officially $35.
A magnet changes the magnetic field to interact with the sensor in this design.
It is borrowed from my professor.
There are some optional components like battery stations if you do not want to connect it to a stationary charger, or a phone app if you want to design it...
Step 2: Assemble Them
The main job here is to connect the hall magnetic sensors with GPIO pins on the Raspberry Pi.
The attached file is a detailed description of the structure and usage of the hall magnetic sensor. In short, they have 3 pins, the – pin, the middle pin, and the S pin. The – pin is ground. The middle pin is the +5V input. The S pin is the signal pin. When a magnet is not present, the hall magnetic sensor would output a high signal (+3.3), when the sensor detects a magnetic field, it would output a low signal.
On the other side, Raspberry Pi 3's GPIO has 40 pins. We need to use three pins: a ground, a +5V, and a GPIO pin. I picked pin 2, pin 3, and pin 17 in my design to connect to the three pins of the hall magnet sensor.
Step 3: The Code for GPIO
First of all, we need to import by import RPi.GPIO as GPIO.
After that, we need to say GPIO.setmode(GPIO.BCM) to set the mode
Then, we need GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP) to settle down with the pin 17 with input mode and to pull up the current on it.
In addition, I wrote a function sensorCallback to define the behavior when a magnetic field change is present.
Finally, I typed GPIO.add_event_detect(17, GPIO.BOTH, callback=sensorCallback, bouncetime=200)
to add an event. GPIO.both means no matter the current on the pin 17 is rising or dropping, we'll call the function sensorCallback
The full code is attached. In addition to the GPIO code, it integrated the flask python code to make it an IoT device. More details are explained next->
Step 4: Make It IoT
To make this device an IoT device, I used python flask as a web server, which is the file you downloaded at the previous step. What the server does is to keep track of the magnet accesses and the time stamp associated with them, and finally calculating the four outputs.
As mentioned in the intro, there are four parameters being output: current speed, overall speed, best speed and total access.
The current speed records the frequency of the last 5 accesses. It is calculated as 5 / time it takes to finish 5 accesses and should be in the unit number of access per second. The overall speed records the frequency of all the accesses. It is calculated as total accesses / total time and should be in the unit number of access per second as well. The best speed records the highest frequency record of the current speeds. It should be in the unit number of access per second as well. The total access is just the total amount of access recorded.
From the above picture, we are able to see two buttons beside the four outputs. As their name suggests, they are controlling the device to start and pause the recordings. They use flask forms to work.
To run the code you should:
first: using a terminal to get into that dir;
sec: type export FLASK_APP=iotapp.py to set iotapp.py for flask
3rd: type python -m flask run --host 0.0.0.0 to open up the server.
To access the website, you just type in any browser: your raspberry pi's ip address and '/5000' (''not included).
Step 5: Applications and Expansions
With this prototype device, we are able to expand on it pretty easily.
As we mentioned in the intro, to expand on this, the best fit is to build a speedometer of spinning things.
This is another instructable that builds a bike speedometer with a similar concept: https://www.instructables.com/id/Bike-Speedometer-...
This is a video about a Fidget Spinner RPM recorder:
What this tutorial can enhance on them is the IoT part. Imagine seeing the bike speed on your phone if you design an app for it!
Other applications I could think out is to see it hall magnetic sensors could detect the electrical magnetic waves created by wireless devices or wireless charging station, and see their changing speed.(Possibly)