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);
}
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.