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!
43.670233
-79.386755