Teaching robotics with the BBC micro:bit

Jim Mussared
May 18, 2017 · 8 min read

For the past several years, I’ve been fortunate to help run the Embedded Programming stream at the NCSS Summer School, a high-school summer camp attended by over 130 students and teachers each year. Previously, we’ve taught Arduino but this year we decided to try something new: enter the BBC micro:bit.

The BBC micro:bit is a new embedded computing device produced by a collaboration of tech companies including ARM, The Python Software Foundation, Tech Will Save Us, Microsoft, and of course, the BBC. In 2016, every grade seven student in the UK was given a micro:bit. Getting technical, it’s an ARM Cortex M0, with onboard Bluetooth radio, accelerometer, compass, temperature sensor, 25 LEDs and two buttons. They can be programmed in a variety of languages, from MicroPython to JavaScript to C++, plus a selection of visual languages such as PXT / MakeCode and Blockly.

Students work towards a Robo-olympics project: a late-night robotics & fitness event where the groups go head to head in a set of interactive activities designed by the students themselves. Each group of about 2–5 students has to come up with their own idea, from push-up challenges and robo-soccer arm-wrestling, to ‘Dab Dab Revolution’, a modern take on Dance Dance Revolution.

Many of the students have no previous experience with electronics, robotics or programming, whilst some have been competing at a high level in other computing and robotics programs. This extreme disparity in experience makes teaching at a level where everyone is engaged and pushed rather challenging, but we are incredibly lucky to have a great team of tutors such that we have a student-to-tutor ratio of around 3:1.

Teaching electronics and programming together

In previous years we’ve had to teach programming and electronics at the same time which was too challenging for many students. This year spent the first few days entirely working on programming skills. We used a combination of the Grok micro:bit Crash Course, using Grok’s micro:bit emulator, and writing code for the actual devices. In both cases, the emphasis was on logic, program flow and debugging, rather than “electronics”.

Previously we’ve worked with embedded devices like Arduino that need additional components like LEDs or buttons to be wired externally before students can run a “Hello World” style program. Having external wiring as an additional, separate source of errors makes programs particularly hard to debug, especially when students are only just learning about debugging! We found that adding this extra hurdle got in the way of building a solid programming foundation.

2012: Last-minute debugging before sending it up in the plane!

When it came time to build much more sophisticated programs that involved external circuitry, debugging was a lot simpler and much less frustrating because the students had first learnt to write more robust and well-designed code. In the past, simple things like a wire falling out could be catastrophic.

When a student’s first experience with both coding and electronics is overly frustrating, it’s all too easy for students to write it off as uninteresting and complex. This year, we had much better engagement from the beginning, particularly with students with less coding experience.

Embedded programming with MicroPython

It’s incredibly easy to get your code onto the micro:bit! We’ve struggled a lot in the past with other devices with getting drivers working on all the lab computers, and trying to debug why uploading wasn’t working or was unreliable. Whether we were using the wonderful Mu editor, uFlash or Grok’s web-based editor, we had very few problems. This process being easy and reliable is a huge factor in building student engagement. Additionally, using Grok’s micro:bit emulator let students experiment and iterate quickly, and the auto-marker allowed students to ensure that their lab work was correct and pointed them in the right direction if it wasn’t.

Using Grok’s micro:bit Crash Course in the lab.

We used MicroPython as the primary language for the summer school because it gives a great balance between simplicity and power. In comparison to C++ (which is used on many other similar devices, such as Arduino), the ability to get understandable error messages including traces and line numbers, removes a huge part of the frustration of getting your program working. We’ve watched students in the past being forced to litter their code with Serial.println to narrow down where the program fails (if it even reliably fails at all!), whereas on the micro:bit, errors like “IndexError on line 20” mean students know exactly where to look.

When combined with the MicroPython REPL (the Read Eval Print Loop), which allows the student to get an interactive Python prompt on the micro:bit itself, students can try out and explore what a piece of code will do without needing to upload a program, or even pause the current program and explore the value of variables and call functions. This facilitates a really useful way to learn by experimentation. We encouraged the tutors to ask questions like “what do you think will happen if…?” and the students could very quickly experiment.

We also explored the MicroPython ecosystem, including using the “pyboard” which has a much more powerful CPU and the ability to run much longer programs. One of the most popular exercises in the labs was to use the pyboard’s “HID device support” where it can pretend to be a USB keyboard or mouse. A few students also used the ESP8266 to add WiFi to their projects, although the additional time required to cover socket programming and run a server proved to be a little too much for the 10 days. It’s great that the MicroPython ecosystem allows the same code to be written and run on a wide variety of different devices, from micro:bits to satellites.

Python errors are easy to understand and fix!

Adding electronics to the mix

The next few days were spent learning some basic digital and analog electronics — voltage, current, Ohm’s law, LEDs, buttons, pull up/down resistors, capacitors, motor control with H-bridges, ADCs, DACs, opamps, communication protocols (I2C, SPI and UART), digital logic gates and integrated circuits (such as the venerable 555). The labs involved building circuits with breadboards and interfacing with devices such as OLED displays, a myriad of sensors including flex sensors, light sensors, keypads. The students also built a simple resistor-ladder DAC and played with the output on an oscilloscope, and then repeated this with an I2C DAC. We also had a lot of fun with many metres of NeoPixels and DotStars.

We then introduced line-following robots using a robot kit from Kitronik, and the students wrote code to control the robots and explore the behaviour of various control algorithms.

Making micro:bits talk to each other

The micro:bit includes an on-board Bluetooth radio transceiver. Due to device constraints, the MicroPython port doesn’t actually include the software to operate in Bluetooth mode, rather you can just transmit and receive short broadcast messages. This sounds like a limitation, but it turns out to be one of the micro:bit’s best kept secrets. By forgetting about Bluetooth, we didn’t have to worry about pairing, managing masters + slaves, or any high-level Bluetooth concepts. Any two (or more) micro:bits on the same channel can exchange messages, and there are 100 channels to choose from. An interesting classroom exercise would be having students devise a system where no two groups chose the same channel.

Simple radio demo — sender detects button presses and receiver shows them on the display.

One limitation is that because MicroPython mode doesn’t support full Bluetooth functionality, the micro:bit can’t pair with a PC or Phone/Tablet. However, you can communicate with a PC by using another micro:bit plugged into the PC via USB and a simple Python program running on the PC to access the serial port.

Compared to using a Bluetooth module (e.g. with Arduino) the micro:bit radio was a huge success. The students had no trouble at all building activities that involved 10+ micro:bits, all communicating simultaneously with each other. A few students even experimented with building a mesh network and were able to successfully get micro:bits that were individually out of radio range of each other to still talk via the devices between them.

Project time: micro:bit robo-olympics!

The final challenge was for students to implement their robo-olympics activites. The specification was fairly broad: students had to create an exercise-related activity where two players “compete” head-to-head. The exercise would be detected by a set of micro:bits communicating over radio (e.g. using the flex sensor, accelerometer or compass), and the score or progress would be indicated by a display or a robot moving. Some students chose to combine the two together — i.e. the exercise actually involved controlling the robot directly.

Again, the radio proved fantastic. Being able to attach 6 micro:bits to a player’s arms, legs, head, etc. and not have to worry about connecting wires simplified program development and debugging, and opened up lots of opportunities. Players (and robots!) were free to move around — rather important in exercise games! If there was a problem, a replacement micro:bit could be used without worrying about pairing — just upload the same software and it was ready to go!

Another key part of the project was the non-computing side. A huge variety of craft supplies and hundreds of metres of masking tape went into creating some incredible physical interfaces, including sensor-laden vests, mazes for the robots, dance machines, control panels and displays.

Final thoughts

The students were engaged, the tutors were happy and there were blinking lights everywhere — embedded programming with the micro:bit was a success because:

  • Students were more confident experimenting — they were able to try new ideas faster because hardware and software problems were easier to debug.
  • Teaching first Python, then electronics, was a win.
  • The micro:bit radio allowed students to build fundamental skills in something that’s quite complex.
  • Finally, the micro:bit is ridiculously cute and engaging. Students were hooked from the very first “Hello World” style smiley face.
happy.py :)

Thanks to WiseTech Global, for giving each NCSS student a micro:bit to take home with them. And thanks to the micro:bit Foundation for donating a set of micro:bits which allowed us to have enough spare micro:bits to have a lot of fun with the radios.

Check out Grok’s micro:bit resources at https://groklearning.com/microbit

Grok Learning

Articles about coding by the team at Grok Learning &…