Photoresistor Light Sensor
The photoresistor is a special resistor made of semiconductor materials such as cadmium sulfide or cadmium selenide. Its working principle is based on the internal photoelectric effect. As the light intensity increases, the resistance value rapidly decreases, because the carriers generated by light all participate in conduction and drift under the action of an external electric field, with electrons moving toward the positive electrode of the power supply and holes moving toward the negative electrode of the power supply, thereby rapidly reducing the resistance value of the photoresistor. When there is no light, it is almost in a high-resistance state, and the resistance is very large in the dark. Photoresistor modules are generally used to detect the brightness of ambient light, and to trigger microcontrollers or relay modules, etc.
Module Source
Purchase link: https://item.taobao.com/item.htm?spm=2013.1.0.0.68c07a63p9f0me&id=522579320463 Materials download link: https://pan.baidu.com/s/1VMFN1fVo5jxB80IYTsY67A Materials extraction code: y8jw
Specifications
Operating voltage: 3.3-5V Operating current: 1MA Module dimensions: 31.1475 x 14.097mm Output method: DO interface is digital output AO interface is analog output Read method: ADC Number of pins: 4 Pin (2.54mm pitch header)
Principle Analysis
The photoresistor model used in this module is 5516. According to the figure below, it can be seen that the resistance is about 8 to 20KΩ in bright light and about 1MΩ in the dark.
The corresponding schematic diagram is shown in Figure 2.2.3.1-2, where U2.1 is the LM393 and R3 is the photoresistor. The AO output directly outputs the voltage after voltage division by R2 and R3, so it is an analog quantity; the DO is the high or low level output after voltage comparison by the LM393, so it is a digital quantity. The specific principle is that the voltage at pin 3 of the 393 is compared with the voltage at pin 2. When the voltage at pin 3 is higher than that at pin 2, pin 1 outputs a high level; when the voltage at pin 3 is lower than that at pin 2, pin 1 outputs a low level; the voltage at pin 2 can be controlled by adjusting R4.
Porting Process
Pin Selection
Port to Project
Our goal is to port the example to the ESP32-S3 dev board. Complete driver code has been provided for you. Follow the steps below to complete the porting. For detailed instructions on creating folders and new .c and .h files, refer to section 1.4.2 in the [DHT11 Temperature and Humidity Sensor] chapter; we will not repeat it here. However, here we change the file names bsp_dht11.c and bsp_dht11.h to bsp_illume.c and bsp_illume.h, and change the folder name to illume.
Write Code
Write the following in the bsp_illume.c file:
#include "bsp_illume.h"
#include "stdio.h"
#include "stdlib.h"
#include "rom/ets_sys.h"
esp_adc_cal_characteristics_t *adc_chars;
void delay_ms(unsigned int ms)
{
vTaskDelay(ms / portTICK_PERIOD_MS);
}
void delay_us(unsigned int us)
{
ets_delay_us(us);
}
/******************************************************************
* Function Name: Illume_GPIO_Init
* Function Description: Initialize photoresistor light sensor pin functions
* Function Parameters: none
* Function Return: none
* Author: LC
* Notes: none
******************************************************************/
void Illume_GPIO_Init(void)
{
gpio_config_t adc_config = {
.pin_bit_mask = (1ULL<<GPIO_DO), // Configure pin
.mode =GPIO_MODE_INPUT, // Input mode
.pull_up_en = GPIO_PULLUP_DISABLE, // Disable pull-up
.pull_down_en = GPIO_PULLDOWN_DISABLE, // Disable pull-down
.intr_type = GPIO_INTR_DISABLE // Disable pin interrupt
};
gpio_config(&adc_config);
adc1_config_width(width);// 12-bit resolution
//ADC_ATTEN_DB_0: indicates reference voltage is 1.1V
//ADC_ATTEN_DB_2_5: indicates reference voltage is 1.5V
//ADC_ATTEN_DB_6: indicates reference voltage is 2.2V
//ADC_ATTEN_DB_11: indicates reference voltage is 3.3V
//adc1_config_channel_atten( channel,atten);// Set channel 0 and 3.3V reference voltage
// Allocate memory
adc_chars = calloc(1, sizeof(esp_adc_cal_characteristics_t));
// Initialize ADC characteristics so that conversion results and compensation factors can be correctly calculated
esp_adc_cal_characterize(unit, atten, width, DEFAULT_VREF, adc_chars);
}
/******************************************************************
* Function Name: Get_Adc_Dma_Value
* Function Description: Output after calculating the average of saved data
* Function Parameters: CHx which scanned data
* Function Return: corresponding scanned ADC value
* Author: LC
* Notes: Return value minimum 0, maximum 4095
******************************************************************/
unsigned int Get_Adc_Dma_Value(char CHx)
{
unsigned char i = 0;
int AdcValue = 0;
AdcValue = adc1_get_raw(ADC1_CHANNEL_0); //GPIO1
// //Convert the result of ADC1 channel 0 (gpio1) to voltage, unit mV
// voltage = esp_adc_cal_raw_to_voltage(AdcValue, adc_chars);
return AdcValue;
}
/******************************************************************
* Function Name: Get_illume_Percentage_value
* Function Description: Read photoresistor value and return percentage
* Function Parameters: none
* Function Return: returns percentage
* Author: LC
* Notes: Brightest 100, darkest 0
******************************************************************/
unsigned int Get_illume_Percentage_value(void)
{
//2 to the 12th power = 4096
//Since the microcontroller counts from 0, so 4096-1=4095
int adc_max = 4095;
int adc_new = 0;
int Percentage_value = 0;
adc_new = Get_Adc_Dma_Value(channel);
//Percentage = ( current value / maximum value ) * 100
Percentage_value = ( 1 - ( (float)adc_new / adc_max ) ) * 100;
return Percentage_value;
}
/******************************************************************
* Function Name: Get_DO_In
* Function Description: Read the level status of DO pin
* Function Parameters: none
* Function Return: 1=detected too bright 0=detected too dark
* Author: LC
* Notes: none
******************************************************************/
char Get_DO_In(void)
{
if( GET_DO_IN == 1 )
{
return 1;
}
return 0;
}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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
Write the following in the bsp_illume.h file:
#ifndef _BSP_MQ2_H_
#define _BSP_MQ2_H_
#include <stdio.h>
#include <inttypes.h>
#include "sdkconfig.h"
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_rom_sys.h"
#include "esp_timer.h"
#include "driver/uart.h"
#include "rom/ets_sys.h"
#include "esp_system.h"
#include "driver/gptimer.h"
#include "esp_log.h"
#include "freertos/queue.h"
#include "driver/spi_master.h"
#include "nvs_flash.h"
#include "esp_adc/adc_cali_scheme.h"
#include "esp_adc/adc_cali.h"
#include "driver/adc.h"
#include "esp_adc_cal.h"
#define DEFAULT_VREF 1100 // Default reference voltage, unit mV
#define channel ADC_CHANNEL_0 // ADC measurement channel
#define width ADC_WIDTH_BIT_12 // ADC resolution
#define atten ADC_ATTEN_DB_11 // ADC attenuation
#define unit ADC_UNIT_1 // ADC1
#define GPIO_AO 1
#define GPIO_DO 2
#define GET_DO_IN gpio_get_level(GPIO_DO)
void delay_ms(unsigned int ms);
void Illume_GPIO_Init(void);
unsigned int Get_Adc_Dma_Value(char CHx);
unsigned int Get_illume_Percentage_value(void);
char Get_DO_In(void);
#endif2
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
39
40
41
42
43
At this point, the porting is complete. Please proceed to section 2.5 for porting verification.
Porting Verification
Enter the following code in main.c
#include <stdio.h>
#include "illume/bsp_illume.h"
void app_main(void)
{
Illume_GPIO_Init();
printf("ADC+DMA demo start\r\n");
while(1)
{
printf("ADC-%d\r\n", Get_Adc_Dma_Value(0) );
printf("illume-%d%%\r\n", Get_illume_Percentage_value() );
delay_ms(1000);
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Power-on effect:
Driver code:
File Download
📌 Materials Download Center (click to jump)
📌 In the Materials Download Center -> Module Porting Materials Download, inside the compressed package of this chapter.