Light Dependent Resistor Light Sensor
A Light Dependent Resistor (LDR) is a special type of resistor made from semiconductor materials such as cadmium sulfide (CdS) or cadmium selenide (CdSe). Its operating principle is based on the internal photoelectric effect. As the light intensity increases, the resistance value rapidly decreases. The charge carriers generated by the light participate in conduction and, under the influence of an external electric field, undergo drift motion—electrons move toward the positive terminal of the power supply, and holes move toward the negative terminal, causing the resistance of the LDR to drop significantly. In the absence of light, the LDR is nearly in a high-resistance state, with a very large resistance in darkness. The LDR module is typically used to detect the brightness of the surrounding environment, triggering devices such as microcontrollers or relay modules.
Module Source
Purchase Link:
https://item.taobao.com/item.htm?spm=2013.1.0.0.68c07a63p9f0me&id=522579320463
Baidu Netdisk Download Link: https://pan.baidu.com/s/1VMFN1fVo5jxB80IYTsY67A
Password: y8jw
Specifications
Operating Voltage:3.3-5V
Operating Current: 1MA
Module Size: 31.1475 x 14.097mm
Output Type:
DO interface for digital output
AO interface for analog output
Reading Method: ADC
Pin Count: 4 Pin (2.54mm pitch header)
Hardware Connection
The model number of the photoresistor used in this module is 5516, which corresponds to the figure below, and it can be known that the resistance value is around 8 to 20KΩ when the light is bright, and around 1MΩ when the light is dim.
The corresponding schematic diagram is shown in the figure below, in which U2.1 is LM393 and R3 is a photoresistor.AO output is the direct output voltage after R2 and R3 voltage divider, so it is analog; DO is the output high and low level after voltage comparison by LM393, so it is digital. The specific principle is that the 393 pin 3 voltage and pin 2 for voltage comparison. When the voltage of pin 3 is higher than the voltage of pin 2, pin 1 outputs a high level; when the voltage of pin 3 is lower than the voltage of pin 2, pin 1 outputs a low level; you can control the voltage of pin 2 by adjusting R4.
Therefore the DO pin can be configured for input mode and the AO pin needs to be configured for ADC analog input mode.
- The Vcc terminal of the sensor is connected to the 5V supply of the development board;
- The GND terminal is connected to the GND of the development board;
- AO (analog signal output) is connected to the A0 analog input pin of the development board;
- DO (digital signal output) connects to pin 8 of the development board;
Usage Method
Enter the code:
/******************************************************************************
* Test Hardware: LCSC ColorEasyDuino Development Board
* Version Number: V1.0
* Modified By: www.lckfb.com
* Modification Date: April 8, 2024
* Function Overview:
*****************************************************************************
* Open-source development board hardware and software information and related projects hardware and software information on official website
* Development board official website: www.lckfb.com
* Technical support resident forum, any technical problems are welcome at any time to exchange learning
* LCSC Forum: club.szlcsc.com
* Follow our Bilibili account: [立创开发板], stay toned to our latest news!
* We focus on cultivating Chinese engineers rather than profiting from board sales.
******************************************************************************/
int LDR_AO_Pin = A0; // 定义光敏电阻传感器的AO连接到开发板的A0引脚
int LDR_DO_Pin = 8; // 定义光敏电阻传感器的DO连接到开发板的8号引脚
int LDRValue = 0; // 用于存储光照强度值的变量
#define ADC_MAX_VALUE 1023 //ADC的最大采集值
void setup() {
Serial.begin(9600); // 开始串行通讯,设置波特率为9600
pinMode(LDR_DO_Pin, INPUT_PULLUP);
}
void loop() {
// 读取按钮的状态
int LDRState = digitalRead(LDR_DO_Pin);
if (LDRState == LOW) {
Serial.println("检测到高亮度");
} else {
Serial.println("检测到低亮度");
}
LDRValue = analogRead(LDR_AO_Pin); // 读取A0引脚的电压值
Serial.print("光照强度: ");
Serial.println(ADC_MAX_VALUE - LDRValue); // 将读取的值发送到电脑的串行监视器
delay(1000); // 延时1秒
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38