Imp Control of Lego Train

Hi all,

I was impressed to hear Hugo presenting the Imp at the BLN Internet of Things event and have duly taken delivery of an Imp + Impee today.

The kids and I have been playing with PWM control of Lego Power Functions. Originally we were prototyping it up with a Raspberry Pi but this is too power hungry for what we need. The Imp, however, fits requirements very well!

I thought you might enjoy seeing what we came up with this evening…

Electric Imp WiFi Control of Lego Power Functions Train Motor

The video demos an Android application (just a slider bar and a stop button) which POSTs up to an Imp Cloud HTTP input.

The control values are then pushed down to an Imp which is outputting two PWM signals to control the train motor.

No need to wait for Lego Mindstorms EV3 any more!


Alex Lennon
Dynamic Devices Ltd

Oooo, which lego kit is this? I don’t remember seeing lego trains before. Is it designed to take a PWM input or was there hackery involved? :slight_smile:

You should get on the agent beta then link the train speed to, say, the temperature in Paris…

It’s surprisingly straightforward Hugo :slight_smile:

There’s a Lego City passenger train which comes with Power Functions components - battery box, IR transmitter, IR receiver and train motor.

There’s also a special lego power functions wiring connection which has four pins, +V, GND, C1 and C2. C1, C2 carry PWM to the motor, which I believe also takes power from the two control lines. See here.

The motor is controlled via PWM at a frequency of 1150Hz, as shown here.

We didn’t need to use the IR receiver, instead breaking out the C1, C2 signals from a cable. These are taken from the output of an H-bridge (I suspect it may be possible to use the Hannah for this).

I took two GPIO pins as input to the H-Bridge and with a little Squirrel code as here could control the PWM.

Lastly we just needed to use the planner to wire up two HTTP input endpoints which the Android application posts to, as in the attached image.

Having proven this out with the train, the next step (when I have some free time!) is to control this Lego Technic 4x4 :slight_smile:

Email and SMS agents? Yes that’d be interesting to look at. Can we get me signed up?

Hi Alex,

Great project!!! I bought an IMP yesterday. I wanted to buy a arduino wifi shield when I saw the IMP on Adafruit. I wanted to control my 4x4 crawler with arduino but can’t wait until my IMP arrives. I think IMP has a lot of possibilities!

The 4x4 crawler has a servo for steering and I am very anxious to get it working with an IMP :slight_smile:


Hi Alex,

What h-bridge did you use? Do you have a simple scheme of your great project? I’m trying to recreate it but I’m a newbie on electronics So I’m really struggling here:-((.

Thanks, thanks, thanks,


Hi Jeroen,

I used a bridge purchased on eBay, this model if I recall

I haven’t had time to put any more details together I’m afraid!

Thanks Alex for your quick reply.

I ordered a H-bridge L298n driver.

Now I have to wait till it arrives. Thanks again for your help!


Great - if you take a look back at my earlier post I think the links I provided there should give you a starting point for schematics and code. Good luck! Let me know how you get on :slight_smile: