Pages: [1]   Go Down
Author Topic: Light sensor reading problems  (Read 65 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

First timer for Arduino...

I have a project to make a light sensor which will measure the level of transmitted light via a glass with different coatings on it .
So far i managed to get the LED switching, light sensor wiring and even the display working.

BUT i am not able to read out the sensor data from A0 in the state "no" and "off" correctly. Somehow the seem to be reversed. Meaning i get higher values for led off then on...



Here is my humble code

Code:
#include <LiquidCrystal.h>

LiquidCrystal lcd(12,11,5,4,3,2);
const int RED_PIN = 8;
const int GREEN_PIN = 9;
const int BLUE_PIN = 10;

char redled = 'N';
char blueled = 'N';
char greenled = 'N';

int DISPLAY_TIME = 100;  // In milliseconds

void setup() {

  lcd.begin(16, 2);
  lcd.setCursor(0,0);
  lcd.write("R: ");
  lcd.write(redled);
  lcd.write(" G: ");
  lcd.write(blueled);
  lcd.write(" B: ");
  lcd.write(greenled);
 
  lcd.setCursor(0,1);
  lcd.write("LIGHT: "); 
 
  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
 
  Serial.begin(9600);          //  setup serial
}

void loop() {
 
  digitalWrite(RED_PIN, LOW);
  digitalWrite(GREEN_PIN, HIGH);
  digitalWrite(BLUE_PIN, LOW);

  int lightonsensorValue = analogRead(A0);
  greenled = 'Y'; 
  lcd.setCursor(7,1);
  lcd.print("---");
  lcd.setCursor(7,1);
  lcd.print(lightonsensorValue);
  lcd.setCursor(8,0);
  lcd.print(greenled);
  Serial.print("Light On:");
  Serial.println(lightonsensorValue);
 
  delay(5000);
 
  digitalWrite(RED_PIN, LOW);
  digitalWrite(GREEN_PIN, LOW);
  digitalWrite(BLUE_PIN, LOW);

  int lightoffsensorValue = analogRead(A0); 
  greenled = 'N';
   
  lcd.setCursor(7,1);
  lcd.print("---");
  lcd.setCursor(7,1);
  lcd.print(lightoffsensorValue);
  lcd.setCursor(8,0);
  lcd.print(greenled);
 
  Serial.print("Light Off:");
  Serial.println(lightoffsensorValue);
 
  delay(5000);
   
}

SOS
Logged

Offline Offline
Sr. Member
****
Karma: 14
Posts: 440
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is your LDR connected to ground an A0, and with a resistor from A0 to 5V ?
With more light the resistance of the LDR gets lower, so the voltage at A0 gets lower.
I think everything is working as it should.

When you want to map the value of A0 to for example a percentage, you can use the multimap.
http://playground.arduino.cc/Main/MultiMap
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Peter.

I think everything is connected as supposed to. If i just write a code without the LED switching ON and OFF and i just read the A0 where the light sensor is connected and close the black box i read low values (about 10), if i open the black box i read higher values (700-800) as it should. I even tried to use an external LED and have the right response... So i must assume it is the code what is wrong..

My point is in the code:

Void Loop ()
{
Read A0 value at no light state
Switch on a LED
Read A0 vale at a lit state
}

I am thinking that i cannot read twice A0 in the loop.. But that is strange to my mind...


 
Logged

Offline Offline
Sr. Member
****
Karma: 14
Posts: 440
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It might be the 5V power. Can you try a seperate power supply for the Arduino ?
You power everything from the usb ? That is less than 5V.
The backlight of the display needs current, and that led in the box seems to be pretty bright.
The 5V is used as the reference voltage for the analog inputs.
You can read analogRead() as many times as you like. Just once, or thousand times, that's okay.

When you goal is to measure coatings, I would recommand a RGB sensor, and even beyond the vissible: UV and IR leds and sensors.
This is just one sensor, there are so many:
http://www.adafruit.com/products/1980
You might even want to measure deep IR: heat waves. That is what HR++ glass will block. I don't know if that can be measured with a contact-less temperature sensor pointing to the glass.
http://www.adafruit.com/products/1748
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Peter, will try this tomorrow.

Cheers
Logged

Pages: [1]   Go Up
Jump to: