micro:bit OLED compass

These 128 x 64 pixel OLED displays are inexpensive and provide a great way to augment micro:bit projects. They’re easy to wire up using a breakout board and 4 female-to-female jumper wires. Just connect it like this:

```OLED  / micro:bit
----------------
GND  -> GND
VCC  -> 3v
SCL  -> SCL (pin 19)
SDA  -> SDA (pin 20)
```

There are lots of different ways of driving OLED displays from a micro:bit. I’ll do a round-up of them all soon, but here I experimented with using a MakeCode extension for a 4Tronix robot, the Bitbot, which happens to include some quite nice blocks for writing text and drawing shapes on an OLED display.

This driver can use the full 128 x 64 resolution but I’ve chosen to use the ‘zoom’ mode here to make the text easier to read.

The program keeps taking bearing readings in a ‘Forever’ loop, rounds them to the nearest 10 degrees to stop the display jumping around too much.

As well as showing the compass bearing and current temperatures as numbers, it also draws a line to show which way North is – just like a compass needle!

A bit of maths is called on for this to convert an angle to x,y co-ordinates that can be plotted on a screen. It gets a bit messy because of the offset to the middle of the screen, having to rotate it through 90 degrees and I needed to flip it left-right for some reason but in short it works like this:

x co-ordinate = cos(angle*pi/180) * half width of screen

y co-ordinate = sin(angle*pi/180) * half height of screen

It draws circles for degree symbols and turns the bearing into a string padded with spaces to make sure digits are over-written when it goes from a 3-digit bearing like 350 to a single digit bearing like 0.

Next I may improve this by allowing you to set the bearing that it points to to the current bearing, so you’d have a little gizmo that always points whatever way you want to go.

This entry was posted in microbit and tagged , , , . Bookmark the permalink.