Project 8.01 Max and Min Brightness
In this project, we'll find out the real-world minimum and maximum readings from our Light Dependent Resistor (LDR) in the room you’re in. Unlike the potentiometer, in practice the LDR won't give us the full range from 0 to 1023 that we can read with the analogRead function.
Why is that? Well, our LDR is set up in a circuit in series another resistor, which has a resistance of 10,000 ohms. The readings we get depend on the total resistance of this setup.
For us to get a reading of 0, the LDR would have to have no resistance at all - that's 0 ohms, which isn't going to happen because all objects have some resistance.
Similarly, for us to get the maximum reading of 1023, the LDR would need to have an infinite amount of resistance. That's not possible either.
So, the real-world readings we get will be somewhere in between, and that's what we're going to find out!
Project Code:
/////////////////////////////////////////////// // 8.01 Max and Min Brightness byte lightSensorPin = A2; int maxValue = 0; int minValue = 0; void setup() { pinMode(lightSensorPin, INPUT); Serial.begin(9600); maxValue = analogRead(lightSensorPin); minValue = analogRead(lightSensorPin); } void loop() { int currentBrightness = analogRead(lightSensorPin); if (currentBrightness > maxValue) { maxValue = currentBrightness; Serial.print("The new max brightness is: "); Serial.println(maxValue); } if (currentBrightness < minValue) { minValue = currentBrightness; Serial.print("The new min brightness is: "); Serial.println(minValue); } } ///////////////////////////////////////////////
*If you’re copying and pasting the code, or typing from scratch, delete everything out of a new Arduino sketch and paste / type in the above text.
We will need a variable to hold the min and max voltage values that come across the LDR.
int maxValue = 0; int minValue = 0;
In the setup() function, we have to load these variables we created with analogRead values. The reason these variables need loaded with real analogRead values is because we do not want to load them initially with values more or less than their range will support.
What does that mean? For example, if the lowest in the range of 0 – 1023 that the LDR would read was 20, then if we loaded the minValue with 0 and checked to see if any value went below it, it would never happen.
If we instead load it with an analogRead then that is part of the range by default, since it is a reading of the voltage across the LDR.
maxValue = analogRead(lightSensorPin); minValue = analogRead(lightSensorPin);
In the loop() function, we first analogRead the current voltage value of the LDR
int currentBrightness = analogRead(lightSensorPin);
After that, we then look to see if the value of the currentBrightness variable is greater than the value of the maxValue variable. If so, we have a new max value and the maxValue variable needs to be reassigned with the value of currentBrightness. Then we print that value for our records.
if (currentBrightness > maxValue) { maxValue = currentBrightness; Serial.print("The new max brightness is: "); Serial.println(maxValue); }
We then do the same thing but with the minValue variable. If currentBrightness variable is less than the minValue variable, we reassign it with its value and print it.
if (currentBrightness < minValue) { minValue = currentBrightness; Serial.print("The new min brightness is: "); Serial.println(minValue); }
Make sure and write these values down for the next project. These are the max and min brightness values for the room you’re in. Think of this project as calibrating your MC Trainer to get the most out of it.