I've been using an LDR to measure light values in a light chamber over several months and have noticed that there is a slight fluctuation in the starting values for the LDR. For example the value might be 345 one day and the next it is 347 then the next 344.
This causes an issue for me because I'm using it to measure the time until a chemical solution reaches a certain light value to shut a motor off and its causing a significant difference in the time the motor shuts off. Because of the project I'm working on, I have to use a light sensor readings from the chemical reaction to stop the motor.
Any thoughts on how to fix this would be greatly, greatly appreciated.
What I have found is that the range of the light values during the chemical reaction stays essentially the same and I was wondering if there way a way to set the relay to shut off to the motor after that range has been met? i.e. a change of 200 in light value increments? Currently, I have an If statement to shut it off at a certain value, but I believe a range would give me much more accuracy.
I'm attaching my code, much thanks for a point in the right direction!!
#include <math.h>
int pinOut = 10;
int lightSensorPin = A2; //sets light sensor to analog pin 2
int analogValue = 0; //sets intial value for analog to 0
unsigned long time; //starts program timer
void setup() {
pinMode(A2,INPUT_PULLUP); //defines analog pin 2 as input
pinMode(10, OUTPUT);
Serial.begin(9600); //begins serial monitor
}
void loop() {
Serial.print("Light Reading: "); //statement printed in serial monitor
Serial.println(analogRead(A2)); //light reading of sensor printed in serial monitor
Serial.print("Time: "); //statement printed in serial monitor
time = millis(); //time unit millisecond
Serial.println(time/ 1000.0, 3); //prints time since program started in seconds to third decimal place
delay(500); //delays measurement by 1/2 second
analogValue = analogRead(lightSensorPin); //takes value from light sensor
if(analogValue >= 0 && analogValue >= 735){
delay(500); //delays measurement by 1/2 second
if(analogValue >= 0 && analogValue >= 735){
digitalWrite(pinOut, LOW); //Switches off relay if value met
delay(10000);
}
}
else{
digitalWrite(pinOut, HIGH);
}
delay(500);
}
My guess is that the voltage of your power source is changing slightly. How do you power your setup? Remember the difference between 345 and 347 is about 10mV so if the voltage of your power source is changing by 30mV from one day to the other it explains that difference.
I'm missing the links to the used hardware and the wiring diagram.
If you write about ranges, do you mean you just want to take care for differences in the read voltage? That's easy, just save the read value at the start of the day in a variable and subtract that from every read value for the rest of the day.
Depending on the actual source of your measurement errors this might not solve your problem.
Hi, thanks for your response. I attached my wiring schematic, I apologize for it being rudimentary, I'm not an EE by trade so I hope it makes sense. I have voltage regulators set measured to the .01 of a volt on the light source and motor. For monitoring the LDR, I had the Arduino plugged into my computer via USB, so there is no regulator there. Could that be an issue and how would I fix that?
And yes, I'd like to measure the difference in the readout of the LDR, i.e. if I want the motor to shut off when a difference of 200 is reached, it could start at 0 and reach 200 or start at 100 and reach 300, but the difference is still 200. I assumed I would have to take starting value and set it as a variable and then compare it to the continuing measured values, but honestly, I have no idea of how to do the code in IDE for that. I looked up a couple tutorials, but I didn't find any that used an initial sensor value and did comparisons. If you can point me to the right place to start I'd appreciate it, and thank you again!
I would do this using a reference beam, viewing the source directly (possibly via an optical attenuator), to help eliminate supply or source degradation variations.
I assumed I would have to take starting value and set it as a variable and then compare it to the continuing measured values, but honestly, I have no idea of how to do the code in IDE for that.
In the setup function take an initial reading. Then take the reading in the loop. When you check the new reading simply do:-
if(newReading - initialReading >= 200){ // shut your motor off
The relay should have a fly-back diode, check that for the relay board you use (I guess you use a board as you have GND, 5V and the signal connected to it).
pylon:
My guess is that the voltage of your power source is changing slightly. How do you power your setup? Remember the difference between 345 and 347 is about 10mV so if the voltage of your power source is changing by 30mV from one day to the other it explains that difference.
An LDR/resistor is a ratiometric setup, so supply/Aref voltage does not matter.
How you connect the LDR/resistor matters.
They should have their own supply/ground wires to the Arduino (not shared by other loads/relay)
100 ohm pull down is rather low for an LDR. Is the light that strong?
I go with hammy. What kind of light source, and is it's supply stable.
Leo..