Skip to main content

ESP32-S3-Relay-6CH Working with Arduino

This chapter includes the following sections. Please read as needed:

Arduino Getting Started​

New to Arduino ESP32 development and looking for a quick start? We have prepared a comprehensive Getting Started Tutorial for you.

Note: This tutorial uses the ESP32-S3-Zero as a reference example, and all hardware code is based on its pinout. Before you start, we recommend checking the pinout of your development board to ensure the pin configuration is correct.

Setting Up the Development Environment​

1. Install and Configure Arduino IDE​

Please refer to the tutorial Install and Configure Arduino IDE to download and install the Arduino IDE and add ESP32 support.

  • Board installation instructions for ESP32-S3-Relay-6CH
Board NameBoard Installation RequirementVersion Requirement
esp32 by Espressif Systems"Offline installation" / "Online installation"2.0.12

2. Installing Libraries​

  • When installing Arduino libraries, there are typically two options: Online Installation and Offline Installation. If the library installation requires offline installation, you must use the provided library files.
    For most libraries, users can easily search and install them via the Arduino software's built-in Library Manager. However, some open-source or custom libraries are not synchronized to the Arduino Library Manager, so they cannot be found through online search. In this case, you can only install these libraries manually offline.
  • For a library installation tutorial, see the library installation section in the Install and Configure Arduino IDE.
  • The ESP32-S3-Relay-6CH library files are stored in the example program. Click here to go to: ESP32-S3-Relay-6CH Example.
  • Library installation instructions for ESP32-S3-Relay-6CH
Library NameDescriptionVersionLibrary Installation Requirement
ArduinoJsonLightweight JSON libraryv6.21.4Online or offline installation
PubSubClientMQTT message publish/subscribe libraryv2.8.0Online or offline installation
NTPClientNetwork time synchronization client libraryv3.2.1Online or offline installation
info

For more LVGL learning and usage, refer to the LVGL Official Documentation.

Example​

Example

ESP32-S3-Relay-6CH Example Programs

ExampleBasic DescriptionDependency Library
01_MAIN_WIFI_APRS485 interface control, Bluetooth control, Bluetooth IP transmission, Web page control (short range)Can be flashed directly. The Web page can only be used when connected to the device's Wi-Fi
02_MAIN_WIFI_STARS485 interface control, Bluetooth control, Bluetooth IP transmission, Web page control (short range)Requires modifying the Wi-Fi to connect to. The Web page is for intranet use only
03_MAIN_WIFI_MQTTRS485 interface control, Bluetooth control, Bluetooth IP transmission, Waveshare Cloud control (long range)Requires modifying the Wi-Fi to connect to. A device must be created in Waveshare Cloud. (Note: Waveshare Cloud is no longer maintained. Please choose another cloud service.)
04_MAIN_ALLRS485 interface control, Bluetooth control, Bluetooth IP transmission, Web page control (short range), Waveshare Cloud control (long range)Requires modifying the Wi-Fi to connect to. A device must be created in Waveshare Cloud. The Web page is for intranet use only. (Note: Waveshare Cloud is no longer maintained. Please choose another cloud service.)

Arduino Project Settings​

ESP32-S3-Relay-6CH-demo-10

01_MAIN_WIFI_AP​

Example Description

  • This example implements controlling the switches of 6 relays via Wi-Fi, Bluetooth, and RS485. This example enables the Wi-Fi AP mode.

Note

  • Can be flashed directly.
  • The Web page can only be used when connected to the device's Wi-Fi.

Code Analysis

  • Relay_Analysis(): This function is mainly used to analyze the received data and, based on the data content, execute the corresponding relay control operations and output the corresponding status prompt information.

    • Parameter Analysis
      • uint8_t *buf: A pointer to an unsigned 8-bit integer array. This array should store the received data. The function determines the specific command content based on the value of the first element of the array (buf[0]).
      • uint8_t Mode_Flag: A mode flag indicating the data source. By checking this flag, it outputs the corresponding data source prompt information (such as Bluetooth data, Wi-Fi data, or RS485 data), and performs different relay operations according to different commands.
    • Logic Flow
      • First, output the corresponding data source prompt information based on the value of Mode_Flag.
      • Then, use a switch statement to execute different operations based on the value of buf[0]:
        • For cases CH1 to CH6, use the digitalToggle function to toggle the level state of the corresponding GPIO pin (such as GPIO_PIN_CH1, etc.), update the corresponding Relay_Flag array element to record the change in relay state, call the Buzzer_PWM function to control the buzzer, and output the corresponding on or off prompt information based on the final relay state.
        • For the ALL_ON command, set all GPIO pins (corresponding to the 6-channel relays) to high level (on state), use the memset function to set all elements of the Relay_Flag array to 1, indicating that all relays are on, output a prompt that all relays are on, and control the buzzer.
        • For the ALL_OFF command, similarly set all relevant GPIO pins to low level (off state), update the Relay_Flag array elements to 0, output a prompt that all relays are off, and control the buzzer, with two additional buzzer control operations (with a delay in between).
        • If the value of buf[0] does not match the above command cases, output a prompt indicating that non-command data was received.
  • WIFI_Init (): Configures the device as a Wi-Fi access point (AP), sets up a Web server, and sets request handler functions for different paths, implementing relevant network functions and device control functions, while providing corresponding status prompts.

    • AP Creation
      • First set the Wi-Fi mode to WIFI_AP, then attempt to create a soft AP via WiFi.softAP(ssid, password). If it fails, it will loop with a prompt and retry until successful.
    • Parameter Configuration and Prompts
      • After successfully creating the AP, provide a prompt using the RGB LED (green light for 1 second).
    • IP Address Display
      • Get and format the soft AP's IP address, store it in ipStr, and output it for display, making it easy to know the network address.
    • Web Server Setup
      • Use server.on to set corresponding request handler functions (such as handleRoot, handleGetData, etc.) for multiple paths (such as "/", "/getData", etc.). Each function should be defined elsewhere for different functional operations, such as returning pages, getting data, controlling switches, etc.
      • Finally, call server.begin() to start the Web server and output a startup prompt.

RS485 Control​

Use an RS485 device to connect to the ESP32-S3-Relay-6CH device. Sending data to the ESP32-S3-Relay-6CH can control the opening and closing of each relay. Communication is performed at a default baud rate of 9600.

Operation CommandCommand Function
06 05 00 01 55 00 A2 EDToggle CH1 relay state
06 05 00 02 55 00 52 EDToggle CH2 relay state
06 05 00 03 55 00 03 2DToggle CH3 relay state
06 05 00 04 55 00 B2 ECToggle CH4 relay state
06 05 00 05 55 00 E3 2CToggle CH5 relay state
06 05 00 06 55 00 13 2CToggle CH6 relay state
06 05 00 FF FF 00 BD BDTurn on all relays
06 05 00 FF 00 00 FC 4DTurn off all relays
  • Hardware Connection

The example uses a USB TO RS232/485 Converter for demonstration.

ESP32-S3-Relay-6CHUSB-TO-RS232-485
RS485 - A+Tx B - A+
RS485 - B-Rx B - B-
  • Software Operation

    • Use SSCOM for data sending

    • Open the SSCOM software and select the COM port corresponding to the Port B connected above.

    • Open the serial port and use the multi-send function to quickly send commands

      ESP32-S3-Relay-6CH-demo-01

  • The following commands can be used for relay control.

    ESP32-S3-Relay-6CH-demo-02

Bluetooth Control​

Use a mobile Bluetooth debug assistant to connect to the ESP32-S3-Relay-6CH device. Sending data to the ESP32-S3-Relay-6CH can control the opening and closing of each relay. Please note: some Bluetooth debug assistants send data in ASCII format by default. Before controlling the device, you need to enter the correct control commands according to the Bluetooth debug assistant.

ASCIIHexCommand Function
10x31Toggle CH1 relay state
20x32Toggle CH2 relay state
30x33Toggle CH3 relay state
40x34Toggle CH4 relay state
50x35Toggle CH5 relay state
60x36Toggle CH6 relay state
70x37Turn on all relays
80x38Turn off all relays
  • Software Operation (View IP)
    • Use the mobile Bluetooth debug assistant nRF Connect for relay control (other Bluetooth debug assistants can also be used).
    • The following uses nRF Connect for functional demonstration.
    • Connect to the Bluetooth device named ESP32 S3 Relay 6CH.

800px-ESP32-S3-Relay-6CH_TO_Bluetooth_1

  • After successful connection, select Unknown Service, click to read data. If Wi-Fi connection fails for a long time, the RGB will stay red, and this operation will have no response.
  • When Wi-Fi is successfully connected, the device IP will be received as follows. The device IP is 192.168.6.133.

800px-ESP32-S3-Relay-6CH_TO_Bluetooth_2

  • The Bluetooth relay control commands are characters 1-8, i.e., hexadecimal 0x31 - 0x38.
  • Click the send button, select the hexadecimal BYTE data type, and enter 31 to send the CH1 toggle command.

800px-ESP32-S3-Relay-6CH_TO_Bluetooth_3

  • Sending 0x31 can control relay CH1 to toggle its state.

800px-ESP32-S3-Relay-6CH_TO_Bluetooth_4

  • Sending 0x38 can control turning off all relays.

800px-ESP32-S3-Relay-6CH_TO_Bluetooth_5

Web Page Control​

Use a mobile Bluetooth debug assistant to connect to the ESP32-S3-Relay-6CH device, obtain the IP after connecting to Wi-Fi, and open the Web page via the IP.

  • AP Mode

    • Connect to the Wi-Fi of the ESP32-S3-Relay-6CH device. The Wi-Fi name is ESP32-S3-Relay-6CH, and the password is waveshare.

    ESP32-S3-Relay-6CH_TO_Web_0

    • Obtain the current IP through the Bluetooth debug assistant.
    • Open the Web page to control the relays. (After the device is powered on, it takes a short time to configure. The first time the Web page is opened after each power-on may be slow.)

    ESP32-S3-Relay-6CH_TO_Web_1

  • STA Mode

    • After the device is powered on, it will automatically connect to the configured Wi-Fi (modify the Wi-Fi to connect to before flashing the program).
    • Obtain the current IP through the Bluetooth debug assistant.
    • Open the Web page to control the relays. (After the device is powered on, it takes a short time to configure. The first time the Web page is opened after each power-on may be slow.)

    ESP32-S3-Relay-6CH_TO_Web_2

02_MAIN_WIFI_STA​

Example Description

  • This example implements controlling the switches of 6 relays via Wi-Fi, Bluetooth, and RS485. This example enables the Wi-Fi STA mode.

Note

  • Requires modifying the Wi-Fi to connect to.
  • The Web page only supports controlling the device when it is on the same network as this product. If using a mobile phone for control, mobile data must be turned off.

Code Analysis

  • WIFI_Init (): Makes the device connect to the specified Wi-Fi network in station (STA) mode. If the connection is successful, it performs subsequent network-related configurations (such as obtaining the IP address, starting the Web server, and registering callback functions, etc.). If the connection fails, it provides a corresponding prompt and sets the connection status flag. During the process, it also provides visual prompts of the connection status via the RGB LED.
    • Wi-Fi Network Connection Attempts:
      • First set Wi-Fi to WIFI_STA mode and enable sleep mode, then start connecting to the specified network. During the loop waiting for a successful connection, output a dot as a prompt every half second. On every even-numbered attempt (except the first), briefly light the RGB LED red as a prompt. After every 10 failed attempts, disconnect and reconnect. If the number of attempts exceeds 22, consider the connection failed and exit the loop.
    • Operations After Successful Connection:
      • If the number of connection attempts is less than 23 (i.e., connection successful), set WIFI_Connection to 1, light the green LED for 1 second as a success prompt, then obtain and display the local IP address. Next, register callback functions for multiple paths with the Web server (such as the root path, get data, control different switches, etc.). Finally, start the server and output a startup prompt, enabling the device to accept corresponding control through the web page.
    • Operations After Connection Failure:
      • If the number of attempts is greater than or equal to 23 (connection failed), set WIFI_Connection to 0, output a prompt informing that the device can be controlled via a Bluetooth debug assistant, and light the red LED to indicate the connection failure status.

RS485 Control

Bluetooth Control

Web Page Control

03_MAIN_WIFI_MQTT​

warning

Note: Waveshare Cloud is no longer maintained. Please choose another cloud service.

Example Description

  • This example controls 6 relays via MQTT, Bluetooth, and RS485 communication. It uses the ESP32 as the main control unit, supports Wi-Fi and Bluetooth connections, and provides remote control based on the MQTT protocol.

Notes

  • Requires modifying the Wi-Fi to connect to.
  • A device must be created in Waveshare Cloud.

Code Analysis

  • Relay_Analysis(): Receives data from different communication sources and executes the corresponding relay control operations.

    • Data Downlink: Commands (such as CH1, ALL_ON, etc.) are sent to the device via Bluetooth, Wi-Fi, or RS485. After receiving the command, the device parses and executes the corresponding operation. For example, when receiving the CH1 command, the function toggles the state of relay 1.
    • Data Feedback: The execution of control commands is printed to the serial monitor via printf (e.g., relay status update: "Relay CH1 on") and fed back via the buzzer (Buzzer_PWM). This allows users to see real-time status updates.
    • Communication Protocols
      • Bluetooth: Wireless communication with a mobile phone or other devices via the Bluetooth module. The device controls the relays after receiving commands.
      • MQTT: Connects to an MQTT server using Wi-Fi. The device subscribes to specific topics (such as relay control commands). When a new command is published, the device receives the message via MQTT and executes relay control.
      • RS485: The device receives RS485 commands through the serial port and toggles relay states according to the commands.
  • setup()

    • Initializes the various modules required by the system, including serial, GPIO, RTC, Bluetooth, and Wi-Fi (MQTT).
      • Calls Bluetooth_Init(), allowing the device to establish connections with other Bluetooth devices and receive control commands.
      • Through MQTT_Init(), the device connects to the Wi-Fi network and can communicate with a remote server via the MQTT protocol.
    • Time Synchronization
      • If the system is connected to Wi-Fi and RTC is enabled, the Acquisition_time() function synchronizes the current time to the RTC over the network, ensuring the device has accurate system time.
    • Data Upload and Downlink
      • Upload: When the device successfully connects to Wi-Fi or Bluetooth, it can upload device status information or sensor data (e.g., temperature, humidity) via MQTT. This data is periodically sent to the MQTT server for other systems or users to view.
      • Downlink: The device receives control commands from the MQTT server and executes corresponding operations, such as switching relays or changing configurations.

RS485 Control

Bluetooth Control

04_MAIN_ALL​

warning

Note: Waveshare Cloud is no longer maintained. Please choose another cloud service.

Example Description

  • This example is a collection of all functions: RS485 interface control, Bluetooth control, Web page control (short range), and Waveshare Cloud control (long range).

Notes

  • Requires modifying the Wi-Fi to connect to.
  • The Web page only supports controlling the device when it is on the same network as this product. If using a mobile phone for control, mobile data must be turned off.
  • A device must be created in Waveshare Cloud.

RS485 Control

Bluetooth Control

Web Page Control