Make Hardware Serial port LOW after sending data

I'm trying to send data to a sensor using the ESP32's TX (UART2) pin, and then I want to set pin 19 LOW afterwards.

The command analogWrite(19, 0) successfully sets the pin LOW, but afterwards, I'm unable to send another message. What I'm aiming for is to have pin 19 go LOW by default after sending the message, rather than remaining at 3.2V.

Any suggestions on how to achieve this would be greatly appreciated!



#include "HardwareSerial.h"
HardwareSerial SERIAL_2(2);  // UART2

void setup() {
  Serial.begin(9600); // Start Serial communication
  SERIAL_2.begin(9600, SERIAL_8N1, -1, 19); // Initialize serial communication 
}

void loop() {
 if (Serial.available() > 0) {
    // If a character is available, read it
    char receivedChar = Serial.read();
    
    if (receivedChar == '1') {
      UART2_msg();
    } 
  }
}

Void UART2_msg(){
  uint8_t data[] = { 0x7E, 0xFF, 0x06, 0xEF };
  SERIAL_2..write(data, sizeof(data));
analogWrite(19,0);

}

x

Why?

the sensor that i send message to, needs that

Post a link to the datasheet for the mystery sensor.

Force a BREAK condition.

I think you should move your SERIAL2.begin() into the UART2_msg() function, and use SERIAL2.end() instead of analog write.

Ciao, Ale.

still pin 19 is 3.2v

Are you sure that voltage coming from the board and not from the sensor? The linked reference about Serial.end() says:

Disables serial communication, allowing the RX and TX pins to be used for general input and output.

So, the pin should be putted in input mode, as default. Maybe ESP32 implementation is different.

Ciao, Ale.

i measure pin 19 and gnd with multimeter after sending end()

Have you try to disconnect the sensor before measuring?

Ciao, Ale.

after calling end(); i called analogWrite(19, 0); // and then it works

You are not using it as an analog pin, you are using it as a digital pin. So it would make more sense to use pinMode(19, OUTPUT) and digitalWrite(19, LOW).

Don't forget to call Serial2.begin() again before sending the next message to the sensor.

2 Likes

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