Skip to main content

ESP32-S3-Relay-6CH Working with MicroPython

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

MicroPython Getting Started​

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

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

Setting Up the Development Environment​

1. Configure Thonny and Flash MicroPython Firmware​

2. Additional Tips​

  • The development board currently uses custom firmware for development. The firmware is available in the ESP32-S3-Relay-6CH Example. Note that the download address is the 0x00 position.
  • Firmware path: ../ESP32-S3-Relay-6CH-Demo/MicroPython/Firmware
  • The bin file under Firmware, which is at the same level as the Arduino and MicroPython folders, is for testing onboard functions and do not need to be flashed here.
info

Refer to MicroPython Firmware Compilation for more learning resources.

Example​

Example

ESP32-S3-Relay-6CH Example Programs

ExampleBasic Description
main.pyMain function
ws_bluetooth.pyBluetooth initialization and command parsing
ws_gpio.pyRGB, buzzer, and relay initialization
ws_information.pyGlobal system parameter settings
ws_relay.pyImplements relay control
ws_serial.py8-channel relay expansion

Uploading the Example​

  • To upload local files to the development board, select the file, right-click, and find "upload to/" to upload.

    ESP32-S3-Relay-6CH TO MicroPython Environment 8

  • The following is the interface after all files are uploaded. The uploaded files must match those in the image; otherwise, it may fail to run.

    ESP32-S3-Relay-6CH TO MicroPython Environment 10

Running the Example​

After running the example, you can control the onboard relays through RS485 or Bluetooth.

RS485 Control​

Connect an external RS485 device to the ESP32-S3-Relay-6CH to control its relays. Send control commands to toggle or change the state of each relay. The default baud rate is 9600.

Control Command Table

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

This example uses a USB TO RS232/485 Converter for the connection demonstration. Match the pins as follows:

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

Software Operation

  1. Open SSCOM, select the COM port corresponding to Port B, and set the baud rate to 9600.

  2. Open the serial port, prepare relay commands in the multi-send list, and enable hexadecimal (HEX) sending.

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

  3. Click the send button beside the required command to control the relays.

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

Bluetooth Control​

Use the mobile nRF Connect app to demonstrate Bluetooth control. Other Bluetooth debugging apps can also be used.

tip

Some Bluetooth debugging apps send data as ASCII strings by default, while others send hexadecimal data. Choose the command format supported by the app.

Control Command Table

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

Operation Steps

The example uses the mobile nRF Connect app for demonstration. Other Bluetooth debugging apps can also be used.

  1. Open nRF Connect, scan for Bluetooth devices, and connect to ESP32 S3 Relay 6CH.

    800px-ESP32-S3-Relay-6CH_TO_Bluetooth_1

  2. Read the device IP (optional): After connecting, expand Unknown Service and tap the read button (down arrow). If Wi-Fi connects successfully, the device returns its IP address. If Wi-Fi fails to connect for a long time, the RGB LED remains red and the read returns no data.

    800px-ESP32-S3-Relay-6CH_TO_Bluetooth_2

  3. Send a control command: The relay commands are characters 1 to 8 (hexadecimal 0x31 to 0x38). Tap the send button (up arrow), select the hexadecimal BYTE data type, and enter 31.

    800px-ESP32-S3-Relay-6CH_TO_Bluetooth_3

  4. Sending 0x31 toggles the state of relay CH1.

    800px-ESP32-S3-Relay-6CH_TO_Bluetooth_4

  5. To turn off all relays, send 0x38.

    800px-ESP32-S3-Relay-6CH_TO_Bluetooth_5