In this example we connect a photoresistor to a ESP32 Lolin32, the value read from the photoresistor corresponds to the amount of light present. The photoresistor is connected to A0 in this example.
A photoresistor (or light-dependent resistor, LDR, or photocell) is a light-controlled variable resistor. The resistance of a photoresistor decreases with increasing incident light intensity; in other words, it exhibitsphotoconductivity. A photoresistor can be applied in light-sensitive detector circuits, and light- and dark-activated switching circuits. A photoresistor is made of a high resistance semiconductor.
In the dark, a photoresistor can have a resistance as high as several megohms (MΩ), while in the light, a photoresistor can have a resistance as low as a few hundred ohms. If incident light on a photoresistor exceeds a certain frequency, photons absorbed by the semiconductor give bound electrons enough energy to jump into the conduction band. The resulting free electrons (and their hole partners) conduct electricity, thereby lowering resistance. The resistance range and sensitivity of a photoresistor can substantially differ among dissimilar devices. Moreover, unique photoresistors may react substantially differently to photons within certain wavelength bands.
A practical example could be a dark room sensor for photography, if the reading approached a critical level an alarm could be activated or even a night light
Here is a sample module
Schematic
In this example I connected 3v3 to the module and it worked fine
Code
In this example we simply output the reading via the serial port.
[codesyntax lang=”cpp”]
int sensorValue; void setup() { Serial.begin(9600); // starts the serial port at 9600 } void loop() { sensorValue = analogRead(A0); // read analog input pin 0 Serial.print(sensorValue, DEC); // prints the value read Serial.print(" \n"); // prints a space between the numbers delay(1000); // wait 100ms for next reading }
[/codesyntax]
Testing
Open the serial monitor and move the LDR closer to a light, cover the LDR.
464
1535
4054
3999
1471
425
326
368
522
509
507
540
0
0
98
365
0
426
Links
You can just buy some photoresistors, they are cheap items