I've just programmed this compass using a Micro:bit and a glow bit rainbow. You can see that the LED always wants to point this way. I'm going to show you how to use MicroPython to extract a compass heading out of the Micro:bit and how to display which way north is on your rainbow.
Let's get started. We'll start by wiring our rainbow to our micro bit. I'll use black for ground, ground on the Micro:Bit, ground on the rainbow, red for three volts and yellow for data. That's pin zero on the Micro:bit and data on the rainbow.
Connect your Micro:bit to your computer with a USB lead and with your browser navigate to python.MicroBit.org and this is our MicroPython programming environment. This looks like the default project. I'm not going to need any of this code. If you followed along with any of the other projects in this series, I've just done the same housekeeping as usual. I've imported NeoPixel because rainbows work with the NeoPixel library and I've declared my rainbow as being connected to pin zero and having 13 LEDs. Because it's a little bit complicated to write out bit by bit, I've just copied and pasted this function. We'll come back to it but for now all you need to know about this convert function is that we'll use it to convert from one range of numbers to another range.
Let's first get familiar with the Micro:bit compass. I'm just going to stream out the compass heading to the console. I can say heading equals compass dot heading and for now I'll print, I'll convert, the heading is a number, I'll convert it to a string for now and just print that out. We also need a short delay. Sleep for 100 milliseconds.
Connect to your Micro:bit and flash. Once your Micro:bit is flashed, you'll have a message that requiresYou need to calibrate your Micro:bit by tilting it in every direction. As you tilt it, you will see the little grid of LEDs being filled. The goal is to tilt it so that every LED turns on.
If you open the serial console, you will see the heading in degrees that your Micro:bit is pointing. As you rotate the Micro:bit closer to magnetic north, the number will decrease until it reaches around 350 or 360-degrees when you cross over.
The compass ranges from 0 to 360-degrees, with north at 0-degrees and east and west at 90 and 270-degrees respectively. However, working with values that quickly transition from near 0 to near 360-degrees can be tricky due to the discontinuity in those numbers.
To address this, you can convert the compass values to range from -90 degrees to 90-degrees, with north still at 0-degrees. This creates a nice continuous line of values that can be easily assigned to the LEDs on the rainbow.
To achieve this, you can check if the heading is greater than 180-degrees. If it is, then you are on the left-hand side or the west side of the compass, and you just need to subtract 360 from the heading.
By flashing the updated code, you will notice that as you rotate the Micro:bit more towards north, the numbers get smaller and eventually cross through zero to become negative. This eliminates the discontinuity and creates a nice continuous range of compass values.
Now that you have nice continuous compass values, you just need to map the negative values to the LEDs.90 through to 90-degrees as LED numbers on our rainbow. That's where this convert function comes in. So index, which will be the LED number that we want to illuminate, we're going to set that to the result of our conversion. Convert heading from 90-degrees down to negative 90-degrees and we want to convert that to zero to 12. That's our LED number.
So these arguments are the range that we're converting from and these are the two. And then because this will return a decimal point number, we have to round it to the nearest whole number. We can't pass in LED number 1.5. We have to pass in a whole number for the LED that we want to illuminate. And then we just illuminate that LED. So I'll just say rainbow index and I'll just make it red and rainbow.show.
In our help message as well, we can also add a space and add the index of LED that we're going to light up just for some debugging help. Helps to clear the strip at the start of every loop. That way we're only illuminating one LED. I'll use this plastic so you can see the LED a little easier. There you go. You can see that as I hold my Micro:bit to the rainbow and turn them both together, that LED will always point over that way.
If I turn it too far in the other direction, of course, it just hits the end and stops moving. But if I take it past around to this way, now it's telling me that I need to keep turning right to get to north. So it'll always show me the direction of north. Or if I'm rotated too far away, it'll show me the closest side.
There you go. That is an electronic compass. Thanks for joining me on this little GlowBit rainbow programming adventure. If you make anything cool with this project or any others, I'd love to see them on the Core Electronics forums.And until next time, happy coding.
Makers love reviews as much as you do, please follow this link to review the products you have purchased.