Meet the LegoDuino

A couple of months ago, my 9 year old son took an interest in my electronics projects. He wanted to do some experimenting of his own, so I gave him a bread board, a couple of leds, resistors, buttons, switches and some batteries. Then I drew him a circuit diagram and explained what it all meant and challenged him to make the leds light up. So, after a while (read about 15 minutes), he proudly showed me his glowing leds. So I drew some more diagrams, did some more explaining and on he went. After a few days he started to draw his own diagrams and building them. Sometimes he failed but a lot of times he succeeded. A few weeks later he said he wanted more…. He also wanted to use those little black thingies (i.e. ATMega's) I was using.

Unfortunately, that wasn't as easy and I had to disappoint him at that time. But, an idea was born. I wanted to create a kids friendly micro controller. Something he could create the most amazing things with. I made a list of requirements:

  1. one self contained block
  2. it must be easy to use in something my son can build.
  3. ports must connect to all kinds of IO (leds, sensors, motors, servo's…)
  4. connecting to the IO ports must always be correct (i.e. reversal of the connector must be impossible or no problem)
  5. my son must be able to program it himself

For the first two requirements, that is easy. Of course it should be a lego block containing the micro controller, the batteries and all other components needed to make it all work in for instance a train, car or robot.

The thrid en fourth requirements were a little more difficult. I needed to make connectors on the outside of the block and these connectors must be polarized. I managed to find some connectors that matched my requirements.

The last requirement was a problem. AVR Studio an C programming obviously will not do and even the arduino IDE is not suitable for 9 year olds. Then I encountered an article on Minibloq which is exactly what I was looking for. So now I had all ingredients for this great project. It's time to start building some components and do some assembling.



The main board

The main board consists of an ATMega328, an nRF24L01+ and some other components to make it all work. The ATMega is running at 5V while the nRF24L01 requires 3.3V. However, I needed the IO pins to run at 5V and although the nRF24L01 requires a lower supply voltages all interface pins can handle 5V.

There's a protection diode and a stabilizing capacitor. These were added to assure that the microcontroller keeps running even when the power drops shortly as result of inductive loads. The stepdown converter used in the power module had some problems with that.

I got these nRF24L01 modules but there are cheaper and even smaller alternatives when you by a pair.



The IO ports and peripherals

This part was a bit of a challenge. It should be easy to connect all kinds of peripherals. Simple peripherals like LEDs, push buttons, LDRs. But also more complex ones like servo motors or a ping sensor. I also wanted to be able to connect lego motors because these are not to expensive and provide an easy way to make things move with lego.

Although it must be easy to connect things, it should be impossible to do it wrong. So I needed connectors that could not be connected in reverse (or it should not matter). I thought about using RJ11 connectors because they are cheap, easy to use and can only be connected in one way. But these connectors are quite large. I decided to use polarized headers. I placed two 3 pin headers above each with one space in between. Each 3 pin header has a ground pin, a 5V pin and an IO pin. This way I can connect three pin connectors using one IO pin or I can use a 7 pin connector that has two IO pins available.

The connectors are placed in such a way that the serial port as well as the I2C port of the ATMega328 are available using 7 pin connectors.

Another challenge was to make sure the connectors would stay in place in the enclosure. I took two 2×2 blocks for each side and cut a verical slot in them. I used a hobby knife for this. I made the PCBs for the connectors a little wider and these now fit the slots. This way, the connectors will not move.

One important detail on the connector prints is the diode that is in reverse between the 5V and the ground. This is a clamping diode to protect the power supply from inductive loads like servo motors or relais.







For the lego motors, I wanted to be able to connect them without modification. I wanted the same connectors that are available on the lego power block so the LegoDuino could be used to control the motor that's part of the train set and still be able to use the remote control that is part of the train set. So I bought an lego extension chord (Lego 8886) that has two suitable connectors. The cable has four wires. I cut the cable in two and stripped the ends. The middle two wires are the ones used by the motors. Lego motors require 9 volts. So the 5V of the IO pins is insufficient. Therefore, I've added a motor driver (L293D). Some more detail about that is in the section about the power module.

To make the lego motor connectors fit, I had to cut of a few studs (more details on that in the enclosure section) and to make sure the lego motor connectors stay in place I glued these bricks together. Now I have a nice lego brick on which I can connect two lego motors.



And here are some photo's of my peripherals



The battery holder

The LegoDuino takes three AA size (14500) lithium cells (I got these). A 6 stud wide lego plate is about 48mm. AA size batteries are 14mm; so 3 batteries are 42mm wide. It should fit within the 6 wide lego enclosure, however battery holders are all much wider and simply won't fit. So, I created my own battery holder that would fit exactly within the enclosure.

I started with a normal 4 cell AA battery holder, some prototyping board and some office supplies. I cut the metal strips of the paper binders in half and sanded down the ends. Bending the ends 90 degrees makes very nice place holders with a length of 60mm. The right size for the battery holder. Furthermore, these metal strips are nice conductors, so at the same time, they can be used to connect the poles of the batteries to create an 11.1V supply.

The pieces of prototyping board are about 43mm wide and 23mm high. On one side I soldered the springs I cut from the battery holder. On the other side, I soldered three pieces of solid wire I bend into a little spiral and filled them with solder. After wiring the battery poles, I soldered a 2×8 pin header beneath the battery springs. This header is used to plug in the main board. On the other side I soldered another piece of prototyping board again with a 2×8 pin header to connect the power module.

One lesson I learned (after burning my fingers to the metal spring of one of the batteries) is that lithium cells can generate high currents when shorted out. Therefore it's important to wire the battery holder as shown here. Because the enclosure covers part of the battery holder, it's much harder this way to short out one of the batteries while replacing them.



The power module

The most complex part of the LegoDuino is the power module. This module must of course allow switching the module on and off again. I also want the micro controller to be able to switch the module off programmatically so the batteries won't drain when my son forgets to turn the LegoDuino off. Powering off must also be possible independent of the micro controller bacause if it does not work anymore, it must still be possible to power off (other then removing the batteries). I found schematics for a "push on, hold off" circuit. I added a power led and a kill switch for the micro controller, a step down converter for the 5V power supply and a motor driver for the Lego motors.

The motor driver is an L293D. It is very easy to use but it has a pretty high voltage drop. About 2.5V. Starting with 11.1V passing a few diodes and the motor driver results in about 8V for the lego motors. When the lithium cells are fully charged and the voltage on the input exceeds 12V, the lego motors will be at about 9V. A nice and safe voltage for these motors.

An interesting challenge in this circuit is the dual mosfet IRF3719. This is an SMD component. Fortunately, pins 5 and 6 are connected and can be soldered on one pad. The same applies to pins 7 and 8. The other side of the chip is a little more difficult. So I bend pins 1 and 4 up. Now I can solder pin 2 to one pad and pin 3 to another. Pin 1 and pin 4 can now be connected using wires.

The power module consists of a lot of components on a small board. I used both sides of the board to solder components. I started with a few components on the component side and then soldered the mosfet on the solder side. From there I added all other components finishing of with the step down converter. The photo's give an impression of the build.



The enclosure

The enclosure for the LegoDuino is based on a 16×6 base plate, three bricks high and closed with two plates. A 6×6 over the power module and a 6×8 over the battery compartment. To make the IO connectors fit, I had to cut of 4 studs on either side of the base plate. I used a hobby knife to remove the studs. Putting the knife flat on the base plate against a stud and then rotating the knife a little bit horizontally while applying pressure did the trick. Note that these knifes are sharp and that you have to apply a fair amount of pressure. So be very careful not to cut yourself and that the base plate is stable when cutting.

I also removed a little material from the side plates to make a little more space for the battery holder, drilled holes in the top plate for the power button and led, cut of some studs to make the lego motor connectors fit and a few holes to pass the cable through. And of course, I made slots in the bricks on either side of the IO ports.

I know, it's almost a crime to damage lego bricks and it even gets worse because I glued the side together and blocks that hold the motor connectors. But when you look at the result, maybe I can be forgiven.



The programmer and bootloader

To make programming as easy as possible, I added an nRF24L01 module to the LegoDuino. This way it's possible to program the LegoDuino wirelessly. This required two things.

First, I needed a bootloader in the LegoDuino that uses nRF24L01. I searched the internet for a while but could not find anything suitable. I wrote my own. I use a simple protocol sending a packet with a command and some data and returning an acknowledgement. I use the mirf library to interface with the nRF which makes receiving and sending small data packets very easy.

The second part is a device that connects an nRF24L01 to the PC. I decided to make it easy on myself. I took an usb to serial converter, an ATMega88 and an nRF24L01 and combined these into a nice dongle. Then I modified the stk500v1 bootloader code and replaced of the sections of code that actually program the device by code that send packets to the LegoDuino bootloader using the nRF and wait for the acknowledgements. Now, I can use avrdude to program the LegoDuino.

As you can see in the circuit diagram, I used a 32KHz watch cristal. These are very cheap and I happen to have plenty. In the firmware I added a OSCCAL calibration routine that sets the internal RC to 7.3728 MHz which in turn divides nicely to an 115200 baud rate. And to make my life a little easier, I added a real stk500v1 bootloader (modified with OSCCAL callibration) on the ATMega88 so I can easily upgrade my nRF programmer. Sources and .hex file are available for download here.



Minibloq

Minibloq is a graphical programming environment for Arduino like boards. I downloaded it and found that I could add my own blocks relatively easy. It also seemed possible to add my own board but the boards are actually hard coded into Minibloq. So unfortunately, adding a board requires recompilation of the Minibloq program. Also when a programming block requires compilation, the minibloq program needs to be modified. Fortunately, the source code of Minibloq is also available. First I added the LegoDuino as hard coded sections in Minibloq. But each time I needed to make a relatively small change, I needed to recompile Minibloq. I decided to fix this and made a some changes to Minibloq enabeling me to add boards and programming blocks without the need to recompile Minibloq. I gave my changes to Julián da Silva Gillig of Minibloq and he posted them on the forum. But even better, Julián is currently working on a version that not only solves my problem with recompilation but also is much more versatile.

For the LegoDuino target in Minibloq, I copied the ArduinoUno target. I modified minibloq.h to use my defined port names as well as the minibloq target board definition (main.board). I also added some of my own programming blocks and the Arduino NewPing sensor (instead of the existing Ping sensor). I also copied the DCMotor programming block and modified it into a LegoMotor programming block. And I added some special power button en LED support so the power button can be used to turn the LegoDuino on and off. But also to power off the LegoDuino after a fixed time. This way, the batteries will not be drained when my son forgets to turn it off.

You can download the modified version of Minibloq for windows. This includes my LegoDuino target board. I also tried to compile Minibloq for linux but I needed a modified version of a library which I haven't been able to obtain. I gave up on this because the windows version is sufficient for me.



The final result

And this is what my son got for his birthday



What's next

Build anothor one because two controllers can do more spectacular things than one

More peripherals (a sound module, microphone, …)

Create an nRF24L01 remote control for the LegoDuino

Easier construction of the LegoDuino. It should be a single PCB. It could use a custom case. I saw someone who drilled holes in the casing. Using the correct diameter hole and the correct distance, you can use Lego technics to build around the LegoDuino

Unfortunately all of this is fully dependend on my very limitted resource: Time. So who knows. But maybe someone else can try some of these ideas