Summertime equals spending time outside and that’s great, but a too big exposure to UV is not good and hence this wearable UV sensor project which will help you stay safe.
This is not a medical device and is only meant to heighten the awareness of UV exposure.
Before anything else, you should ensure that the sensor and micro:bit are working as expected.
Connect the micro:bit to the UV sensor using some crocodile leads as follows:
On your host computer, go to https://makecode.microbit.org/ if you’re unfamiliar with Makecode and programming the micro:bit, you can refer to this guide.
Create a new project, give it a name and assemble the following code:
This first block, is only there to make sure the micro:bit is working.
Next, add
This will read the data coming from the sensor and display it on the micro:bit.
Load the code to the micro:bit.
You should now see the heart displayed on the micro:bit’s matrix briefly, followed by the sensor reading which will be between 0 and 1023.
If you only see a value of 0, the sensor is not being exposed to UV. Try holding it close to a window or outside as some windows have UV filters.
Ensure that:
Functions are reusable snippets of code that can be called whenever necessary. The first programming step is to create all the functions needed for the project. Alternatively, you can find the completed Makecode file here.
This function defines some of the variables that you’ll be using in the code.
Repeat the process to create 3 more variables:
Setup defines the starting conditions before the programme runs.
This function deals with the alarm triggering.
Assemble the blocks below. All green blocks can be found under Logic.
This function reads the UV sensor and processes the reading.
This function clears the log and resets the device.
So far your code should look like this:
On start, gets executed when the programme is first launched, setting everything up to start working. It does this by executing some actions and calling some of the functions that you declared in the previous step.
Forever is the main process that operates the project. It runs forever in a loop calling the relevant functions.
The next step is to program what happens when pressing the buttons.
Pressing button A will display if the UV level is OK.
Pressing button B will display the total UV exposure.
Pressing A+B will clear the log.
Next, create the timed loop that will repeatedly check for UV and trigger the alarm if necessary.
This block detects when the log is full.
Your blocks should now look like this:
Now you’re ready to go outside and test it.
From a quick tap to smashing that love button and show how much you enjoyed this project.