Tutorial on how to build a "hall effect sensor" controlled LED
By David Klarbrant, dk222ra@student.lnu.se
Short description: A hall effect sensor is a sensor that can detect magnetic flux and switches the output from the sensor once the flux exceeds a specific threshold. This tutorial describes how to control a LED-light to make it light up once the sensor "triggers" and senses a change in the flux. The output will also be shown in a graph using PyBytes which is Pycoms own IoT cloud based platform.
Time estimated to setup from scratch: ~30 mins
Objective:
I chose this project mainly because I wanted to learn the basics of IoT i.e. how to wire sensors, how to program them, how to connect them to the internet, etc. This was done solely with this kit from Electrokit. I wanted to do something other than just using the sensors on the Pysense shield because I wanted to wire some stuff myself and opted for the Hall Effect sensor together with a LED to make it a bit more exciting!
As the project stands now, it doesn't serve much of a purpose other than being an easy entry point to learn the basics of IoT but one could imagine a similar concept where you for example mount a sensor on a door together with a magnet so that you are sent information or a LED lights up when the door is open/closed. There are a lot of things you can do with the concept of a hall effect sensor and a magnet.
This tutorial will give you an easy project to start with using a pre-configured bundle in order to understand the basics around IoT, sensors, programming, internet connection, graphing/visualisation, etc.
Material:
As mentioned previously, everything needed for the project is included in this kit from Electrokit. The kit costs 1499.00 SEK as of June 2022. You can however, find the items for a cheaper price if you buy only the needed items, but it isn't much money you will save.
Here are the specific items needed:
The total cost of the individual parts is ~1233 SEK so I would recommend just getting the complete kit to make it easy.
What is everything used for?
Computer Setup
I've used Atom as my preferred IDE (the program where you write, test and excute your code), it's easy to setup and learn the basics. I've had only some experience with programming and for that I used Visual Studio, ATOM was easier for me to get into than VS was.
To setup Atom and start your IoT journey, you can simply follow these steps:
Step 1: Download and install Node js (here). Node js is needed for the Pymakr plugin below.
Step 2: Download and install Atom (here).
Step 3: Open Atom and the package manager from: File >> Settings >> Install.
Step 4: Look for Pymakr and Install it. (It takes a while; wait until shows it is successfully installed)
Once you have setup your Atom IDE (or other suitable IDE of your choice) you have to update the firmware on your FiPy and Pysense. Otherwise you will get issues and errors once you start pushing your code.
Let's start with the PySense firmware. You should not mount the FiPy (microcontroller) on the PySense for this, just connect the Pysense to the computer using the USB.
Instead of me typing out exactly how to do it, I recommend that you follow the instructions in this video as that will be a way faster way of understanding and doing it. I recommend you to check out more of Core Electronics youtube and website as they have some really useful and cool guides.
FYI, the button that you have to press in that they reference in the video is this one, I spent some time trying the other oneโฆ
Now it's time to update the firmware on your microcontroller, simply follow these steps:
Follow these steps:
Step 1: Mount your FiPy on the Pysense shield, remember that the LED of the FiPy is on the same side as the USB port on the Pysense.
Step 2: Connect your PyCom Expansion board to your computer with a USB cable.
Step 3: Download the Firmware Update from this link.
Step 4: Install the downloaded file and open it.
Step 5: Follow these steps to upgrade firmware:
Pic 1.
Pic 2.
Pic 3.
If you have done all of the steps for the FiPy above, you should be able to open ATOM and disconnect and reconnect the board. Note that the area where the text "Connecting to COM9โฆ" etc is called REPL.
You are now ready to push some code to the microcontroller! Let's see if it works!
Try print("Hello from PyCom") on REPL and see if it successfully shows the message.
Step 3: We are ready to make our first project now. Create a folder on your computer, on the desktop or anywhere where you want your projects to reside.
Step 4: In Atom click on File > Add Project Folder from the left panel then open the folder you created.
Step 5: Right-click on your project folder from the left panel and choose New File to create a new file, in the opened box enter main.py and press enter. All projects must have a main.py as it is the start point for code execution.
Step 6: Copy the following lines in main.py and save it. The # is used to comment and describe the code to make it easy to follow along what each line or function is doing.
Step 7: Now upload the file to your board from the upload project button:
Step 8: The LED on your board starts to blink different colour
Congrats, you have now created your first project and pushed your first code to your microcontroller!
Putting the hardware together
Set the board up as shown in this image. Note that the Hall Effect should be rotated 180 degrees in reference to the picture so the flat side of the sensor is the other way around, always check the data sheet to see which pin is supply voltage(red wire), GND (blue wire) and output (white wire.)
The cables that goes from the microcontroller directly to the Pysense are the following:
5V <โโโโโโโโ-> 5V.
GND <โโโโโโโ-> GND.
UART_Rx <โโโโโโโ-> UART_Rx.
UART_Tx <โโโโโโโ-> UART_Tx.
IRL it should look something like this. Once again, note the hall effect sensor. The flat side towards you from this point of view.
Platform
The IoT platform of my choice was PyBytes which is PyComs own cloud platform used to display information, graphs and so on. I choose this one because I wanted to use their platform as it seemed like the easiest one to connect to as this was totally new to me and to be fair, it was very easy and fast. Getting everything setup was super easy, however, I would not recommend you to use Pybytes for this project if you have other options such as Datacake, Grafana, etc. The reason being that Pybytes free version (I have no experience with the payed version, it might be better) is very down scaled with limited functionality:
If you have the time and/or knowledge, I do strongly recommend using another visualisation platform for this project.
The code
In the project folder you need two files, the "boot.py" which is the first file to trigger when you start executing the code and start the microcontroller(note that unlike "main.py", the "boot.py" is not required for the software to run but is very useful), this will contain the connection to WiFi, and the "main.py" which contains the code for the functionality of the sensor, sending information to PyBytes and the lighting of the LED.
The "boot.py" should contain the following code:
It contains some unnecessary code that is not relevant to the acctual connecting but can help you verifiy that you actually connect. I believe you can remove the last function "def http_get" etc but the code doesn't "hurt" or change any functionality. You can actually use visit http://detectportal.firefox.com/ and verify that data was succesfully sent/recieved.
Make sure you edit "YourSSID" (xxx in the picture) and "YourPassword"(xxx in the picture) to your own SSID and network password.
The "main.py" should contain the following code:
Transmitting the data
The data is sent using WiFi and is sent every time the sensor is triggered within a timeframe of 5 seconds (can't be triggered more often than that). As mentioned before though, the free pybytes version is limited to 5 minutes so that is how often it will show updated data in Pybytes and uses MQTT altough the code contains some HTTP since I reused it from the tutorial.
Presenting the data
As mentioned before, the Pybytes visualisation for this use case isn't all that exciting, as been below you can see 3 different widgets/charts. The past hour/day data only shows the amount of data sent i.e bytes over time.
The third one named "test" shows a flatline and will only indicate that FiPy1 has sent a signal named 1 with the value 0, which is the value that the sensor sends when it is triggered by a magnet. The plan was to make this a bit more exciting and send all the value 1:s as well so that you can see in real time whenever the sensor is not triggered and exactly when it gets triggered and sends a 0 instead. But due to the limitations previously mentioned about how often you can send/store data (once again limited to 5 minutes) to Pybytes free version, it makes then visualisation dull to say the least, but it works!
Finalizing the design
The final result is kind of what I was expecting in terms of functionality of the hardware/software. As mentioned multiple times earlier I was quite disappointed in Pybytes limitations and hence moving forward, I will not use Pybytes for my future projects. Due to the circumstances with shortages and longer delivery times I only just received the sensors that I wanted to use originally so I believe I can make a much more exciting project in the future, using the experience and knowledge I've acquired during the course. And for me that was the whole point of this course, learn, try, make mistakes and try again. I will remain within the Discord community seeing that it is very active and filled with great knowledgeable people, both teachers, teacher assistants and students that have all been very helpful for someone who was new to this.
If you are new to IoT, programming and hardware wiring I can definitely recommend this project as one with very low skill level, you will understand the basics and from here on move to more advanced and fun projects! Check out the video if you want to see it in action, it's not the coolest thing but it works! :)
Video of the final result:
https://youtu.be/KT2Rq_VL6-8
Best regards, David!