Serial port 1 on Nano ESP32 using HardwareSerial

I'm using HardwareSerial on the Nano ESP32 and it's working, but pin designations don't seem to be recognized. I originally had:

HardwareSerial dfSD(1);
    .
    .
    .
dfSD.begin(28800, SERIAL_8N1, D9, D10);  
 

And it worked just fine. But I wanted to change to use D8 for Xmit:

dfSD.begin(28800, SERIAL_8N1, D9, D8);  

When I did, it still transmitted on D10.

Any explanation why?

Hello @IraSch, I am not able to replicate your issue with Arduino ESP32 Boards version 2.0.18-20240930-arduino3. What platform and core version are you using?

Can you confirm you succesfully updated the revised sketch on the Nano ESP32? You can add/remove a LED just to double check :slight_smile:

I'm using IDE 2.3.3, Board Manager version 2.0.18-20240930.arduino3

I'm sure it's updating. Besides seeing it flash to board I also changed the onboard LED color from blue to red just in case. I do have "By GPIO Number" set if that makes any difference.

I tried to see what version of HardwareSerial I'm using but it's not a library so I'm guessing it goes with the Board version.

Already checked it was working with both modes. It definitely shouldn't matter (you are using pin labels :heart_eyes:), but thanks for mentioning.

Yup, indeed!


Thanks for double checking... it's really strange. Can you try to simplify your sketch to the smallest possible example that shows this issue and post it? TIA!

The sketch is below. I'm using this to test the RS485 transceiver capability (speed, cable length). A packet sent from the master and the client (this sketch) sends a small response. The RS485 transceiver is connected to D9, D10 & D5. You can pretty much ignore the RS485 aspect of things since it just takes the signals from these 3 pins - what actually gets transmitted is irrelevant to the D10/D8 pin question.

// Board: Arduino Nano ESP32
// Processor: ESP32
// Programmer: ESPTOOL

#include "RS485_protocol.h"

const byte LED_PIN = LED_BLUE;
#define RS485_ENABLE_PIN D5  //    // GPIO8
#define RS485_RECV_PIN   D9  // D0 // GPIO44
#define RS485_XMIT_PIN   D10 // D1 // GPIO43
int RS485BufferSize;

HardwareSerial dfSD(1);

struct CmdStruct {
  byte client;
  byte msgno;
  byte stat;
  byte command;
  int16_t PIN[4];  // *************************************** ESP32 int is 32 bits, need to be 16. 
  byte Zone[16];
} ; 

union myunion {
  byte msgread[28];  // This is actually used when reading from the RS485 data
  CmdStruct CMD;
} u;

void fWrite (const byte what)
  {
  dfSD.write (what);  
  }
  
int fAvailable ()
  {
  return dfSD.available();
  }

int fRead ()
  {
  return dfSD.read();
  }
  
void setup()
{
  Serial.begin(115200);
  pinMode (LED_BLUE, OUTPUT);          // LED on Nano
  pinMode (LED_RED, OUTPUT);          // LED on Nano

  dfSD.begin(28800, SERIAL_8N1, RS485_RECV_PIN, RS485_XMIT_PIN);  // For some reason, Xmit can be D8 or D10 and still goes to D10. 
  pinMode (RS485_ENABLE_PIN, OUTPUT);    // driver output enable
  digitalWrite(RS485_ENABLE_PIN, LOW);
  RS485BufferSize = dfSD.availableForWrite();
  delay(2000);

  Serial.println("Client Starting.");
}

void loop()
{
  digitalWrite(LED_PIN, HIGH);
  byte received = recvMsg (fAvailable, fRead, u.msgread, sizeof (u.msgread));
  
  if (received)
    {
    Serial.println("Client Receive");
    Serial.print(" Client: "); Serial.print(u.CMD.client);
    Serial.print(" Msgno: "); Serial.print(u.CMD.msgno);
    Serial.print(" Stat: "); Serial.print(u.CMD.stat);
    Serial.print(" Command: "); Serial.print(u.CMD.command);
    Serial.print(" PINs: "); Serial.print(u.CMD.PIN[0]); Serial.print(", ");Serial.print(u.CMD.PIN[1]); Serial.print(", ");Serial.print(u.CMD.PIN[2]); Serial.print(", ");Serial.print(u.CMD.PIN[3]);
    Serial.println();   
    
    if (u.msgread[2] < 128) digitalWrite(LED_PIN, LOW); else digitalWrite(LED_PIN, HIGH);
    
    byte msg [] = {
       0,  // device 0 (master)
       3,  // status OK
    };
    
    delay (5);  // give the master a moment to prepare to receive
    digitalWrite (RS485_ENABLE_PIN, HIGH);  // enable sending
    sendMsg (fWrite, msg, sizeof msg);

    unsigned long start_time = millis ();
    #define MaxSendWait 100
    while ((millis () - start_time < MaxSendWait) && (dfSD.availableForWrite() != RS485BufferSize)) { // wait for buffer to empty or up to 100 msec
      delay(1);
    }
    delay(1);

    digitalWrite (RS485_ENABLE_PIN, LOW);  // disable sending
    
    
   }  // end if something received
   
}  // end of loop

Is an include for HardwareSerial.h no longer necessary ?

It is not necessary because it is #included transitively via the automatically injected #include <Arduino.h>: