It’s alive!

I’ve been fiddling for the last couple of weeks now, on and off, playing with my 25 year old Lego (thanks Mum & Dad for saving it for the grand-kids – I’ll let them have it once they’re old enough!). I’ve been using Lego kit 8860 which is Lego Technic kit (no longer available, I think) which amongst many other things built a car chassis with steering, gears, adjustable seats, and engine with moving pistons. The Turtle luckily was much simpler…

The only fiddly bit really is how to attach the motors with 5mm shafts to the lego “+ shaped” shafts.  Proto-pic do an adapter for 3mm shafts, so I have a couple of those, so I just needed to carefully drill them out to 5mm.  One of the few times in my life I wished I had a lathe!

Turtle Power!

A headless Turtle really needs to be tail-less too.  It’s hardly free to roam with a mains lead shoved somewhere indelicate, is it?  It’s been a little tricky and so costly getting this to work, so I’ll share with you the final magic ingredients…

Lithium Ion battery 7.4v 2.2AH – why?

  • Even without monitor, keyboard and mouse, RPi boots and then dies without a 3.3v and 5v supply even though the 5v isn’t used for much outside the GPU
  • Lithium Ion cells produce 3.7v, so you need to get a couple in series to get >5v required
  • I bought 2.2Ah batteries to give me > 2 hours running the Turtle between charging
  • So 7.4V 2200mAH it is, but I don’t want to just warm up the ecosystem with the extra  voltage over 5v, so…

switch mode regulator is needed as the most efficient way to convert 7.4V to 5V at up to 3A without producing 7W of heat –  the 5V powers the stepper motors as well as pieces of Pi and they can draw > 1A each if they’re in a mood.

Then I need to take the 5V 3A DC, and tap off a 3.3V @ ~700mA to run the bulk of the Pi – that I used a Low Drop Out regulator as the voltage drop was 1.7V and the current < 0.7 =>  1.2W heat generated – the regulator doesn’t even start to get warm.

Put that lot together and this is what you get – a well stuffed breadboard, to the extent that I bought many heat sinks and inductors while trying to find a combination that would fit on the breadboard.

A headless RPi

TurtlePi, powered from a Lithium Ion rechargeable 7.4V battery with just WiFi to the outside world

P.S.  A little logical thought (too late) meant I realised the 3.3V regulator was unnecessary. The 5V regulated from the battery was fed via the GPIO pin 2 to the RPi 3.3V regulator, thereby feeding 3.3V back to the breadboard via GPIO pin 1.  Which is lucky because I was running out of breadboard lines, but this frees a couple.


Talking to a headless Turtle

So how do you talk to a headless turtle?

That’s the problem I wanted to solve.  A “headless” RPi had no screen, keyboard or mouse, meaning the only way of talking to it is over the wireless network I have here (as do most people with an internet connection).  The way I want to ultimately achieve is to connect the Turtle to SupeRpi with a dedicated TCP connection using Python on the SupeRPi to take commands from its keyboard, and pass them to the Turtle for it to carry out.  By commands, I mean “forward 10”, “rotate clockwise 90”, or “reverse 20”.

TCP connections are set up between a unique pair of IP addresses.  Each network card has it’s own unique one.  In my RPis, each has only one for the wireless link – SupeRPi is, and the Turtle is  These addresses aren’t (in my case, yet) configured on the RPi – the RPi learns them over the wireless network from the dhcp server.  This runs (d)ynamic (h)ost (c)onfiguration (p)rotocol which assigns IP addresses to each network device it hears from on the network.  It is possible to ensure the dhcp server assigns the same device each time it starts up, but in my case, that’s not necessary as mine tries to assign the same IP address to each network device across each time it reboots.

So how do you find out the IP address for your device?  I ran “ifconfig” which lists all network interfaces, and shows their IP addresses amongst other stuff).  Having found the 2 addresses, I check they can talk to each other by using “ping”.  Typing ping from the SupeRPi (  to Turtle ( like this “ping” tries to find every few seconds, printing the results on screen.

So now I have IP addresses, and proof they talk to each other, the next step is to be able to send Turtle commands from SupeRpi.  As I said previously, I am going to do this with more Python code, but first I remembered a quicker way: “rlogin”.

From SupeRPi, I can type “rlogin” and after typing the password for the Turtle, the screen actually acts as though it’s on the Turtle, so typing “sudo python” from the SupeRPi rlogin screen sets the Turtle running (well crawling actually, but…).

So the next step has changed.  I need to guillotine the Turtle (no harm was made to any animals), and then I can get back to the electronics of powering the Turtle without the mains – well within the bounds of a Lithium Ion battery, but outside of this months pay-packet.  Lucky payday is imminent so I hope my next post will be less wordy, and more interesting.  Until then…

Headless Chickens / Turtles & SupeRPi

Been a bit quiet for the last few days as I’ve been looking onto what’s required to liberate the Turtle from it’s wires, and also to wirelessly connect to another RPi for guidance/ direction.  I now know where I’m going and how to get there so hopefully I’ll share that with you as I progress.

On a separate thread, I’ve just bought the MPEG-2 license so I can use my Turtle server as a desktop DVD player too.  Again I’ll let you know how that goes

The wheels on the Turtle go round and round, round and round, round and round…

OK, jumping the gun a little as actually it’s the motors which are now spinning; I haven’t attached the wheels yet. As a result the photo below is hard to actually believe the motors are spinning @ 1.8 degrees per second, but you can see from the power supply they are taking ~1.5A @ 3.3V doing something!!! I did a couple of speed tests yesterday, and I easily reduced the delay per step to 0.01s and without prints in the code, everything ran pretty quickly ~100 steps a second, or roughly 10cm/s or 0.36km/h. The motors are from here.

Circuit layout with steppers

LEDs swapped for ULN2801a + Steppers

The motors spinning

You can’t see the motors dancing, but you can see the current they are drawing

Now what? Well don’t hold your breath, as I need to wait for another paypacket to put in another order with Farnell, and then I’ll have a joystick to control the Turtle with. After that, another wait for another RPi to arrive to I can set the Turtle free, controlled by wireless and powered by it’s own 6.8AH Lithium Ion rechargeble battery, and once that’s done, the sky’s the limit (literally!). Excited? Me? You betcha!

Turtle python code

Here’s the Python code so far for driving the LEDs representing the two stepper motors’ incremental steps. It starts up the system with the first three GPIO ports as outputs (to drive the 74HC595), with the remaining four set to outputs.

Then it has a “for” loop, lighting up LED 1,2,3,4 in a repeating sequence. Those inputs of 1,2,3,4 are then mapped to setting bits 1-4 (i.e. 1, 2, 4, 8), and then the results for “left” and “right” wheels are combined to make (left + 16 x right). This is “fed” bitwise into the 74HC595 data port (GPIO pin 11). Each bit update is then accepted by toggling the GPIO port 12 (a “chew” event), before finally “swallowing” (GPIO port 13) which lights up the LEDs to match the 8 bits (4 each per wheel) previously fed in.

Finally the code sleeps for a second, so I can check LEDs against the prints on screen, before moving on again.

My apologies for lack of comments in the code. I’ll get round to adding this once I stop adding this first batch of features.

Virtual Turtle python code

Virtual-turtle photos

Just a couple of shots showing the set up of my virtual turtle.

The RPi itself is a B, 256MB RAM, not made in Britain. I have two GB ones on order to offset my guilt! It’s powered from the USB port – I went for a good one (11 ports, mains powered) to limit the number of cables & wiring I had. RPi is running 2012-09-18-Wheezy, and that has worked beautifully with the Edimax Wi-Fi dongle, which is relatively cheap, tiny and works well!

My overall setup

My “workspace”

Here you can see in a bit more detail, the GPIO port wiring – note the LEDs / breadboards are powered from the 3.3v output from the GPIO port – hence my choice of a quality USB port to power the RPi

RPi, 74HC595, +LED simulated stepper motor coils

The setup of the virtual turtle, with yellow LEDs representing the left stepper motor coils, and red representing the right

Turtle Pi

Back when I was a teenager, 30 or so years ago, I had a BBC micro and I used it to built a turtle driven by the BBC computer in Basic. By command, it could move forwards, backwards, spin on its axis, and drop a pen into contact with a paper surface to allow it to plot its path, allowing it to draw pictures.

30 years later, I’m reproducing what I did in my teenage years, but this time with a RaspberryPi controlled from Python via the GPIO ports. Here’s the plan:

  • get my RaspberryPi setup – [DONE] couldn’t be easier for an IT engineer like me, thanks heavens!
  • teach myself basic python – [DONE] a combination of the RaspberryPi user guide combined with this quick reference I found
  • teach myself how to use the gpio port using python – [DONE] starting with the RaspberryPi User Guide
  • extend the basic GPIO code to flash 7 LEDs – the total number of easy accessed GPIO I/O pins [DONE]

Then my first problem. A stepper motor used to drive the turtle has 4 control wires. How to direct 2 motors (8 wires total) when I have only 7 available from the GPIO? Enter the 74HC595 serial in, serial or parallel out latch. This means with only 3 pins, I can drive both stepper motors exactly as needed, leaving me 4 pins to do something else (for example, it’s possible to get joystick switches to provide the inputs to make the Turtle go forward, backwards and rotate. On we go…

  • read and understand the spec for the chip – [DONE]
  • write the python to drive it – [DONE]
  • try it out, using LEDs to represent the motor coils – [DONE]

I’m pretty pleased with progress so far (even though I do say so myself), since I only got my RPi 2 weeks ago. So what next…

  • build a body for the motors in LEGO – [IN PROGRESS]
  • fit the wheels which I kept from my original Turtle to the steppers
  • swap the LEDs for the motors – this means adding some buffering and power as the motors (unlike the LEDs) cannot be powered off the GPIO port
  • add joystick control
  • use a second RPi to control it wirelessly – currently the initial plan is for turtle to be tethered to an immobile RPi via a very long ribbon cable

As a man approaching middle age, I’ve not been this excited since I was a teenager, and even better, doing this means I can help my kids learn to use their own RPi’s when the time comes (they are a little young at the mo!)

I intend to get some pictures up here soon showing progress so far.