Tag Archives: Lego

A Saturday hack

Saturday morning we found an old Lego RC car. Actually Duplo car to be precise. The relevant thing is that it had two internal DC motors and we had two IRF520 boards from Keys.
These boards are essentially driver boards for a MOSFET transistor. In short they allow the Raspberry Pi to switch the motors on and off. And also to control the motor speed py PWM (but unfortunately not direction). Freely shipped from China the IRF520 is less than two Euros pr. piece. Versatile and fun. So make sure to buy a few more than you actually need :o)

The idea was to be able to control the Lego RC car from Scratch using some sort of GPIO control. We quickly discovered ScratchGPIO5 which very quickly became or new favourite tool! It has lots of support for add-on boards for the Raspberry Pi and the possibilities seem endless. Imagine it works with the Adafruit 16-Channel 12-bit PWM/Servo Driver! So that we have to try out at some point.

ScratchGPIO only works with Scratch 1.4 but not 2.0 since the Scratch people for some reason have removed the “remote sensor” option from Scratch 2.0(???).

Next we quickly set up our Raspberry Pi as a Scratch Interface Device following the instructions on the ScratchGPIO5 site.

First step was to slaughter the RC car. Carefully since we wanted it all to work when we put it together again.

Then we soldered some longer red/black wires on to the DC motor terminals so we could put them where we wanted them later. Also wires from the battery pack + and – were soldered with wires in appropriate colours.

When this was done it was time for a test run before assembling the car again.

When we were happy with the result we could put it nicely together and this was more or less it. Done! :o)

Time for programming the basic features in Scratch to allow keyboard control over left/right/forward and speed.

Here is the final result. A nice little car which is easily controlled from Scratch. And there are still plenty of GPIO’s available on the Pi so we may just expand the world of the car some other Saturday.

Sunday hack: Now also with head lights which we can dim from 0 to 100%. Using the last MOSFET in the box, 2×12 V bulbs we had lying somewhere and a battery pack from a broken softgun.

We will NOT try and bring this throgh an airport security anywhere! :o)

Latest update: Now mounted with an ultrasonic sensor. This module is supported in ScratchGPIO5 and only takes up one GPIO pin. It is fairly precise and accurate. It gives a new dimension to the robot car since it can now “see” what is in front of it and react on it.

For the fun of it we are also streaming the data to plotly since we had an idea about monitoring and streaming the operational states of the robot car. But we are not exactly sure why. At least for no other reason than because it’s cool to do some IoT stuff and streaming of data to a graphing service on the internet. Could come handy at some other point.

The Pyhton script for doing this involves both connecting to Plotly and also connecting to Scratch from Python to poll some sensor data and stream these to Plotly. So others may find this interesting since this is how you get Scratch to plot whatever in Plotly. It does not need to be streaming data. So I uploaded the script here.