Skip to content

My First Three Days With micro:bit

Peter Cunderlik edited this page Jan 27, 2016 · 5 revisions

I was really excited when I got an email from @peconia asking me whether I wouldn't mind having micro:bit for a week. Of course I wanted it! Now! Ideally cancelling all real-world activities.

Micro:bit Marja arrived on Wednesday and I couldn't wait to get back home. I quickly checked what other people have done with micro:bits and I was really impressed. The sudden feeling of responsibility of having this rare microbit:bit made me panic a bit. What was I going to do with Marja? So little time, even fewer ideas!

Once I calmed down, got home and connected Marja to the laptop, it was actually difficult not to come up with little scripts. :) So here they are, in chronological order.

read-sensors.py (code)

I thought a good introductory script would be printing out status of all sensors. It didn't take long and I had a basic version showing current, minimum and maximum values of the accelerator. Adding buttons, compass and first 3 pins was easy as well.

The only surprise was when I found out there are so many more pins than I expected. (And two are missing.) Some are input/output others are not. The script tries to figure out which ones are for what.

dice.py (code)

I saw micro:bit for the first time in November 2015 at the Cambridge Python User Group meetup. I didn't quite manage to implement anything interesting but ideas started flowing when walking back home. One of the ideas was a dice/die simulator - all you had to do was to shake the board and the result would show up on the screen. Knowing how to read the accelerometer sensor I started exploring how to toggle some LEDs.

There are two ways of constructing Images that can be displayed on the screen: either creating them from Python strings or setting individual pixels using set_pixel(). Since I only wanted to display 6 different faces, I chose the former.

Next step was to detect when the board was being shaken. It was simpler than I thought (and now I know that everything with micro:bit is simpler than I think!) - all I had to do was to check the difference between consecutive accelerometer read-outs.

The first version was up and running in no-time but it looked a bit dull. The reason was that at any point in time there are fever than 6 LEDs lit up, making it a bit boring. So I added some "noise" to make the display look a bit more random.

When thinking how I could improve it even further I realised that it'd be interesting to add a "cheat mode". As always with micro:bit, simpler done than said, cheat mode was implemented. All you have to do is to press any button whilst shaking. The result will always be 6.

fire.py (code)

When I started programming more seriously in the 90's I was fascinated by the demoscene. I was in awe seeing all the different demo effects people could do with humble hardware. One of those effects was fire. It turned out the algorithm was very simple (generate random row, blur the entire display, shift it up, repeat) so I simply had to implement some variations myself.

Fast forward to 2016 and here I am, implementing fire on micro:bit. The somewhat limited 5x5 LED display makes it more difficult to see the actual fire but with a little bit of imagination it's there.

Again, simply showing some flames was not enough, so I added controls to increase/decrease the flame.

This was the first time I had to think harder how to implement this effect because I have never written any "high-performance" image processing in Python. Was it faster to keep pixels as a string, manipulating it directly or was it faster to use bytearray and then display it using set_pixel()? I tried both and the latter was somewhat faster. I'm sure there's a better way of doing this. It was very tempting to use some assembler (like asmleds.py) but I didn't want to spend days learning how to pass bytearrays into assembler routine.

Light Painting (code)

When I was thinking about the 5x5 display I tried to figure out how to show larger images. That's when I realised that scrolling + moving the board results in light painting. Writing a script was the easy bit, making it show something recognisable turned out to be more difficult. Trying to move the board at the same speed as scrolling proved to be tricky.

After many tries (waving my hand in front of the camera is rather tiresome!) I got a reasonable result:

I'll leave it to other clever people to come up with a mechanised light painting, where micro:bit controls its motor so the board moves at exactly right speed on a perfectly straight path.


So that's my first couple of days with Marja. I'm really impressed how easy it is to write scripts for micro:bit. Idea-to-result trip is so straightforward.

Read more in my second post.

Clone this wiki locally