S12SD UV Sensor
This UV detection module uses a Schottky photodiode based on gallium nitride material, featuring high responsivity and low dark current. The onboard LM358 amplifier amplifies the weak signal output from the photodiode. All components are manufactured with 1% precision components. It is applied in UV testers, UV watches, outdoor sports equipment, mobile phones, and so on.
Module Source
Purchase link: https://item.taobao.com/item.htm?spm=a21n57.1.0.0.d0e2523cw99pzo&id=656169130215&ns=1&abbucket=0#detail Materials download link: https://pan.baidu.com/s/1YuwoCsbiJPaYH-8TaHEwVg Extraction code: 8888
Specifications
Operating voltage: 2.7-5V Operating current: 1mA Measurement angle: 130 degrees Temperature drift: 0.08%/℃ Detection wavelength range: 240nm~370nm Output method: ADC Number of pins: 3 Pin
View Materials
In the circuit diagram, the SIG pin outputs an analog signal after the analog voltage is amplified. After the analog value is collected, it is converted into voltage. According to the voltage and UV intensity comparison table below, the UV intensity can be determined.
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_ultraviolet.c and bsp_ultraviolet.h, and rename the folder to Ultraviolet.
Write Code
In the file bsp_ultraviolet.c, write the following code.
/*
* LCSC-Openkits (LCKFB) software and hardware materials and related expansion board software and hardware materials are all open source on the official website.
* Dev board official website: www.lckfb.com
* Technical support resides on the forum; any technical questions are welcome for exchange and learning at any time.
* LCKFB Forum: club.szlcsc.com
* Follow our Bilibili account: [LCSC-Openkits (LCKFB)] to keep up with our latest updates!
* We do not make money by selling boards; we take cultivating engineers as our mission.
* Change Logs:
* Date Author Notes
* 2024-01-05 LCKFB-lp first version
*/
#include "bsp_ultraviolet.h"
#include "stdio.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);
}
void delay_1ms(unsigned int ms)
{
vTaskDelay(ms / portTICK_PERIOD_MS);
}
void delay_1us(unsigned int us)
{
ets_delay_us(us);
}
/******************************************************************
* Function Name: ULTRAVIOLET_GPIO_Init
* Function Description: UV sensor module pin initialization
* Function Parameters: none
* Function Return: none
* Author: LC
* Notes:
******************************************************************/
void ULTRAVIOLET_GPIO_Init(void)
{
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 calculated correctly
esp_adc_cal_characterize(unit, atten, width, DEFAULT_VREF, adc_chars);
}
/******************************************************************
* Function Name: Get_ADC_Value
* Function Description: Calculate the average of ADC values and output
* Function Parameters: num = number of acquisitions
* Function Return: corresponding scanned ADC value
* Author: LC
* Notes: Error around 80mV
******************************************************************/
unsigned int Get_ADC_Value(unsigned int num)
{
long long Data=0;
int i = 0;
for( i = 0; i < num; i++ )
{
/* Read ADC value */
Data += adc1_get_raw(channel);
delay_1ms(1);
}
Data = Data/num;
return Data;
}
/******************************************************************
* Function Name: Get_Ultraviolet_Intensity
* Function Description: Determine the current UV intensity level
* Function Parameters: value = ADC read value
* Function Return: 0~11 UV intensity level from low to high, 11 is the highest
* Author: LC
* Notes: none
******************************************************************/
char Get_Ultraviolet_Intensity(uint16_t value)
{
char ret = 0;
if( value < 227 )//UV intensity level 0
{
ret = 0;
}
if( value >= 227 && value < 318 )//UV intensity level 1
{
ret = 1;
}
if( value >= 318 && value < 408 )//UV intensity level 2
{
ret = 2;
}
if( value >= 408 && value < 503 )//UV intensity level 3
{
ret = 3;
}
if( value >= 503 && value < 606 )//UV intensity level 4
{
ret = 4;
}
if( value >= 606 && value < 696 )//UV intensity level 5
{
ret = 5;
}
if( value >= 696 && value < 795 )//UV intensity level 6
{
ret = 6;
}
if( value >= 795 && value < 881 )//UV intensity level 7
{
ret = 7;
}
if( value >= 881 && value < 976 )//UV intensity level 8
{
ret = 8;
}
if( value >= 976 && value < 1079 )//UV intensity level 9
{
ret = 9;
}
if( value >= 1079 && value < 1170 )//UV intensity level 10
{
ret = 10;
}
if( value >= 1170 )//UV intensity level 11
{
ret = 11;
}
return ret;
}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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
In the file bsp_ultraviolet.h, write the following code.
/*
* LCSC-Openkits (LCKFB) software and hardware materials and related expansion board software and hardware materials are all open source on the official website.
* Dev board official website: www.lckfb.com
* Technical support resides on the forum; any technical questions are welcome for exchange and learning at any time.
* LCKFB Forum: club.szlcsc.com
* Follow our Bilibili account: [LCSC-Openkits (LCKFB)] to keep up with our latest updates!
* We do not make money by selling boards; we take cultivating engineers as our mission.
* Change Logs:
* Date Author Notes
* 2024-01-05 LCKFB-lp first version
*/
#ifndef _BSP_ULTRAVIOLET_H_
#define _BSP_ULTRAVIOLET_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 ULTRAVIOLET_SIG_PIN 1
#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
void ULTRAVIOLET_GPIO_Init(void);
unsigned int Get_ADC_Value(unsigned int num);
char Get_Ultraviolet_Intensity(uint16_t value);
void delay_us(unsigned int us);
void delay_ms(unsigned int ms);
void delay_1us(unsigned int us);
void delay_1ms(unsigned int ms);
#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
44
45
46
47
48
49
50
51
52
53
54
55
56
Porting Verification
In the main function of your own project, write the following code.
/*
* LCSC-Openkits (LCKFB) software and hardware materials and related expansion board software and hardware materials are all open source on the official website.
* Dev board official website: www.lckfb.com
* Technical support resides on the forum; any technical questions are welcome for exchange and learning at any time.
* LCKFB Forum: club.szlcsc.com
* Follow our Bilibili account: [LCSC-Openkits (LCKFB)] to keep up with our latest updates!
* We do not make money by selling boards; we take cultivating engineers as our mission.
* Change Logs:
* Date Author Notes
* 2024-01-05 LCKFB-lp first version
*/
#include <stdio.h>
#include "bsp_ultraviolet.h"
void app_main(void)
{
uint16_t value = 0;
ULTRAVIOLET_GPIO_Init();
delay_ms(1000);
printf("IRtracking demo start\r\n");
while(1)
{
//Acquire 5 times
value = Get_ADC_Value(5);
printf("Grade = %d\r\n", Get_Ultraviolet_Intensity( value ) );
printf("Value = %d\r\n", value);
value = 0;
delay_ms(1000);
}
}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
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.