Using RCX Lego sensors with Arduino


Looking at what search terms people used to get to my blog, I found that people are looking to find a way to use old Lego sensors (from the RIS kit) with Arduino. It is very simple.

1. Hardware. You need to cut a Lego cable (not the one connected to the sensor, use an extension cable) in 2 and use each half to connect a sensor to Arduino. Split the cable in 2 wires, solder a male pin to each wire and plug one wire in a GND pin and the other wire in any available analog pin, let’s say A0.

2. Software. You need to set the pin as Output and High for about 10 milliseconds for the sensor capacitor to charge, then set the pin back to Input and do a analog reading. Remember, when you set an analog pin to Output you need to use the equivalent digital pin mapping, not the analog channel! Use the reading as raw value (0-1024) or convert it to percent by using the map command. Here is a sample code:

#define legoSensorPin 14 // digital pin D14 is analog pin A0
#define legoSensor 0 // this time we define the analog channel

void setup(){
  Serial.begin(9600);
  Serial.println("Start");
}

void loop(){
  int raw=ReadLegoSensor(); // we read the raw value of the sensor
  Serial.print("Raw value: ");
  Serial.println(raw); // and print it to the monitor
  byte percent=map(raw, 0, 1024, 0, 100); // we convert raw to percent
  Serial.print("Percent value: ");
  Serial.println(percent); // and print it to the monitor
}

int ReadLegoSensor(){
  pinMode(legoSensorPin, OUTPUT); //set pin as output
  digitalWrite(legoSensorPin, HIGH); //set output high
  delay(10); // wait a bit
  pinMode(legoSensorPin, INPUT); //set pin as input
  int value=analogRead(legoSensor); //read the input
  return value; //return the raw value
}

That’s it! I hope someone will find this useful!

About these ads

About Gabriel (Ro-Bot-X)

Robots, nature, bees, and gardening, how do they mix together? After too much indoor activities one needs to get out and breathe! Harvest natural energy and create a little paradise. And ask the robots to help, of course.
This entry was posted in Uncategorized. Bookmark the permalink.

5 Responses to Using RCX Lego sensors with Arduino

  1. Simon says:

    The Arduino provides 5V on the output pins, but the RCX is 9V based. Have you experienced any problems with using the lower voltage? I wonder how the light sensor works with a lower voltage given that it needs to power the LED.

    • Gabriel (Ro-Bot-X) says:

      Hi Simon,

      Think about this: the RCX has a microcontroller inside, and, as all microcontrollers, it uses 5V logic signals for the sensors. Yes, the battery is 9V, the motors are 9V, but the sensors are 5V as most of the sensors out there. The new sensors (and here I’m not talking about Lego sensors) use 3.3V logic, and to use them with the RCX you need a voltage level converter. The newer NXT sensors like the gyro, compass, accelerometer, use internally 3.3V sensors, but externally they are 5V compatible.

      • Simon says:

        Thanks for the response, it hadn’t occurred to me that the sensors would work that way. I tried your code and with the light sensor I get readings that hover around 130-140. Unfortunately, the readings don’t change at all, regardless of what colour the sensor is placed next to. I tried two different sensors with the same result. I also tried a rotation sensor and I get the same thing, no changes in value despite rotating the sensor. Any ideas?

  2. Cristobal says:

    Hi Gabriel thanks for the post, I got the sensors to give me a full range of readings … it helped me start my project.

  3. Brady says:

    I built this circuit how you described and loaded your program onto my arduino nano, but I get a roughly constant value around 190 or 17-18%. Do you have any thoughts as to why that might be?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s