Hi, I want to create a segway that knows about the distance of the ground using light sensors. I have been trying to use IR phototransistor and IR led in order to measure the intensity of the light that goes back to the phototransistor.
I used IR because I do not want the surrounding light to disturb the signal.
First, I wanted to see what the Phototransistor measures in front of a LED. I have weird values. (Either 0 or 1023, which means either everything or nothing, it is not quite analogic).
Connections. It is the basic:
- Resistor of 220 Ohms connected in serie to the LED to produce light feeded to the 5V.
- Same resistor connected to the phototransistor, same connection, and the analog input connected between resistor and phototransistor.
Coding:
int led = 5;
int ledState = HIGH;
int LDR = 3;
long previousMillis = 0;
long interval = 1000;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
pinMode(LDR, INPUT);
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// turn the LED on (HIGH is the voltage level)
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval)
{
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// set the LED with the ledState of the variable:
digitalWrite(led, ledState);
}
Serial.println(analogRead(LDR));
}
I do not know what went wrong. I would like the transistor to return normal values varying on the intensity it senses.
Can anyone help me understanding what went wrong ?
PS: I cannot test anymore, one of the LED pin is missing. I could on Monday, UK time.
Thank you by advance.
Kind regards.