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:

4 Comments

  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
  2. Tomi Engdahl says:

    Raspberry Pi is releasing an add-on that will let you use many of its tiny, inexpensive computers to control certain Lego robot motors and sensors. The add-on is called the Build HAT (HAT stands for Hardware Attached on Top), and slotting it onto a Raspberry Pi’s GPIO pins will give you four ports that you can use to control Lego Education’s SPIKE components, which the HAT and its software are specially designed for. It’ll also connect to most other parts that use an LPF2 connector, including the components from the Lego Mindstorms robot inventor kit.

    https://www.msn.com/en-us/news/technology/this-raspberry-pi-add-on-lets-you-control-lego-robots/ar-AAPG1Fz?ocid=uxbndlbing

    Reply
  3. Tomi Engdahl says:

    New Part Day: Raspberry Pi LEGO HAT
    https://hackaday.com/2021/10/21/new-part-day-raspberry-pi-lego-hat/

    The Raspberry Pi Foundation have been busy little bees for the last couple of years producing their own silicon, new boards and now in collaboration with the LEGO Education team a new HAT to connect to the LEGO SPIKE education platform. This new HAT board will work with every Raspberry Pi board with a 40-pin GPIO header.

    Based on the RPI2040 microcontroller, it makes an interesting detour away from dumb slave boards, although it looks like the firmware is closed (for now) so you’ll have to make do with the pre-baked capabilities and talk to it with the supplied python library.

    According to the documentation, the communication between the Pi and the RPI2040 nestled beneath the HAT PCB is plaintext-over-serial, freeing up the majority of the GPIO pins for other uses.

    Meet the Raspberry Pi Build HAT: create with Raspberry Pi and LEGO® Education
    https://www.raspberrypi.com/news/raspberry-pi-build-hat-lego-education/

    Reply

Leave a Comment

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

*

*