# UART TTL to RS485 Two-way Converter Module + Nodemcu

**URL:** <https://forum.arduino.cc/t/uart-ttl-to-rs485-two-way-converter-module-nodemcu/1079783>\
**Category:** Networking, Protocols, and Devices\
**Created:** [January 19, 2023, 7:31pm UTC](https://forum.arduino.cc/t/uart-ttl-to-rs485-two-way-converter-module-nodemcu/1079783 "2023-01-19T19:31:41Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![sefaguntepe](https://avatars.discourse-cdn.com/v4/letter/s/aca169/32.png) [@sefaguntepe](https://forum.arduino.cc/u/sefaguntepe)\
**Post date:** [January 19, 2023, 7:31pm UTC](https://forum.arduino.cc/t/uart-ttl-to-rs485-two-way-converter-module-nodemcu/1079783/1 "2023-01-19T19:31:41Z")

</div>

Hello, I want to read and write data from the CNC machine with the RS485 converter in the image. Can anyone share sample code?

 ![imagen](https://europe1.discourse-cdn.com/arduino/original/4X/e/e/9/ee9ba26db6b27750c5f2930018134b7b50943058.jpeg)

---

<div class="post-metadata">

**Author:** ![horace](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/horace/32/1014299_2.png) [@horace](https://forum.arduino.cc/u/horace)\
**Post date:** [January 19, 2023, 8:38pm UTC](https://forum.arduino.cc/t/uart-ttl-to-rs485-two-way-converter-module-nodemcu/1079783/2 "2023-01-19T20:38:02Z")

</div>

try a web search for _arduino rs485_ you will get plenty of links plus sample code  
you may find information and sample code for a different RS485 breakout module, e.g.[rs-485-arduino-quick-primer](https://www.codrey.com/learn/rs-485-arduino-quick-primer/), which had two extra pins RE and DE to enable the transmitter and receiver - you can ignore these  
a USB-RS485 dongle for a PC s useful for testing and debugging code  
do you have information on the protocol used by the CNC machine?  
what Arduino are you planning to use? recommend you use one with hardware serial ports, e.g. Mega, ESP32, etc

---

<div class="post-metadata">

**Author:** ![sefaguntepe](https://avatars.discourse-cdn.com/v4/letter/s/aca169/32.png) [@sefaguntepe](https://forum.arduino.cc/u/sefaguntepe)\
**Post date:** [January 19, 2023, 9:01pm UTC](https://forum.arduino.cc/t/uart-ttl-to-rs485-two-way-converter-module-nodemcu/1079783/3 "2023-01-19T21:01:18Z")

</div>

I am currently getting data using the product in the image. However, I think I will get better performance with the first product I sent. Can I use the same codes?

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/e/b/0/eb0a07eae1f1db49e79616ef3f46ec321d7c985b.jpeg)

---

<div class="post-metadata">

**Author:** ![horace](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/horace/32/1014299_2.png) [@horace](https://forum.arduino.cc/u/horace)\
**Post date:** [January 19, 2023, 9:07pm UTC](https://forum.arduino.cc/t/uart-ttl-to-rs485-two-way-converter-module-nodemcu/1079783/4 "2023-01-19T21:07:47Z")

</div>

the board in the first image should be easier to use as you don't have the pins RE and DE to enable the transmitter and receiver - I assume that the internal logic of the first board sorts this out for you  
just connect TXD and RXD to your host microctroller and you should be able to transmit and receive RS485 data with a simple program  
what arduino are you using?

---

<div class="post-metadata">

**Author:** ![sefaguntepe](https://avatars.discourse-cdn.com/v4/letter/s/aca169/32.png) [@sefaguntepe](https://forum.arduino.cc/u/sefaguntepe)\
**Post date:** [January 19, 2023, 9:19pm UTC](https://forum.arduino.cc/t/uart-ttl-to-rs485-two-way-converter-module-nodemcu/1079783/5 "2023-01-19T21:19:05Z")

</div>

I am using ESP8266 12-E  
Lib: OTA+MQTT+WifiManager

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/c/5/3/c531ef84870c8dd734160a77f9526a7cabe318e8.jpeg)

---

<div class="post-metadata">

**Author:** ![horace](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/horace/32/1014299_2.png) [@horace](https://forum.arduino.cc/u/horace)\
**Post date:** [January 19, 2023, 9:35pm UTC](https://forum.arduino.cc/t/uart-ttl-to-rs485-two-way-converter-module-nodemcu/1079783/6 "2023-01-19T21:35:49Z")

</div>

try this code

```cpp
// ESP8266 SoftwareSerial
// https://circuits4you.com/2016/12/14/software-serial-esp8266/

#include <SoftwareSerial.h>

// pins Rx GPIO14 (D5) and Tx GPIO 12 (D6)
SoftwareSerial swSer(14, 12);  

void setup() {
  Serial.begin(115200); //Initialize hardware serial with baudrate of 115200
  swSer.begin(115200); //Initialize software serial with baudrate of 115200
  Serial.println("\nESP8266 Software serial test started");
}

void loop() {
  while (swSer.available() > 0) { //wait for data at software serial
    Serial.write(swSer.read()); //Send data recived from software serial to hardware serial    
  }
  while (Serial.available() > 0) { //wait for data at hardware serial
    swSer.write(Serial.read()); //send data recived from hardware serial to software serial
  }
}

```

connect  
ESP8266 Rx GPIO14 (D5) to RS485 TXD  
ESP8266 Tx GPIO 12 (D6) to RS485 RXD

any idea what is the baudrate of the CDC machine? the above is 115200baud

you should be able to type on the serial monitor to send data over the RS485 link and see the response

---

<div class="post-metadata">

**Author:** ![sefaguntepe](https://avatars.discourse-cdn.com/v4/letter/s/aca169/32.png) [@sefaguntepe](https://forum.arduino.cc/u/sefaguntepe)\
**Post date:** [January 20, 2023, 6:29am UTC](https://forum.arduino.cc/t/uart-ttl-to-rs485-two-way-converter-module-nodemcu/1079783/7 "2023-01-20T06:29:29Z")

</div>

> [@horace](#):
>
> ```arduino
> // pins Rx GPIO14 (D5) and Tx GPIO 12 (D6)
> SoftwareSerial swSer(14, 12);  
> 
> void setup() {
> Serial.begin(115200); //Initialize hardware serial with baudrate of 115200
> swSer.begin(115200); //Initialize software serial with baudrate of 115200
> Serial.println("\nESP8266 Software serial test started");
> }
> 
> void loop() {
> while (swSer.available() > 0) { //wait for data at software serial
> Serial.write(swSer.read()); //Send data recived from software serial to hardware serial    
> }
> while (Serial.available() > 0) { //wait for data at hardware serial
> swSer.write(Serial.read()); //send data recived from hardware serial to software serial
> }
> }
> 
> ```

9600 - 8 - Even - 1

---

<div class="post-metadata">

**Author:** ![horace](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/horace/32/1014299_2.png) [@horace](https://forum.arduino.cc/u/horace)\
**Post date:** [January 20, 2023, 6:32am UTC](https://forum.arduino.cc/t/uart-ttl-to-rs485-two-way-converter-module-nodemcu/1079783/8 "2023-01-20T06:32:23Z")

</div>

> [@sefaguntepe](#):
>
> 9600 - 8 - Even - 1

set the Serial1 baudrate to 9600, e.g.

```arduino
  swSer.begin(9600, SERIAL_8E1); //Initialize software serial with baudrate of9600

```

---

<div class="post-metadata">

**Author:** ![sefaguntepe](https://avatars.discourse-cdn.com/v4/letter/s/aca169/32.png) [@sefaguntepe](https://forum.arduino.cc/u/sefaguntepe)\
**Post date:** [January 20, 2023, 6:55am UTC](https://forum.arduino.cc/t/uart-ttl-to-rs485-two-way-converter-module-nodemcu/1079783/9 "2023-01-20T06:55:17Z")

</div>

Can I see all the data coming from the machine? In my previous work, I could only see the parameters I set.

---

<div class="post-metadata">

**Author:** ![markd833](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/markd833/32/1186928_2.png) [@markd833](https://forum.arduino.cc/u/markd833)\
**Post date:** [January 20, 2023, 7:07am UTC](https://forum.arduino.cc/t/uart-ttl-to-rs485-two-way-converter-module-nodemcu/1079783/10 "2023-01-20T07:07:19Z")

</div>

You should be able to see any reply from your CNC machine.

You need to know the serial communication parameters such as baud rate in order to successfully communicate with the CNC machine.

What communications protocol is used? You need to know this in order to interrogate your CNC machine and understand the response you get from it.

Post a link to the user guide.

Also, is the image in post #5 your board? Are RXD2 & TXD2 available. If so, you could use a hardware serial port.

---

<div class="post-metadata">

**Author:** ![horace](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/horace/32/1014299_2.png) [@horace](https://forum.arduino.cc/u/horace)\
**Post date:** [January 20, 2023, 7:26am UTC](https://forum.arduino.cc/t/uart-ttl-to-rs485-two-way-converter-module-nodemcu/1079783/11 "2023-01-20T07:26:16Z")

</div>

> [@sefaguntepe](#):
>
> Can I see all the data coming from the machine? In my previous work, I could only see the parameters I set.

if you know the protocol you send a command and should be able to see the response

Edit: checking the ESP8266softwareserial documentation the baud rate setting should be

```arduino
swSer.begin(9600, SWSERIAL_8E1); //Initialize software serial with baudrate of9600

```

---

<div class="post-metadata">

**Author:** ![sefaguntepe](https://avatars.discourse-cdn.com/v4/letter/s/aca169/32.png) [@sefaguntepe](https://forum.arduino.cc/u/sefaguntepe)\
**Post date:** [January 20, 2023, 8:04am UTC](https://forum.arduino.cc/t/uart-ttl-to-rs485-two-way-converter-module-nodemcu/1079783/12 "2023-01-20T08:04:58Z")

</div>

> [@markd833](#):
>
> Also, is the image in post #5 your board? Are RXD2 & TXD2 available. If so, you could use a hardware serial port.

yes my device

---

<div class="post-metadata">

**Author:** ![markd833](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/markd833/32/1186928_2.png) [@markd833](https://forum.arduino.cc/u/markd833)\
**Post date:** [January 20, 2023, 8:21am UTC](https://forum.arduino.cc/t/uart-ttl-to-rs485-two-way-converter-module-nodemcu/1079783/13 "2023-01-20T08:21:55Z")

</div>

The use of an RS485 interface may suggest a protocol such as Modbus. You need to provide a link to the user guide or photos of the relevant pages from a paper manual if that is the case.

---

<div class="post-metadata">

**Author:** ![sefaguntepe](https://avatars.discourse-cdn.com/v4/letter/s/aca169/32.png) [@sefaguntepe](https://forum.arduino.cc/u/sefaguntepe)\
**Post date:** [January 20, 2023, 8:47am UTC](https://forum.arduino.cc/t/uart-ttl-to-rs485-two-way-converter-module-nodemcu/1079783/14 "2023-01-20T08:47:26Z")

</div>

These codes are for the converter in the second (4 pin) picture I took. I actually want to convert this code to the converter (RX-TX) one in the first image I shot.  
The code I used before:

```cpp
#include <ESP8266WiFi.h>
#include <SPI.h>
#include <ModbusRTU.h>
#include <SoftwareSerial.h>

char str0[6];

SoftwareSerial S(0, 4);
ModbusRTU mb;

bool cb(Modbus::ResultCode event, uint16_t transactionId, void* data) { // Callback to monitor errors
  if (event != Modbus::EX_SUCCESS) {
    Serial.print("Request result: 0x");
    Serial.print(event, HEX);
  }
  return true;
}

void setup() {
Serial.begin(9600);
  S.begin(9600, SWSERIAL_8E1);
  mb.begin(&S, 16); // RE/DE connected to D0 of ESP8266
  mb.master();
}
uint16_t val0[1];

void loop() {
       if (!mb.slave()) {
    mb.readHreg(2, 8501, val0, 1, cb); // Slave id is 1 and register address is 3926 and 
    //we are reading 2 bytes from the register and saving in val
    while(mb.slave()) { // Check if transaction is active
      mb.task();
      delay(100);
    }
      Serial.println("Register Values ");
      Serial.println(val0[0]);
      dtostrf(val0[0], 0, 0, str0);
        yield();
     }

            if (!mb.slave()) {
    mb.readHreg(2, 3240, val0, 1, cb); // Slave id is 1 and register address is 3926 and 
    //we are reading 2 bytes from the register and saving in val
    while(mb.slave()) { // Check if transaction is active
      mb.task();
      delay(100);
    }
      Serial.println("Register Values ");
      Serial.println(val0[0]);
      dtostrf(val0[0], 0, 0, str0);
        yield();
     }
}

```

---

<div class="post-metadata">

**Author:** ![markd833](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/markd833/32/1186928_2.png) [@markd833](https://forum.arduino.cc/u/markd833)\
**Post date:** [January 20, 2023, 9:42am UTC](https://forum.arduino.cc/t/uart-ttl-to-rs485-two-way-converter-module-nodemcu/1079783/15 "2023-01-20T09:42:54Z")

</div>

Assuming that the code works for the 4 pin RS485 board, then going from a board with RE & DE signals to an auto switching board should not need any code modifications.

---

<div class="post-metadata">

**Author:** ![noiasca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/noiasca/32/1126910_2.png) [@noiasca](https://forum.arduino.cc/u/noiasca)\
**Post date:** [January 21, 2023, 8:31am UTC](https://forum.arduino.cc/t/uart-ttl-to-rs485-two-way-converter-module-nodemcu/1079783/16 "2023-01-21T08:31:15Z")

</div>

> [@sefaguntepe](#):
>
> Can I see all the data coming from the machine? In my previous work, I could only see the parameters I set.

I told you already in

> [@Receiving all data with modbus using Arduino + RS485](https://forum.arduino.cc/t/receiving-all-data-with-modbus-using-arduino-rs485/1079238):
>
> Hello, I am getting data from CNC machines using MAX485 with Nodemcu. However, I only get value over the parameter I specified and Slave Id. How can I get all the data at once? without entering any parameters. Then I will transfer and filter with MQTT. if (!mb.slave()) { mb.readHreg(2, 2450, val0, 1, cb); // Slave id is 1 and register address is 3926 and //we are reading 2 bytes from the register and saving in val while (mb.slave()) { // Check if transaction is active …

that with modbus you have to request a register and the amount of values you want to read starting from that register.

To read more information you send another request with the next register and the amount of values.

You have to repeat this for all information you want.

Furthermore I told you already to post the datasheet of your device describing the protocol you want to read.  
If you would do so - someone could help you.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 20, 2023, 8:31am UTC](https://forum.arduino.cc/t/uart-ttl-to-rs485-two-way-converter-module-nodemcu/1079783/17 "2023-07-20T08:31:53Z")

</div>

Closed on July 20, 2023, 8:31am UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
