Half-duplex UART over a single wire

Hello,
I need to implement half-duplex UART over a single wire and configure alternatively the same pin for both UART TX and UART RX.
The idea it to remap the pin (attach/detach) using the GPIO matrix.

Can somebody help me to correct switchToTX and switchToRX below that are supposed (but still not working...) to do RX/TX switching of the single wire.

For the tests, I use two ESP32 boards connected together throught the GPIO17.

Thank you,
Michel

#define SingleWire_pin 17
HardwareSerial MySerial(1);

void setup() {
  //begins with the UART1 standard pins: 17 and 16. The pin 16 is definitively realeased bc no more needed.
  MySerial.begin(115200, SERIAL_8N1, SingleWire_pin, 16);
  pinMatrixOutDetach(16, false, false); 
  ...
}

void switchToTX(){
 pinMatrixOutDetach(SingleWire_pin, false, false);
 pinMode(SingleWire_pin, OUTPUT);
 pinMatrixOutAttach(SingleWire_pin, U1TXD_OUT_IDX, false, false);
}

void switchToRX(){
 pinMatrixOutDetach(SingleWire_pin, false, false);
 pinMode(SingleWire_pin, INPUT);
 pinMatrixInAttach(SingleWire_pin, U1RXD_IN_IDX, false);
}


void loop() {
//before writing over MySerial: switchToTX
switchToTX();
...

//before reading over MySerial: switchToRX
switchToRX();
...

}

Did you forget the ground wire?

2 Likes

Thank you Paul for your response
No, I plugged the GND of the two boards together

The standard solution is a RS-485 driver module, but it requires 2 GPIO pins.

Does the ESP32 also have open collector outputs?

thank you DrDiettrich for your response.
I only have 1 remaining GPIO and can't use RS-485 for some reasons.
Yes, ESP32 also have open collector outputs. What's the relationship with my request?

You can connect o.c. outputs of both controllers and switch the usage of the pin between RX and TX without switching between input and output mode. At least this will reduce problems if both controllers try to send at the same time.

But if you want to use the hardware UART this may not help, I'm not familiar with the ESP32.

thank you for the response.
I will have a try, but it does not save pins. I still have to use both pins on each MCU, right?

GitHub - nickstedman/SoftwareSerialWithHalfDuplex: Software Serial With Half Duplex Library for Arduino

Thank you, but this is not Single wire circuit. It uses the two wires (RT and TX) of the UART. See SoftwareSerialExample.ino, it written:
The circuit:

  • RX is digital pin 2 (connect to TX of other device)
  • TX is digital pin 3 (connect to RX of other device)

Shouldn't there be a pullup resistor somewhere (internal or external, device1 or device2)? If both devices are in RX mode, it's probably expected that the signal would be pulled high.

Could you point out where in the ESP32's docs you found that the ESP32 has open collector outputs, API Reference - ESP32 - — ESP-IDF Programming Guide latest documentation (espressif.com)?

Search "open-drain" at:
https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/gpio.html?highlight=gpio%20pull%20up

dlloyd: I don't see any pullup

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