Relay Module
A relay is an electronic component that controls a circuit. It senses changes in the input current to control the on/off state of a circuit. It is commonly used in automated control circuits and plays roles such as automatic regulation, isolation, safety protection, and conversion. A DC relay usually has 5 pins, of which two are coil control pins; the other three are the normally open (NO) terminal, the normally closed (NC) terminal, and the common (COM) terminal. When unpowered, the coil is not energized, the NC terminal and COM terminal are connected, and the NO terminal and COM terminal are disconnected; when powered, the coil is energized, the NO terminal and COM terminal are connected, and the NC terminal and COM terminal are disconnected. Therefore, a relay is essentially a switch that can use low voltage to control high voltage.
Module Source
Purchase link: https://item.taobao.com/item.htm?spm=a1z10.5-c-s.w4002-24706531925.33.886b4450Jc12tZ&id=546724904969 Materials download link: http://pan.baidu.com/share/link?shareid=3950641169&uk=2302102993
Specifications
Operating voltage: 5V Controllable AC voltage range: up to 250V, 10A Controllable DC voltage range: up to 30V, 10A Control method: GPIO Number of pins: 4 Pin (2.54mm pitch header)
Note: Uses optical coupler isolation to protect the MCU pins; uses a transistor driver;
View Materials
If the relay is energized at a low-voltage power source, the electromagnet gains magnetic force, attracts the armature, and the contacts close, allowing the high-voltage power source to form a loop in a conductive state; if the relay is de-energized, the electromagnet loses its magnetic force. Due to the spring, the spring disconnects the contacts, and the high-voltage power source does not form a loop, remaining in a disconnected state;
Now that we understand the control principle, let's look at the schematic. K1 is the relay, where pins 4 and 5 are the relay's low-voltage power control pins. When IN1 outputs a low level, pins 1 and 2 of opto-isolator U1 conduct, causing pins 4 and 3 to conduct. VCC flows through pin 4 to pin 3 and through R2 to power the base of the transistor. Because the transistor's base is powered, the transistor turns on, and VCC flows from the relay's pin 4 to pin 5, through the transistor to ground. Therefore, the relay coil is energized, and the relay's contact at pin 1 moves from pin 2 to pin 3, achieving our purpose of switch control. In the schematic, pin 1 of terminal P1 is the normally closed contact, pin 2 is the common contact, and pin 3 is the normally open contact. The corresponding wiring diagram is shown in the right figure below.
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.
Just note that here we change the file names bsp_dht11.c and bsp_dht11.h to bsp_relay.c and bsp_relay.h, and the folder name to RELAY.
Write Code
In the file bsp_relay.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-12 LCKFB-lp first version
*/
#include "bsp_relay.h"
#include "stdio.h"
static void delay_ms(unsigned int ms)
{
vTaskDelay(ms / portTICK_PERIOD_MS);
}
static void delay_us(unsigned int us)
{
ets_delay_us(us);
}
static void delay_1ms(unsigned int ms)
{
vTaskDelay(ms / portTICK_PERIOD_MS);
}
static void delay_1us(unsigned int us)
{
ets_delay_us(us);
}
/******************************************************************
* Function Name: relay_GPIO_Init
* Function Description: Initialize the relay pin
* Function Parameters: None
* Function Return: None
* Author: LC
* Notes: None
******************************************************************/
void Relay_GPIO_Init(void)
{
gpio_config_t lll_config = {
.pin_bit_mask = (1ULL<<GPIO_RELAY_IN), // Configure pin
.mode =GPIO_MODE_OUTPUT, // Output mode
.pull_up_en = GPIO_PULLUP_ENABLE, // Enable pull-up
.pull_down_en = GPIO_PULLDOWN_DISABLE, // Disable pull-down
.intr_type = GPIO_INTR_DISABLE // Disable pin interrupt
};
gpio_config(&lll_config);
}
/******************************************************************
* Function Name: Set_Relay_Switch
* Function Description: Set the relay state
* Function Parameters: 0 relay engaged 1 relay released
* Function Return:
* Author: LC
* Notes:
******************************************************************/
void Set_Relay_Switch(unsigned char state)
{
RELAY_OUT(state);
}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
In the file bsp_relay.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-12 LCKFB-lp first version
*/
#ifndef _BSP_RELAY_H_
#define _BSP_RELAY_H_
#include <stdio.h>
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/uart.h"
#include "driver/gpio.h"
#include "driver/i2c.h"
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "freertos/queue.h"
#include <inttypes.h>
#include "sdkconfig.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "rom/ets_sys.h"
#include "esp_system.h"
#include "driver/gpio.h"
#include "driver/spi_master.h"
#include "driver/spi_common.h"
#include "hal/gpio_types.h"
#include "string.h"
#define GPIO_RELAY_IN 1
#define RELAY_OUT(x) gpio_set_level(GPIO_RELAY_IN, x?1:0)
void Relay_GPIO_Init(void);// Initialize the relay pin
void Set_Relay_Switch(unsigned char state);// Set the relay state
#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
Porting Verification
In the main function of your 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-12 LCKFB-lp first version
*/
#include <stdio.h>
#include "bsp_relay.h"
#include "string.h"
#include "esp_private/esp_task_wdt.h"
#include "esp_private/esp_task_wdt_impl.h"
int app_main(void)
{
esp_task_wdt_deinit();
Relay_GPIO_Init(); // Initialize the relay
printf("Start......\r\n");
while(1)
{
Set_Relay_Switch(0);// Control relay to engage
vTaskDelay(2000 / portTICK_PERIOD_MS);
printf("\r\nRelay engaged!\r\n");
Set_Relay_Switch(1);// Control relay to release
vTaskDelay(2000 / portTICK_PERIOD_MS);
printf("\r\nRelay released!\r\n");
}
}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
Power-on behavior:
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.