Read more about the Modbus RTU here.
- ESP-Wroom-32 (or Arduino Uno)
- USB to Micro USB cable
πOptional
- Max 485 Module
- USB to RS-485 converter
- Max7219 LED matrix
- HCSR-04 Ultra Sonic Sensor
- C# with Visual Studio 2019
- C++ with Visual Studio Code and PlatformIO extension installed [tutorial here]
- NModbus4 from NuGet ( tutorial ) in C# WinForms
- Modbus Master Slave for Arduino github repo (setup in VScode tutorial)
- Example 1 uses this library with Platform IO for max7219 module
- Example 2 uses this library with Platform IO for HCSR 04 module
Connect your USB to USB micro With the Esp32 and plug in to one of your PC Ports (done). Simple yet limited by the USB cable length.
Important Note: Upload the code before connecting the RX0 and TX0 pins
Connect the max 485 module to the ESP 32 like so :
-
Bridge the RE and DE connections in the breadboard
-
Connect that bridge to the GPIO Pin 4 of the ESP32
-
RX0 (ESP32 ) --> R0 (Max485 Module)
-
TX0 (ESP32 ) --> DI (Max485 Module)
-
Connect A and B pins from the Max485 module to the A(D+) and B(D-) of the USB to RS485 module
- Open Project : Modbus-implementation / Simple_Example /Simple Modbus Slave Esp32 With PlatformIO
- Upload the code to your ESP32
- Open GUI: Modbus-implementation / Simple_Example /
- Connect and do Read and Write Holding Registers operations
On the ESP32 side of the code, the serial begin baudrate must match the selected baudrate in the GUI application. Also the COMport must not be in use by other software
Also, the slave ID in the ESP32 must match the ID of the called function in C# .
Technically, you can connect over 240 slave devices on the same serial connection, all you have to do is change the slave ID for each one and call the specific device by that unique ID from the GUI.
Module-->ESP32
- Vcc --> +5v
- Gnd --> GND
- DIN --> GPIO 23
- CS --> GPIO 5
- CLK --> GPIO 18
Each of the numbers in the Modbus RTU message represents the bit position of the matrix but expressed in decimal form.
How are the bit values placed?
Take a look at Column 0 and Rows from 0 to 7. The first 2 LED-s are OFF thus 00 , next 4 LED-s are ON so 1111, and lastly 2 LEDs are OFF thus 00.
So the entire message is 00111100 in binary, or 60 in decimal form, which in terms represents our first RTU message, audata16[0]=60 next Column 0 is 01000010 in binary, or 66 in decimal form and so on .
Upload the code from Example1/ESP32 Code With VScode/ to the ESP32
- Open the programm located here: Example1/GUI Led matrix/WindowsFormsApp1/ with Visual studio 2019
- or directly : Example1/GUI Led matrix/WindowsFormsApp1//bin/Debug/WindowsFormsApp1.exe
- Connect with your settings tuch the interactive LEDs and Send(Message)
- Vcc --> +5v
- Gnd --> GND
- Trig --> GPIO 5
- Echo --> GPIO 18
Timers from Winforms are used to request a register from a slave device (ESP32 in our case), we can see the update interval in which these registers are requested from the slave device, and the response is used to update the user interface.
As stated earlier it's not recommended to use the delay function when using the Modbus slave library so instead we used millis()(how to) , to update Modbus registers and the HCSR 04 readings.
Upload the code from Example2/Modbus Rtu With Hc-sr04 to the ESP32
- Open the programm located here: Example2/HCSR04DisplayReadings with Visual studio 2019
- or directly : Example2/HCSR04DisplayReadings/bin/Debug/HCSR04DisplayReadings.exe
- Connect with your settings and dont forget to set the request interval the unit is miliseconds
- To call the first device (ID 1) change the parameters to the the shown function
- To call the second device (ID 2) change the parameters to the the shown function
- To call the third device (ID 3) change the parameters to the the shown function