Among the BBC Micro:bit’s onboard sensors is an accelerometer. This can be used for a myriad of applications, but I will use it for the simplest of motion detection – a step-counter!
The idea is simple, it detects enough motion and considers it a step.
As soon as the system turns on, it will begin “sensing” steps. The current step count will display on the LED matrix screen. The BBC micro library provides functions to recognize various gestures. As I read in some articles, best gesture for a step meter would be a 'shake' in terms of the library.
The software works as follows:
- If 'shake' gesture is recognized then increase a step counter.
- Always show the step counter on the LED's.
First, let’s gather all the necessary components you’ll need to build this project.
1x 3 V battery pack - This is for AA cells, feel free to use the AAA one.
2x AA batteries - I am linking the ones I used here.
Since to use a step counter you’ll want to be mobile, I am recommending a battery pack. I used 2x AA rechargeable batteries out of curiosity; if rechargeable AA cells are usually at 1.2V, will it be enough to power everything at 2.4V or less, without issues? --- The answer is, yes, it’s fine.
Video demonstration of the project:
Load the code onto the Micro:bit.
I am providing the hex file you need to drag and drop onto the Micro:bit, and the python code as two separate files. For the below process, just use the hex file.
Plug the MicroUSB cable into the Micro:bit, and plug the other end of the cable to a PC or MAC.
At this point, you are going to copy over the code to the Micro:bit. I am providing the program (code) in this posts that needs to be copied over. When the Micro:bit plugs into a computer, it shows up as a USB flash drive. All you have to do is copy the file over to the Micro:bit, like it’s a USB Flash Drive, and the Micro:bit will reset, and the program is active.
After it resets, remove the Micro:bit from the PC and take it to wherever you have that battery holder.
A little bit about the code:
I commented the code enough to figure out I think. The program itself is rather straight forward:
I used the application "Notepad++" to write this, in case you were wondering.
Insert the batteries into the battery holder, and plug said holder into the Micro:bit.
If everything is ok, it will turn on and display a zero. It is ready to could steps.
To get “steps” to show up, it definitely needs a hardy shake. You can shake it by hand and see the counter go up.
If you are running, I recommend putting the Micro:bit and battery pack into some sort of medicine bag you wear around your neck. Otherwise, you might have to attach it to your ankle or wrist somehow.
The system will count steps until you turn it off, or it runs out of power. I had it on for several hours, curious how long the rechargeable batteries would last. I assume it would go beyond a few hours for sure.
It’s simple and to the point. However, this isn’t just a way to count steps. It could also be used to count any sort of motion.
- Attach it to a door and keep track of the number of times it was opened.
- Filing cabinet, desk drawers, anything that moved could be counted.
- Place inside your lunch bag, see how many times your sandwich was handled!
Imagination is the limit.