Nano ESP32 and Serials

Hello,

I want to connect to the Serial while an USB cable is connected. I know ESP32 has 3 serial ports. On is used by USB while working on a script and the two others are expected to be free for device connectivity.

I'm not sure what pins to use for Serial1 and Serial2. The pinout cheat shows there is two pins D0/D1 which are expected to be RX0/TX0 thus used by USB.
Reading the Starter page, I see Serial1 is D0/D1 That's a bit surprising as I though it should be Serial. Actually, a script connecting to these pins with Serial1 object does not seems to work when the board is running with no USB cable. Nor with Serial object either.

I read here and there ESP-32 Serial GPIO should be 9-10 (respectively RX/TX) or 16-17. The Nano does not present GPIO 16, so I can't try this one. And I tried using 9-10 with Serial1 with no success.

Here is the initialization code used:

#define RX_PIN 9U  // D6
#define TX_PIN 10U // D7

Serial.begin(9600); // Works with USB cable. Otherwise, fails
Serial1.begin(9600): // Fails on D0/D1 and D6/D7
Serial2.begin(9600); // Fails on D0/D1 and D6/D7
Serial.begin(9600, SERIAL_8N1, RX_PIN, TX_PIN); // Fails
Serial1.begin(9600, SERIAL_8N1, RX_PIN, TX_PIN); // Fails
Serial2.begin(9600, SERIAL_8N1, RX_PIN, TX_PIN); // Fails

I jsut can get a device to discuss with my Nano using Serial for now. :confused:

Do anyone has a working example?

Regards,

This is an implementation of Serial1 as used with a "DFPlayer"

//
const byte RXD2 = 16; //
const byte TXD2 = 17; //

HardwareSerial dfSD(1); // Use UART channel 1
DFRobotDFPlayerMini player;
   
void setup() 
{
  Serial.begin(19200);
  dfSD.begin(9600, SERIAL_8N1, RXD2, TXD2);  // 16,17
  delay(5000);

  if (player.begin(dfSD)) 
  {
    Serial.println("OK");

    // Set volume to maximum (0 to 30).
    player.volume(17); //30 is very loud
  } 
  else 
  {
    Serial.println("Connecting to DFPlayer Mini failed!");
  }
}

void loop() 
{
  Serial.println("Playing #1");
  player.play(1);
  Serial.println("play start");
  delay(5000);
  Serial.println("played");
  delay(1000);
  //  "as is", it will repeatedly, maddeningly, play 'track No.1' for 5secs.
}

Hmmm, now I recognize your handle from that topic. Didn't work then?

1 Like
#define RX_PIN 9U  // D6
#define TX_PIN 10U // D7

9U, 10U
What's with the "U"? Is that a Nano-ESP32 convention?

I believe that the rx/tx can be assigned to "any" GPIO.

const byte RXD2 = 16; //
const byte TXD2 = 17; //

byte idx;

HardwareSerial S1_test(1); // Use UART channel 1
   
void setup() 
{
  Serial.begin(19200);
  S1_test.begin(9600, SERIAL_8N1, RXD2, TXD2);  // 16,17
  delay(2000);
}

void loop() 
{
  for(idx = 0; idx < 10; idx ++)
  {
    S1_test.print("Hello ");
    S1_test.println(idx);
    delay(1000);
  }
}

Hello @anoril!

[...] two pins D0/D1 which are expected to be RX0/TX0 thus used by USB.

Actually, the "Serial over USB" device is internal to the CPU, so those pins can be connected to an actual physical UART if you desire. But on the ESP32 you can choose any digital pin to be used as RX or TX for the hardware UARTs.

#define RX_PIN 9U  // D6
#define TX_PIN 10U // D7

That is probably the issue. As other Arduino platforms, on the Nano ESP32 pins are referenced by their "digital number" (not by GPIO index, as is common with other ESP32 implementations). Can you try simply with 6 for D6 and 7 for D7?

Thanks, best regards,
Luca

1 Like

Oh,. . . not again.

Hi!

Yes, it's me again :wink:
I though everything worked fine, and it did... But I'm working at night and I was working on two boards: an Arduino Nano ESP32 and a Adafruit HUZZAH32 Feather. And I eventually mixed the two.

The Long Version:
My code and the given solution for the voltage divider worked fine with the Feather and at the time, I thanked you (and J.M.L) as It solved all my problems... But in the morning, I realized it was on the wrong board. No issue here: I just plug one out and placed the other one, check pins and cables (only GND to change and RX/TX) and... Everything went OK BUT the DFPlayer stuff.

I tried again several Serial setings with no luck. Setting back the pins for the Feather and the music plays.

<TL;DR>
9U and 10U are convientions I use when defining stuff to be sure the compiler will get I'm working on unsigned int for later convertion to uint_8. Not a point, I suppose as I had no issue with that for the time I code on Arduino :slight_smile: (but, I tried without U and declaring const byte as you suggest, for the Honor and to be sure. I'm never sure of anything for the years I code...)

The code you gave uses ports 16 and 17 but I can't find them on the Nano ESP32.

And yes, I read here and there that any GPIO can be used. And I tried several for sure, but no success. I go back to the root from a post on StackOverflow (arduino - ESP32 - SoftwareSerial Library - Stack Overflow) that seems to know what it talks about. But hey: no success anyway.

:slight_smile:

At last, My all setup (IR detection, a reed switch, a keypad, a buzzer... A digicode :wink: ) works fine with the same code for Huzzah and NanoESP32... But the Huzzah is able to play music where the NanoESP32 does not due to that Serial issue I can't spot.

Hello,

Thanks for the point. But that is not the case. There is many names for the pins on the board; their little name (Dx/Ax) that can be declared in a .h file regarding your IDE settings and the actual name for the MCU (GPIOx).
For instance, if you use a "generic ESP32", you can see that: (pins_arduino.h)

static const uint8_t A0 = 18;
static const uint8_t A1 = 17;
static const uint8_t A2 = 16;
static const uint8_t A3 = 15;
static const uint8_t A4 = 14;

But when you look at a specific board, GPIO may be assigned to another pin number/id as the NanoESP32 pinout shows:


Here, you have the names and the actual GPIO number. And because the names are dictate by a piece of code, it may be wrong or inconsistent with you actual board and IDE.

This is why I prefer to use GPIO numbers I'm sure of. :slight_smile:
And I also sure I'm not wrong in that case: I have four other devices attached to my board and they all respond correctly to the port/pin I assigned them to.

Note I tried the 6 and 7 as you propsed. With no success any :confused:

Except for that ~#{[#{[@ Serial :smiley:

OK, I guess.
I'm still waiting for my Nano32's.
They should be here in a few days.

1 Like

I leave for few days. The world will change by when I'm back online.

I work with PlatformIO which has a very light support for the NanoESP32 and I'm struggling with it (I suppose ArduinoIDE works fine, but I'm not happy with that IDE). For instance, I have to use an other ESP profile for having the board uploading a script, It changes dynamically its com port, I have to manually reset the board for it to run the new script and manually restart the serial monitor, missing all the setup debug stuff...
Eventually, I use a ledstrip for debugging that part :wink:

In few weeks I suppose the PlatformIO support for the board will me better. And maybe some new tutorials will be available regarding the serials :slight_smile:

https://docs.arduino.cc/tutorials/nano-esp32/cheat-sheet#uart

They're showing Serial1 in an example where it's all set up to use 0 and 1. Importantly, Serial is still SerialMonitor (like the Leonardo and so on).
Even though I have my Nano32's, I haven't checked to see if they can be configured with the rx/tx assigned to other 'pins'.

Thanks for the check.
That's the first attempt I made, using Serial1 on D0/D1. But no success.
That's why I read more and found the expected other pins. :confused:

I have my DFPlayer running with the NanoESP32, via Serial1 assigned to pins of my own choice (6 & 7), using this sketch --

#include "HardwareSerial.h"
#include "DFRobotDFPlayerMini.h"

//
const byte RXD2 = 6;
const byte TXD2 = 7; 

HardwareSerial dfSD(1); // Use UART channel 1
DFRobotDFPlayerMini player;
   
void setup() 
{
  Serial.begin(19200);
  dfSD.begin(9600, SERIAL_8N1, RXD2, TXD2);  // D6,D7
  delay(5000);

  if (player.begin(dfSD)) 
  {
    Serial.println("OK");
    // Set volume to maximum (0 to 30).
    player.volume(22); //30 is very loud
  } 
  else 
  {
    Serial.println("Connecting to DFPlayer Mini failed!");
  }
  Serial.println("player go");
}

void loop() 
{
  Serial.println("Playing #1");
  player.play(1);
  Serial.println("play start");
  delay(10000);
  Serial.println("played");
  delay(1000);

  Serial.println("Playing #2");
  player.play(2);
  Serial.println("play start");
  delay(10000);
  Serial.println("played");
  delay(1000);
}

If you need a vimeo or pics, let me know.

Hi!
I'm vert happy that works. Well. I'm scratching my head. I known this sketch and I tried it.

I'm away from home, thinking of my life and that circuit... :wink: I'll try again when back.

Ok. The DF is powered by the Nano board, Hence you don't need voltage divider. Good. On my side, I notice I've got poor sound when not on 5V. Not an issue.

I'm now considering changing my Dupont cables and breadboard.... Because nothing looks different between your schème and mine. #Arg

Thanks anyway for the confirmation it would work!
More news in few weeks.
Bests regards

1 Like

Hi!
I'm back home and starting designing stuff.

I changed the place of the Nano on the breadboard, changed my Dupont... Connected the HardwareSerial to D6/D7 and initialized as before and...

It works !! <<

So, The issue was between the chair and the Nano :smiley:

Many thanks all for your invaluable help!