LEGO power functions with Arduino

LEGO Power Functions is a LEGO electric building system. You have battery boxes to deliver the power, different types of motors to make things move, light, remote control and even connection to a computer. The Power Functions system has 4 wires and uses 9V voltage.

The IR Remote Control sends infrared light messages to the IR Receiver much like a television remote sends a signal to a TV. The Power Functions IR System has 4 channels. On the Power Functions IR Remote Control and IR Receiver you have a channel selector switch.

Protocol:

See LEGO_Power_Functions_RC_v120.pdf for  information on the IR signals used to control LEGO creations: LEGO Power Functions RC Protocol The payload is: 1 toggle bit, 1 escape bit, 2 bits for channel switch, 1 bit for address, 3 bits for mode and 4 bits for various data depending on mode (maximum message length 16 ms). When a button is pressed or released on the transmitter the message is sent. Five exactly matching messages (if no other buttons are pressed or released) are sent.

Controlling LEGO power function system with Arduino:

GitHub – jurriaan/Arduino-PowerFunctions: Lego Power Functions Infrared Control for Arduino is a Arduino Lego Power Functions Library. It allows you to easily generate same IR signal that LEGO power function remote controller sends.

Arduino and LEGO® Power Functions – Hackster.io article describes a project that involves a LEGO Train that was controller by an Arduino.

There is Arduino and LEGO® Power Functions project for controlling a LEGO Train with Arduino and LEGO® Power Functions.

First attempt at driving a train! web page is a good tutorial how to start controlling LEGO trains with Arduino.

Here is some code (base on example from First attempt at driving a train! page) I successfully used to send control signals to LEGO train from Arduino with IR LED (needs legopowerfunctions library to be installed). This with make the train go forward for 5 seconds, stops for 1 seconds, goes backwards for 5 seconds. stops for 1 second and starts all over again:

#include <legopowerfunctions.h>
// IR led on port 13
void setup(){
LEGOPowerFunctions lego(13);
}

void loop(){
lego.SingleOutput(0, PWM_FWD4, RED, CH2);
delay(5000);
lego.SingleOutput(0, PWM_FLT, RED, CH2);
delay(1000);
lego.SingleOutput(0, PWM_REV4, RED, CH2);
delay(5000);
lego.SingleOutput(0, PWM_FLT, RED, CH2);
delay(1000);
}

The hardware needed is same as described in First attempt at driving a train! page:

1 Comment

  1. Tomi Engdahl says:

    LEGO-compatible Electronics Kits Everywhere!
    https://hackaday.com/2017/10/12/lego-compatible-electronics-kits-everywhere/

    Within the last few years, a lot of companies have started with the aim to disrupt the educational electronics industry using their LEGO-compatible sets. Now they’re ubiquitous, and fighting each other for their slice of space in your child’s box of bricks. What’s going on here?
    Raison D’Être

    The main reason for LEGO-compatibility is familiarity. Parents and children get LEGO. They have used it. They already have a bunch. When it comes to leveling up and learning about electronics, it makes sense to do that by adding on to a thing they already know and understand, and it means they can continue to play with and get more use from their existing sets. The parent choosing between something that’s LEGO-compatible and a completely separate ecosystem like littleBits (or Capsela) sees having to set aside all the LEGO and buy all new plastic parts and learn the new ecosystem, which is a significant re-investment. littleBits eventually caught on and started offering adapter plates, and that fact demonstrates how much demand there is to stick with the studs.

    LEGO already has the Mindstorms set, and they just released the Boost robot for helping teach coding, so one would think this a dangerous market in which to exist.

    Reply

Leave a Comment

Your email address will not be published. Required fields are marked *

*

*