https://seriousrobotics.wordpress.com/2 ... h-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:
Code: Alles auswählen
#define legoSensorPin 14 // digital pin D14 is analog pin A0
#define legoSensor 0 // this time we define the analog channel
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
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
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!
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.