1. Overview
Photoresistor is an analog sensor whose resistance changes with light intensity. With this characteristic, we design circuits to convert resistance changes into voltage ones. This sensor can be applied to simulate people’s judgment of the ambient light intensity, so as to facilitate friendly interaction.
2. Parameters
3. Principle
The working principle of photoresistor is based on the internal photoelectric effect. The brighter the light is, the lower the resistance will be.
When the light intensity changes, the resistance value fluctuates, so the voltage detected at the signal end S also changes (0 ~ 3.3V). This voltage change is a continuously changing analog which takes any value within 0 ~ 3.3V. Our main board is able to directly process not analog signals, but digital ones, so ADC(Analog to Digital Converter) acquisition is required.
What is ADC?
An ADC(Analog to Digital Converter) converts analog to digital. Our board has integrated ADC acquisition, which can be used directly.
kidsIOT ADC technical specifications
Resolution: 12bit
An n-bit ADC means that the ADC has 2ⁿ scales. A 12-bit ADC includes total
General ADC input voltage calculation:: reference voltage
4. Wiring Diagram
The photoresistor is an analog module. It needs to be connected to a red socket of the main board.
Here we connect the module to socket 7. From the port view of the kidsIOT board, socket 7 is analog io port io35.
When light shines on the photoresistor, it outputs the value read at the moment.
5. Test Code
/*
Project name: 8_light
Function: Read the analog value of the photoresistor
Author: keyestudio
Hardware connection:
- Connect to port 7
Library:
- none (no need to import additional library)
Cautions:
- Connect to port 7
- Before uploading test code, please correctly connect to the development board and port
- Set the baud rate to 9600 before using serial monitor printing
*/
#define light_Pin 35 //set photoresistor to IO35
void setup() {
//Set the baud rate
Serial.begin(9600);
//set IO35 to input
pinMode(light_Pin, INPUT);
}
void loop() {
//Read the analog value of photoresistor
int light_value = analogRead(light_Pin);
//Print the analog value of photoresistor
Serial.print("Analog value:");
Serial.println(light_value);
//Delay 300ms to observe the printed analog value
delay(300);
}
6. Test Result
After uploading the test code, open the serial monitor and set the baud rate to 9600, and the serial monitor prints the analog values of the photoresistor. Cover the sensor with our hand to dim the ambient light, and the analog value decreases.
7. Code Explanation
7.1 analogRead(light_Pin);
We have learned the function of reading digital signals. Herein, analogRead() is a function that reads analog signals.
Syntax: analogRead(Pin);
Pin: pin number of the read analog value
Please refer to the official website for details: analogRead() | Arduino Documentation