In this tutorial, we will learn together how to measure the ambient brightness of a room using a photoresistor and an Arduino / Genuino board. We will first see how a photoresistor works, and then how to use it. As a bonus, we will see how to increase or decrease the sensitivity of the photoresistor if the need arises.
Hello everyone !
In my previous article, we saw together how to measure voltages with an Arduino / Genuino board. Today we are going to use this new knowledge by measuring the ambient brightness of a room using a photoresistor.
What is a photoresistor?
Photoresistor and symbol
A photoresistor is a component whose resistivity depends on the ambient light. To put it simply, it is a resistor whose value changes depending on the light it receives.
There are different types of photoresistors, each with different resistance values depending on the ambient light. The most classic type of photoresistors are 1M ohms (dark) / 12K ohms (full light). It is this kind of photoresistor that is used later in this tutorial.
Regardless of the diameter of the photoresistor, its value in shadow or in bright light, when a photoresistor is illuminated, its resistance decreases. It is therefore possible to use a photoresistor to measure the ambient luminosity.
Without making an exhaustive list, here are some examples of very classic uses for a photoresistor:
- Day / night detection,
- Ambient light measurement (to adjust lighting for example),
- Light tracker (for solar panels, robots, etc.),
Fiat Lux and lux leaks
If you want to be the man of science in the evening, shout "Fiat lux et lux fuit" while turning on the light in a room. It literally means "Let there be light and there was light".
What interests us in this expression, in the context of this tutorial, is the "lux". The "lux" is the unit of measurement for light. For the curious, here is something to read for tonight before going to bed: https://fr.wikipedia.org/wiki/Lux_%28unit%C3%A9%29.
Here are some examples of values, directly pumped from Wikipedia:
Activity or place concerned | Average illuminance |
---|---|
Full moon night | 0,5 lux |
Rue de nuit bien éclairée | 20 à 70 lux |
Living room | 100 à 200 lux |
Well-lit apartment | 200 à 400 lux |
Work room | 200 à 3 000 lux |
Night stadium | 150 à 1 500 lux |
Outdoors in an overcast sky | 500 à 25 000 lux |
Outdoors in full sun | 50 000 à 100 000 lux |
Measuring brightness with a photoresistor
Now that you know everything there is to know about photoresistors, it's time to put one to work in an example montage.
The purpose of the demonstration set-up will be to simply measure the ambient light in a room and send the measured value to the computer via the USB cable.
The demonstration assembly
Necessary material
To carry out this assembly, we will need:
- An Arduino UNO board (and its USB cable),
- A 1M ohm photoresistor (3mm or 5mm in diameter, it doesn't matter),
- A 10K ohm resistor (brown / black / orange),
- A test plate and wires to wire our assembly.
Schematic view of the assembly
Prototyping view of the assembly
To start our assembly, we are going to wire the VCC pin of the Arduino board to one of the legs of the photoresistor by means of a wire. The second leg of the photoresistor is then connected to one of the two legs of the 10K ohm resistor. Finally, we wire the second leg of the 10K ohm resistor to the GND pin of the Arduino board.
The finished assembly
Once the 10K ohm resistor and the photoresistor are wired, all that remains is to connect the junction between these two resistors to pin A0 on the Arduino board.
The most attentive will have noticed that this assembly is a voltage divider bridge, of which we saw the operating principle in the previous tutorial.
The demo code
Okay, now that we have our edit, let's get to the code!
The goal of our code will be to:
- Read the voltage on pin A0,
- Send the value to the PC (for display),
- Start over at point 1.
To achieve this piece of code, we will use the analogRead () function seen in the previous tutorial.
1 2 3 | void setup() {
Serial.begin(9600);
} |
We will start our demonstration program with the setup () function which will simply initiate communication with the PC.
PS Don't try to figure out how to use Serial.begin () yet, it will be the subject of a future tutorial.
1 2 3 4 5 6 | void loop() {
int valeur = analogRead(A0);
Serial.println(valeur);
delay(250);
} |
In the loop () function, we're going to do two things:
- Measure the voltage on pin A0 with analogRead ().
- Send the value to the PC and wait a few milliseconds to have time to read what is happening on the PC side.
Why make it simple when you can make it complicated
This demo code is intentionally very simplistic, because there is no simple mathematical relationship between the measured value and the ambient brightness.
Light curve VS resistance
If we look at the brightness curve (in lux) VS the resistance (in ohms) provided by the manufacturer, we realize that it is not a straight line, but a curve. It is therefore quite complicated to determine which brightness (in lux) corresponds to a value measured by analogRead ().
It is also necessary to take into account the fact that a photoresistor is a very inexpensive component, which explains its presence in toys or other circuits at low cost. Manufacturing tolerances are typically ~ 40% different from nominal strength values.
Color curve VS response
In addition, a photoresistor is sensitive to heat and reacts differently depending on the colors. The measurement will therefore be slightly different if it is very hot or very cold. Likewise, the measured value will be different depending on the color of the light.
Photoresistors are mainly used to detect the presence (or absence) of light in a room or outdoors. To make a real measurement of brightness (in lux, with a fixed precision that matter the temperature and the color), there are specialized sensors for this like the TSL2561 sensor.
The complete code with comments:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | /*
* Example code for a photoresistor.
*/
// Setup () function, called when starting the Arduino board
void setup() {
//Initializes communication with the PC
Serial.begin(9600);
}
// Loop () function, called continuously as a loop as long as the Arduino board is powered
void loop() {
// Measure the voltage on pin A0
int valeur = analogRead(A0);
// Send the measurement to the PC for display and wait 250ms
Serial.println(valeur);
delay(250);
} |
After sending the program to the Arduino board, by opening the serial monitor ("tools" tab), then by selecting the right communication speed (here 9600 bauds), you should see the digital value measured at the output of photoresistor.
If your mounting is correct, covering the photoresistor or pointing it at a light source, the values in the serial monitor should normally change.
The measured value has no unit! It is a purely indicative value.
If you want to have a measurement in lux, you will have to calibrate your photoresistor and integrate the calibration data into the code. Knowing how to calibrate a sensor is not the goal of this tutorial, we will talk about it next time 😉
Bonus: Improve the sensitivity of the editing
Diagram of a voltage divider bridge
As a reminder, the formula for a voltage divider bridge is as follows: Vout = Vin * (R2 / (R1 + R2))
In our assembly, the resistor R1 is replaced by a photoresistor. Consequently, the value of R2 has a huge influence on the sensitivity of the assembly.
If the value of R2 is negligible compared to the value of R1, the photoresistor will be insensitive, as it will be necessary to reach a significant level of luminosity before R2 approaches the value of R1 and begins to make a difference in the equation.
On the contrary, if the value of R2 is not negligible compared to the value of R1, the photoresistor will be very sensitive, because the slightest difference in luminosity will cause a change in the voltage at the output of the divider bridge.
To summarize, the greater the value of resistor R2, the more sensitive the photoresistor. Conversely, the lower the value of resistor R2, the less sensitive the photoresistor. Finding the right balance may take some time, in this kind of case, a potentiometer instead of R2 is very practical (see previous tutorial).
Conclusion
This tutorial is now complete.
If you enjoyed this tutorial, feel free to comment on it on the forum, post it on social media, and support the site if you like it.