Rain Sensor
The rain sensor is mainly used to detect whether it is raining and the amount of rainfall. It is mainly used in automotive intelligent lighting (AFS) systems, automotive automatic wiper systems, and smart window systems. This rain sensor is basically a board coated with nickel in a linear pattern. The common working principle of a rain sensor is to determine whether it is raining by detecting the conductivity of water droplets. It uses the change in conductivity between two electrodes to measure the presence of water droplets. There is an air gap between the two electrodes, and under normal conditions it is in an open-circuit state. When a water droplet contacts the electrodes, the conductivity of the water droplet causes current to flow through the water droplet to form a current loop, thereby changing the resistance value between the electrodes. This also changes the voltage drop across its two ends.
Module Source
Purchase link: https://detail.tmall.com/item.htm?abbucket=0&id=41266204564&ns=1&spm=a21n57.1.0.0.4c52523cd1r9Zc Materials download link: https://pan.baidu.com/s/10bjbsmcOh2N7YGDS3PquPw Materials extraction code: psfm
Specifications
Operating voltage: 3.3V-5V
Detection distance: 1 meter
Output method: DO interface is digital output
AO interface is analog output
Read method: ADC and digital value (0 and 1)
Number of pins: 4 Pin (2.54mm pitch header)
Principle Analysis
This module is based on the LM393 operational amplifier. It includes an electronic module and a printed circuit board that "collects" raindrops. When raindrops accumulate on the circuit board, they form parallel resistance paths that can be measured by the operational amplifier.
There are two indicators on the control board: the power indicator PWR-LED and the output signal indicator DO-LED. The power indicator stays on after powering on, and the output signal indicator is off when there is no rain; when raindrops fall on it, the output signal indicator turns on. The raindrop board and the control board are separate, making it convenient to route wires out, and the large-area raindrop board is more conducive to detecting rainwater.
There are two outputs on the control board: digital output DO and analog output AO. When connected to 5V power, the power light turns on; when there are no water droplets on the sensing board, DO outputs high level; when a drop of water is dripped on it, DO outputs low level; when the water droplets are brushed off, it returns to the high-level output state. The sensitivity can be adjusted through the blue variable resistor.
AO analog output is connected to the analog input port of the microcontroller. By comparing the digital value converted from the analog value, the amount of rain dripping on it can be detected. The heavier the rain, the smaller the resistance value, and the larger the digital value converted from the analog value. Different values correspond to different rainfall amounts in millimeters, which requires physical measurement. The placement of the raindrop board will affect the results differently, and this is not studied here.
The corresponding schematic diagram is shown in Figure 2.17.3.1-2. The AO output is the voltage directly output by the rain sensor, 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. For the specific principle, see section 2.3.3.1 View Materials in the Photoresistor Light Sensor chapter.
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_raindrop.c and bsp_raindrop.h, and change the folder name to raindrop.
Write Code
In the file bsp_raindrop.c, write the following code.
#include "bsp_raindrop.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: raindrop_gpio_config
* Function Description: Initialize rain sensor pins
* Function Parameters: none
* Function Return: none
* Author: LC
* Notes: none
******************************************************************/
void raindrop_gpio_config(void)
{
gpio_config_t Raindrop_config = {
.pin_bit_mask = (1ULL<<Raindrop_GPIO_DO), // Configure pin
.mode =GPIO_MODE_INPUT,
.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(&Raindrop_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_value
* Function: Read ADC value
* Parameters: Channel to be sampled
* Function Return: Measured value
* Author: LC
* Notes: none
**********************************************************/
unsigned int get_adc_value(uint8_t channel_X)
{
unsigned int adc_value = 0;
// Read sampled value
adc_value = adc1_get_raw(channel_X);
// Return sampled value
return adc_value;
}
/******************************************************************
* Function Name: get_raindrop_percentage_value
* Function Description: Read rain sensor AO value and return percentage
* Function Parameters: none
* Function Return: returns percentage
* Author: LC
* Notes: none
******************************************************************/
unsigned int get_raindrop_percentage_value(void)
{
int adc_max = 4095;
int adc_new = 0;
int Percentage_value = 0;
adc_new = get_adc_value( channel );
Percentage_value = (1-((float)adc_new/adc_max)) * 100;
return Percentage_value;
}
/******************************************************************
* Function Name: get_raindrop_do_value
* Function Description: Read rain sensor DO value, returns 0 or 1
* Function Parameters: none
* Function Return:
* Author: LC
* Notes: none
******************************************************************/
unsigned char get_raindrop_do_value(void)
{
return gpio_get_level(Raindrop_GPIO_DO);
}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
In the file bsp_raindrop.h, write the following code.
#ifndef _BSP_RAINDROP_H__
#define _BSP_RAINDROP_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 Raindrop_GPIO_AO 1
#define Raindrop_GPIO_DO 2
#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 delay_us(unsigned int us);
void delay_ms(unsigned int ms);
void raindrop_gpio_config(void);
unsigned int get_raindrop_percentage_value(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
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-02 LCKFB-lp first version
*/
#include <stdio.h>
#include "bsp_raindrop.h"
void app_main(void)
{
// ADC interface initialization
raindrop_gpio_config();
while(1)
{
printf("raindrop = %d%%\r\n", get_raindrop_percentage_value() );
delay_ms(500);
}
}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
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.