A few days ago I was seduced by an offer for a CodeBug.
From the CodeBug.org.uk website:
“CodeBug is a cute, programmable and wearable device designed to introduce simple programming and electronic concepts to anyone, at any age. It is easy to program CodeBug using the online interface, which features colourful drag and drop blocks, an in-browser emulator and engaging community features. Create your own games, clothes, robots or whatever other wacky inventions you have in mind!”
It can also connect to a Raspberry Pi. Well how could I resist. All this for only £15. It was too good to be true, so I bought 2. They arrived on Monday. Today is Friday. Here is my:-
CodeBug(s) arrived. Snazzy box. Unwrapped and took a look. Yes, it looks just like the picture. And comes with a USB cable, and a short instruction leaflet.
Visited CodeBug website and followed the get started instructions, watched the short intro video, which at less than 2 minutes shows you in real-time how to write your first CodeBug program, test it, deploy it to your CodeBug and finally run it.
(I didn’t believe the hype. This can’t be true. I tried it. It was!)
Within 10 minutes of opening the box I had a program up and running on the CodeBug. I understood the basics and had experienced a new programming language – Google’s Blocky (Err, actually Blockly – oops. Post updated.)
I thought, 10 minutes. Is that all the fun there is to be had from a CodeBug. Done that. Next.
But, hang on. The CodeBug has two buttons on it, which respond to program control. So my next program included a bit more logic, and used button presses to do different things. Displaying text on the 5*5 LED display. (Amazing to think that a 25 pixel display can actually display text.)
Then I wrote a program to animate a dancing bear. A very simple dancing bear I admit. This time using loops as well as button press logic.
I was beginning to be impressed with this little £15 device. So why not go further. On the website, which is clean easy to use, and has a professional feel to it, there are some learning examples. Next up is a Fruit Keyboard.
I only had apples. So it was going to be a single fruit keyboard. A single key keyboard.
To make this ideally you connect a wire with crocodile clip ends to the CodeBug and the apple. I only had a jumper lead, so I snipped off one end, and wrapped the wire around one of the 4 input / output terminals. Then pushed the pin end of the wire into the apple.
I modded the program to display apple. The tested the program in the on-line emulator, to ensure that it compiled first, and second worked correctly. Happy with a quick test, and desperate to try it in the real world, I downloaded the program to the CodeBug and set it running.
Every time I touched the Apple, “Apple” was displayed on the LED display – scrolling across. This was the most immediate programming feedback I have ever had. I was very impressed at the simplicity of the CodeBug, yet how powerful and flexible it was, combined with a visual programming language and emulator, including cloud storage for all of my CodeBug programs.
This has been well thought through.
But there is more. A code bug can be tethered to Raspberry Pi, and using Python programs running on the Pi you can control the CodeBug. Effectively a program is downloaded to the CodeBug which then works as a client, whilst you run Python programs on your Pi – server, and communicate through the USB cable to your CodeBug. Client Server computing. With a CodeBug. Amazing.
I have to admit here to my first downer with the CodeBug. It plugged it in but it didn’t work. I spent a lot of time trying, but absolutely nothing was happening. I was obviously doing something wrong, but couldn’t work out exactly what. I was using a Motorola LapDock, powering a Model A+ raspberry Pi.
There are 2 ways to tether a CodeBug to a Raspberry Pi. Tethered, and Tethered with I2C, one of the interface standards supported by the Pi. The CodeBug also has expansion pins on its base and can be plugged direct into the GPIO pins on the Pi.
Nothing I tried worked, so I slept on it. Thinking that a bit of rest might help. Before retiring though, I ordered some CR2032 batteries, because the CodeBug can be battery powered so it can work without a computer connection – it really is quite an amazing little device, and I ordered a USB Cable with a switch on it so I didn’t have to keep on plugging it in and out to reset and download programs.
The power of sleep is amazing. If you have a problem, then stepping away from it for a while can sometimes help. Meanwhile in the background your subconscious carries on working on your problem.
Wednesday morning I decided to rebuild the Pi OS. I started with the latest Noobs (pi speak) build. And try again. So after installing Geany, a lightweight IDE for writing Python I was ready to go.
I plugged in the CodeBug to the GPIO ports on the Pi, then turned on the Pi. Before the Pi had finished booting a dancing bear was showing on the CodeBug LEDs. This made me realise that I had forgotten to download the tethering program, and that I probably needed to read the instructions a little more carefully.
In rereading the instructions I realised that tethering comes in two flavours on the Pi. Firstly, and I had missed this, partly in my haste, that the first tether mode I was attempting was using a USB cable. However I had plugged the CodeBug into the GPIO pins. So USB tethering was never going to work. But i2C tethering through the GPIO pins should have worked. So the software refresh wasn’t a waste of time, but I also needed to read the instructions more carefully.
There were 2 references to connection via a USB cable. In a picture, that doesn’t actually show a Raspberry Pi, and then on the last line of the overview text of the example.
The examples are very good, and well documented. I admit I rushed, and didn’t read the single reference to USB tethering.
Lesson learnt: Use the latest build, and read the example text – thoroughly. Yes, I think that the USB tethering line could have been more prominent in the example, but there was a picture as well, and I had missed 2 clues!
Once plugged in, via a USB cable, I followed the example python scripts and all was well.
Python code to scroll text on CodeBug
But that is the thing with CodeBug, you then think, “well I want to scroll some text like Blockly does“. Ah ha. When you write in Python the CodeBug tether library does not provide a scrolling program. So you have to write your own. An example is given, but it is more fun working it out from basics. The way you get text to scroll is by adjusting the pixel map start point. You display the same text over and over again, just decrementing the starting column, from 0, down to a minus value that is 5 (for five columns you have to scroll across) times the number of characters in the string. Therefore “Hello CodeBug!“, is displayed from column 0, to column (0 – (14*5)= – 70), in decrements of -1.
(NB. Longer lines of text seem to scroll slower!)
CodeBug connected to RasPi GPIO
Then it was on to trying out the direct GPIO connect and the I2C interface. This means connecting the CodeBug directly onto the GPIO pins of the Raspberry Pi. Something that should always be done with care. And note that you should never connect power from the GPIO Pins, and either battery or USB. There is a warning on the page, but others seem not to be reading all of the text, just like me . . . .
I followed the scrolling digital CodeBug clock example. The python program gets the current time and date from the internet, then formats the time and date outputs and sends them to the CodeBug, scrolling them across the LED. I slightly modded the program to display the short month rather than digits, (%b not %B). On my Model A, it displayed the Time then Date almost exactly twice a minute. To within a fraction of a second.
I only experienced one small problem. I set the scrolling time parameter to 15 (15 seconds), not .15 (point 15 of a second) which meant that the display stood still for 15 seconds and would have taken approx 20 mins to scroll the time string! Ooops.
I was pleased that I had overcome the initial problems, software and not reading the instructions properly. And now could think of lots of things to do with the CodeBug, RasPi and Python. I think tethering via USB might be more useful than via the GPIO pins, but it is a useful capability.
Being quite taken with Blockly I decided to investigate further. Following the links I eventually came across the Google Developer Pages for Blockly and the instructions for installing Blockly on your own machine here.
Now thinking about how to get Blockly onto RasPi, and get it to generate Python code. This sounds like fun. Start small. One step at a time. Then when I have learnt enough, completely revise the architecture and start again 🙂
The CR2032 batteries arrived meaning that the CodeBug can be used without plugging into a computer. This just worked!
The USB leads arrived. I had bought the wrong ones. No data cable! They only provide power to the CodeBug, but don’t do data exchange. Oh well. Out with the soldering iron to make the lead I really wanted. Should have read the small print. These will be recycled to use with Raspberry Pis.
Write this blog post as a way of documenting the CodeBug experience and start to think of other things to do with the CodeBug. I am definitely going to try the ‘get coding in under 2 minutes’ out on some friends 🙂 And the fruit keyboard – the tactile immediacy, having written the code, seeing all the wires, is great fun.
And I am thinking of doing more with my CodeBug. I bought 2. I gave the other one to my wife. She’s got the bug as well! In fact she got going a little quicker than me. Obviously reading the instructions more carefully!!! A lesson to be learnt there no doubt?
One project I want to try is to tether 2 CodeBugs to the same computer, then scroll a message across both CodeBugs, so that it appears as if the text is crossing from one to the other. If I get that going I will add a video, so watch this space . . . .
I am very impressed with the CodeBug. It is immediate. It is tactile. It is fun. But I am not only with impressed with the device, but also the complete environment surrounding it. The interactive, visual, development environment, including compiler and simulator is great. The Cloud storage of your code is excellent. The price, £15, is fantastic. The examples are easy (ish) to follow – as long as you actually read them and not skip ahead. It is a well designed system, which just works.
If you have read this far and haven’t already ordered a CodeBug, then stop reading, and buy one now.