Project tutorial
Simple Arduino Based Lego Power Function Receiver

Simple Arduino Based Lego Power Function Receiver © GPL3+

This lesson aims to show how to make a very simple Lego Power Function Receiver.

  • 5,479 views
  • 2 comments
  • 9 respects

Components and supplies

A000053 iso both1
Arduino Micro & Genuino Micro
×1
IR Receiver Tsop2238 - 38 KHZ
×1
10 100 ohm 1/4w resistor
×1
Breadboard and Wire Kit
×1
Lego Power Function Remote
×1

About this project

1. Build the circuit

Since the circuit is essentially based on a TSOP2238, it is very simple. Data pin of the TSOP2238 is connected to pin 3 of the Arduino Board. For this example, we chose an Arduino Micro.

2. The code

Since the code uses a custom .h file that stores all the IR commands sent by the Lego Power Function Remote, it is attached to the project.

Essentially, the code starts the IR receiver, and when a known command is detected, it is written to the serial monitor. Use it to make something that interacts with your Lego Power Function Remote! The available macros are reported in the top of the code. There is also an LED that indicates that a valid command has been received.

Code

Code snippet #1Arduino
/*
 * IRremote: IRrecvDemo - demonstrates receiving IR codes with IRrecv
 * An IR detector/demodulator must be connected to the input RECV_PIN.
 * Version 0.1 July, 2009
 * Copyright 2009 Ken Shirriff
 * http://arcfn.com
 */

/*
 * Testing code for receiving data from the Lego Power Function IR Remote Control http://bit.ly/1Gq18RG
 * If you want to deepen the knowledge of Lego Power Functions Modules, http://www.philohome.com/pf/pf.htm
 *
 * TODO:
 * - test if LEGO® Power Functions IR Speed Remote Control uses the same protocol http://bit.ly/1wDQ13l
 * - sort out some extra codes received
 *
 * Davide Gomba, 16/12/2014
 *Arturo Guadalupi, 17/12/2014
 *
 *Available Macros:
 *CH1_LEFT_UP
 *CH1_LEFT_DOWN
 *CH1_RIGHT_UP
 *CH1_RIGHT_DOWN
 *CH1_BREAK
 *
 *CH2_LEFT_UP
 *CH2_LEFT_DOWN
 *CH2_RIGHT_UP
 *CH2_RIGHT_DOWN
 *CH2_BREAK
 *
 *CH3_LEFT_UP
 *CH3_LEFT_DOWN
 *CH3_RIGHT_UP
 *CH3_RIGHT_DOWN
 *CH3_BREAK
 *
 *CH4_LEFT_UP
 *CH4_LEFT_DOWN
 *CH4_RIGHT_UP
 *CH4_RIGHT_DOWN
 */
 
#include <IRremote.h>
#include "codes.h"

const int RECV_LED = 12;
const int RECV_PIN = 3; // Digital IO pin connected to the IR receiver
IRrecv irrecv(RECV_PIN);

long unsigned startTime = millis();

decode_results results;

void setup()
{
  pinMode(RECV_LED, OUTPUT);
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver

}

void loop() {
  if(millis() - startTime > 200)
    digitalWrite(RECV_LED, LOW);
  else
    digitalWrite(RECV_LED, HIGH);
    
  if (irrecv.decode(&results)) {
    if(legoCode(results.value) != -1) //known code
      startTime = millis();
    // Channel #1
   
    if (legoCode(results.value) == CH1_LEFT_UP)
    {
      Serial.println(" CH - 1 // LEFT UP");
    }
    else if (legoCode(results.value) == CH1_LEFT_DOWN)
  {
    Serial.println(" CH - 1 // LEFT DOWN");
    }
    else  if (legoCode(results.value) == CH1_RIGHT_UP) {
    Serial.println(" CH - 1 // RIGHT UP");
    }
    else if (legoCode(results.value) == CH1_RIGHT_DOWN) {
    Serial.println(" CH - 1 // RIGHT DOWN");
    }
    else if (legoCode(results.value) == CH1_BREAK) {
    Serial.println(" CH - 1 // BREAK");
    }

    // Channel #2

    else if (legoCode(results.value) == CH2_LEFT_UP) {
    Serial.println(" CH - 2 // LEFT UP");
    }
    else if (legoCode(results.value) == CH2_LEFT_DOWN) {
    Serial.println(" CH - 2 // LEFT DOWN");
    }
    else  if (legoCode(results.value) == CH2_RIGHT_UP){
    Serial.println(" CH - 2 // RIGHT UP");
    }
    else if (legoCode(results.value) == CH2_RIGHT_DOWN){
    Serial.println(" CH - 2 // RIGHT DOWN");
    }
    else if (legoCode(results.value) == CH2_BREAK) {
    Serial.println(" CH - 2 // BREAK");
    }
    
    // Channel #3
    
    else if (legoCode(results.value) == CH3_LEFT_UP) {
    Serial.println(" CH - 3 // LEFT UP");
    }
    else if (legoCode(results.value) == CH3_LEFT_DOWN) {
    Serial.println(" CH - 3 // LEFT DOWN");
    }
    else  if (legoCode(results.value) == CH3_RIGHT_UP) {
    Serial.println(" CH - 3 // RIGHT UP");
    }
    else if (legoCode(results.value) == CH3_RIGHT_DOWN){
    Serial.println(" CH - 3 // RIGHT DOWN");
    }
    else if (legoCode(results.value) == CH3_BREAK) {
    Serial.println(" CH - 3 // BREAK");
    }

    // Channel #4

    else if (legoCode(results.value) == CH4_LEFT_UP) {
    Serial.println(" CH - 4 // LEFT UP");
    }
    else if (legoCode(results.value) == CH4_LEFT_DOWN) {
    Serial.println(" CH - 4 // LEFT DOWN");
    }
    else  if (legoCode(results.value) == CH4_RIGHT_UP) {
    Serial.println(" CH - 4 // RIGHT UP");
    }
    else if (legoCode(results.value) == CH4_RIGHT_DOWN) {
    Serial.println(" CH - 4 // RIGHT DOWN");
    }
    else if (legoCode(results.value) == CH4_BREAK) {
    Serial.println(" CH - 4 // BREAK");
    }
    irrecv.resume(); // Receive the next value
  }
}

Comments

Similar projects you might like

Take Control Over Lego Power Functions

Project tutorial by Patrick Müller

  • 43,671 views
  • 3 comments
  • 60 respects

Arduino and LEGO® Power Functions

Project tutorial by Tiago Santos

  • 12,110 views
  • 0 comments
  • 23 respects

LEGO Wall-E with Arduino

Project tutorial by monsterbacke

  • 15,926 views
  • 14 comments
  • 84 respects

A very simple way to power Arduino

by Giovanni Carrera

  • 23,066 views
  • 3 comments
  • 9 respects

Autobot Using Lego NXT Motors and Sensor

Project tutorial by Mark Tashiro

  • 14,206 views
  • 5 comments
  • 25 respects

A Simple Light Follower

Project tutorial by a_guadalupi

  • 6,095 views
  • 1 comment
  • 9 respects
Add projectSign up / Login