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(){

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 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.

10 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?

      • Divya says:

        HI I have the same problem as Simon. Can someone help me get the rotation sensor to work. There are no changes in value while the sensor is rotating.

      • Gabriel (Ro-Bot-X) says:

        The rotation sensor outputs 4 different voltages, you need an analog pin to read it. Set the pin as digital output to charge the sensor, then set it as analog input and read the voltage. Serial send the value to the screen to see it, then rotate the sensor slowly to see how the voltage changes. Then set voltage thresholds to each value, then see how the values increase or decrease as you rotate the sensor. For example, you will see 0V; 1.5V; 3.5V; 5V; 0V; 1.5V; 3.5V; 5V and so on as you rotate one way and 5V; 3.5V; 1.5V; 0V; 5V; 3.5V; 1.5V; 0V as you rotate the other way. The voltage values I used are just examples, the one you will actually read will be different, but will keep the trend. Good luck!

  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?

  4. Jonas says:

    Hi! I’m interested in using this for my Raspberry Pi, would it work? Do you know how i can translate this code into Python? Or should i ask on the raspberry pi forums? Thanks! Jonas

    • Gabriel (Ro-Bot-X) says:

      Hi Jonas, I do not own a Raspberry Pi and I don’t know if this method works. The Lego sensors need 5V and the Ras-Pi (as far as I know) offers only 3.3V at the pins. Perhaps if you get a voltage converter it will work. Processing is similar with Arduino code, so that’s what I would use to code it. I have no ideea how Python works. The code may be converted but I do not know how. So, ask on the forums, perhaps someone will be able to help you. Cheers!

  5. Ruud vd Meer says:

    Hello, i have also tested it,It workst better with a pullup resistor of 22k Ohm.
    And a other small change:
    byte percent=map(raw, 200, 600, 100, 0);

Leave a Reply

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

You are commenting using your 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 )

Connecting to %s