ESP32 - Light Sensor
|1||×||ESP-WROOM-32 Dev Module|
|1||×||Micro USB Cable|
|1||×||10 kΩ resistor|
About Light Sensor
The light sensor used in this tutorial is a photoresistor, which is also called light-dependent resistor or photocell.
It is used not only to detect light but also to measure the brightness/illuminance level of the ambient light.
A photoresistor has two pins. Since it is a kind of resistor, we do NOT need to distinguish these pins. They are symmetric.
How It Works
The more light the photoresistor's face is exposed, the smaller its resistance is. Therefore, by measuring the photoresistor's resistance, we can know how bright the ambient light is.
The light sensor value only reflects the approximated trend of the intensity of light, it does NOT represent the exact luminous flux. Therefore, it should be used only in an application that does NOT require high accuracy.
ESP32 - Light Sensor
ESP32's pin A0 to A5 can work as the analog input. The analog input pin converts the voltage (between 0v and VCC) into integer values (between 0 and 1023), called ADC value or analog value.
By connecting a pin of the photoresistor to an analog input pin, we can read the analog value from the pin by using analogRead() function, and then we can know the light levels relatively.
Image is developed using Fritzing. Click to enlarge image
The below code reads the value from photocell and determine the light level qualitatively
- If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE
- Copy the above code and paste it to Arduino IDE
- Click Upload button on Arduino IDE to compile and upload code to ESP32 board
- Open Serial Monitor on Arduino IDE
- Radiates light to sensor
- See the result on Serial Monitor. It looks like the below::
Light Sensor and LED
The below code turns ON the LED when it is dark, otherwise turns OFF the LED
We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.
- Automatically turn on the light when your room is dark. Hint: Refer to ESP32 - Relay.
Alternative Light Sensors
The above code also works with the following light sensors:
※ NOTE THAT:
Note that this tutorial is incomplete. We will post on our Facebook Page when the tutorial is complete. Like it to get updated.