Trouble getting UART2 to work with ESP32

Hello, I'm working on a weather monitor project. Currently, I'm using an ESP32 board that I purchased from amazon.com. keeyees ESP32S (ESP-WROOM-32) I've been trying to configure an air particulate sensor monitor to it using UART. The air monitor that I'm using is the Nova fitness SDS011. This air monitor transmits measurements in bytes using UART.

The problem:

The problem that I'm having is I'm trying to configure the ESP32 in such as way that it takes readings from the SDS011 using UART2 and transmits via USB to my computer to the serial monitor. So far when I run the code and look at the serial monitor, it's not printing the information (in hex format) to the serial monitor. I've tried using the HardwareSerial library, but still get the same result. I know that the SDS011 is working because I can use a logic analyzer and see it automatically transmitting data. Is there anything I'm missing in my code or something about the board? I'm kind of newbie, and I'm not sure where I'm going wrong.

Thanks

Edit -- Some Additional Information:

  • The code that I posted has GPIO 16 and 17 listed. I've also tried using PINs such as 27 and 28 for UART 2.

  • Something else that cam to mind. What board selection in the Arduino IDE should I be using for board model. The pdf that came with the board said to use ESP32 Dev Module. I've tried this and still haven't been able to get it to work.

Screenshot of Board Pinout

#include <HardwareSerial.h>

HardwareSerial AirSerial(2);

void setup() {
  delay(100);
  Serial.begin(115200); // Start serial communication at 115200 bps 
  Serial.write("Reading data from SDS011.");

  //Set up the Serial1 peripheral for receiving data from the sensor.
  //Since the ESP32 has a pin multiplexer, the tx and rx pins can be set to any GPIO pin.
  AirSerial.begin(9600, SERIAL_8N1,27, 28);
}


void loop() {
  static boolean receivingPacket = false;
  static int packetIndex = 0;
  const int packetSize = 10; // SDS011 packet size

  if (AirSerial.available() > 0) {	//If there is a packet in the rx buffer...
    int c = AirSerial.read();	//Read it and store it in c.

    // Check for the start of a new packet. They always start with 0xAA
    if (c == 0xAA) {
      receivingPacket = true;	//Set this to indicate a new packet has started.
      packetIndex = 0;
      Serial.println(); // Start a new line for a new packet
    }

    if (receivingPacket) {	//Keep looping while there are more bytes in the packet.

      // Print the byte as a hexadecimal value
      if (c < 16) Serial.print('0'); // Add a leading zero for values less than 0x10
      Serial.print(c, HEX);
      Serial.print(' '); // Space for readability
      
      packetIndex++;
      if (packetIndex >= packetSize) {
        // End of packet
        receivingPacket = false;
        Serial.println(); // Start a new line after the packet
      }
    }
  }
}

For the record - you are using GPIO16 and 17 and not 'physical pins' 16 and 17 ?

good point, I think putting 16, 17 was where I left off. I"ve tried pins 27, 28 and still get the same behavior.

Physical pins 27 and 28 are GPIO 16 and 17 (respectively).

This is what I'm doing to read a GPS unit on Serial2 of a Cheap Yellow Display

const int RX_PIN = 17;
const int TX_PIN = 18;

void setup() 
{
    Serial.begin(115200);
    setupUI();

    Serial2.begin(9600, SERIAL_8N1, RX_PIN, TX_PIN);
}

The cheap yellow display is new to me, but doing some Google searching I see it's an ESP control screen. The ESP that's used for the display, what are the pinouts for that model? Is pin 17 transmitting and pin 18 is serving as the receiver?

Other way around: 17 is receive and 18 is transmit.

I also use this config on another project that uses an ESP32 with external TFT screen

const int RX2_PIN = 26;
const int TX2_PIN = 25;

I don't know what the pin multiplex limitations are, but I do know that both of these configurations worked for me.

Test your UART2 (RX2, TX2 = 16, 17, Picture of post #1)) by a simple loop-back connection. TX2-pin (Pin-28 of ESP32 Board) is shorted to RX1-pin (Pin-27 of ESP32 Dev Board of post #1). Disconnect Sensor/Device from UART2 (RX2, TX2 = 16, 17) Port.
Skecch:

void setup()
{
  Serial.begin(9600);
  Serial2.begin(9600);
}

void loop()
{
  byte n = Serial.available();
  if ( n != 0)
  {
    char y1 = Serial.read();
    Serial2.print(y1);
    //---------------------------
    byte m  = Serial2.available();
    if ( m != 0)
    {
      char y2 = Serial2.read();
      Serial.print(y2);
    }
  }
}

Enter Forum in the InputBox of Serial Monitor (select Newline option in the Line ending Box of Serial Monitor, Fig-1) and then click on the Send Buton. Chack that Forum has appeared on the OutputBox of Serial Monitor.

Output:

Forum


Figure-1:

are you sure you have a 38pin ESP32?

for testing ESP32 Serial2 I use

// ESP32 serial2 hardware loop back test - jumper GPIO16 (Rx) and GPIO17 (Tx)

// see https://circuits4you.com/2018/12/31/esp32-hardware-serial2-example/
/* There are three serial ports on the ESP known as U0UXD, U1UXD and U2UXD.
 * 
 * U0UXD is used to communicate with the ESP32 for programming and during reset/boot.
 * U1UXD is unused and can be used for your projects. Some boards use this port for SPI Flash access though
 * U2UXD is unused and can be used for your projects.
*/

#define RXD2 16
#define TXD2 17

void setup() {
  // Note the format for setting a serial port is as follows: Serial2.begin(baud-rate, protocol, RX pin, TX pin);
  Serial.begin(115200);
  Serial2.begin(115200, SERIAL_8N1, RXD2, TXD2);
  Serial.println("ESP32 hardware serial test on Serial2");
  Serial.println("Serial2 Txd is on pin: "+String(TXD2));
  Serial.println("Serial2 Rxd is on pin: "+String(RXD2));
}

void loop() { //Choose Serial1 or Serial2 as required
  while (Serial2.available()) {
    Serial.print(char(Serial2.read()));
  }
  while (Serial.available()) {
    Serial2.print(char(Serial.read()));
  }
}

to test link GPIO16 and GPIO17 to form a loopback
text entered on serial monitor should echo back to display

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