Light sensor reading problems


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

#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.write("R: ");
  lcd.write(" G: ");
  lcd.write(" B: ");
  lcd.write("LIGHT: ");  
  pinMode(RED_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';  
  Serial.print("Light On:");
  digitalWrite(RED_PIN, LOW);
  digitalWrite(GREEN_PIN, LOW);
  digitalWrite(BLUE_PIN, LOW);

  int lightoffsensorValue = analogRead(A0);  
  greenled = 'N';
  Serial.print("Light Off:");


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.

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...

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: 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.

Thanks Peter, will try this tomorrow.



It is not the power supply... I have now connected the uno to an external power source and it seems the readings are still lower when the LED is ON. I really don't think the board cannot handle the stock LED and light sensor in the same time, so must be the code itself...



No, sorry. I don't know. Do you have a schematics or drawing how the led and LDR are connected to the Arduino ?


Just made one!


As you see i have removed the LCD, but seems it behaves the same...

Is that picture okay ? A0 is connected to 5V.

Sorry , bad drawing..

Correct one is:

I read the posts once again, and finally I think I have something that makes sense :cold_sweat: You read the LDR directly after the LED is turned on or off. The led is fast, but the LDR is very slow, about 100ms response time. Can you have delay of 200ms or 500ms before you read A0 ?

Hail, Peter! XD