I have been having trouble interfacing with the Lego NXT Light sensor: the analogRead() from pin1 of the NXT cable always returns either a 0 or 1, with the occasional 2 or 3. I have connected the pins from the sensor in the following way:
Here is my code. I have a slight suspicion that the sensor needs a certain delay between readings, but after scouring the internet and trawling through the Lego NXT documentation, I found nothing. It probably does not help that I am not an advanced user!
Any help would be greatly appreciated!
const int inpin = A0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
delay(3);
int val = analogRead(inpin);
Serial.println(val);
}
Looks like an open-collector output which means that it requires a pull-up resistor.
It appears that it MIGHT be an oscillator that generates a PWM signal on the 'analog' line.
Pin 5 appears to control the red LED (Wikipedia says pin 5 is YELLOW, not BLUE)
Thank you very much. Putting a 10K? pull-up resistor in there did solve the problem.
You are correct, it was indeed yellow, not blue. Sorry for the mistake.
May I ask, how does connecting a pull-up resistor to the collector of a transistor solve the problem? Would it be due to the voltage drop across it, creating the output 'signal'?
Yes, all the sensor is is a variable resistor. By using a pull up you cause current to flow that turns the changing resistance into a changing voltage.