ESP32 Hardware Serial

38 views
Skip to first unread message

Puddleraker

unread,
Dec 30, 2024, 10:44:01 AM12/30/24
to Norwich Hackspace
I would like some assistance in modifying some basic code for an MP3 Dfplayer to utilise the hardware serial pins (16/17) on an ESP32. Any assistance would be very appreciated.
Many thanks. Neil

#include <DFPlayerMini_Fast.h>


#if !defined(UBRR1H)
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
#endif

DFPlayerMini_Fast myMP3;

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

#if !defined(UBRR1H)
mySerial.begin(9600);
myMP3.begin(mySerial, true);
#else
Serial1.begin(9600);
myMP3.begin(Serial1, true);
#endif
Serial.println("Setting volume to max");
myMP3.volume(30);
Serial.println("Looping track 1");
myMP3.loop(1);
}

void loop()
{

Alan Childs

unread,
Dec 30, 2024, 11:00:44 AM12/30/24
to norwich-...@googlegroups.com
What ESP32 are you using?

You probably want to change 
#include <SoftwareSerial.h> SoftwareSerial mySerial(10, 11); to
#include <HardwareSerial.h> HardwareSerial mySerial(16, 17); // Or maybe 17, 16

HardwareSerial is a core header so you shouldn't need to install any new libraries for it.
https://github.com/arduino/ArduinoCore-avr/blob/master/cores/arduino/HardwareSerial.h

--
You received this message because you are subscribed to the Google Groups "Norwich Hackspace" group.
To unsubscribe from this group and stop receiving emails from it, send an email to norwich-hacksp...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/norwich-hackspace/50a9097d-0124-40f0-a3ca-1117393b44b4n%40googlegroups.com.

Puddleraker

unread,
Dec 30, 2024, 11:05:25 AM12/30/24
to norwich-...@googlegroups.com
Hi Alan, many thanks for your quick reply. Im using an ESP32 WROOM.
Would your suggested chnchangeage be the only line I have to amend?
Thanks. Neil


You received this message because you are subscribed to a topic in the Google Groups "Norwich Hackspace" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/norwich-hackspace/eB7vZ6T0gvo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to norwich-hacksp...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/norwich-hackspace/CAHO%2BQONfn%3DOmU2Ayh%3DYseuvAsGx%2BmQ93And_bjwiECO5p1O8YQ%40mail.gmail.com.

Alan Childs

unread,
Dec 30, 2024, 11:14:00 AM12/30/24
to Norwich Hackspace
I'm not sure if

#if !defined(UBRR1H)
is defined or not. The hash # before the if indicates that the code between that line and the #endif will only be copied to the ESP32 if UBRR1H is not configured in the code somewhere. The ESP32 itself won't use that IF statement.

In your code you appear to have different references to serial - mySerial ... Serial ... Serial1 .... You can probably tidy the code to only reference one name, unless you are really using multiple serials.

It's possible that your program is using more than one serial interface, but only one can be hardware, so that is why there is a software serial. At a guess the program is using hardware serial to drive an MP3 module and software serial to output debug code.
Debug code doesn't need to be fast or partially efficient, but the MP3 module does.

Puddleraker

unread,
Dec 30, 2024, 11:18:43 AM12/30/24
to norwich-...@googlegroups.com
Thanks. I will give it a go later and see what happens. 
The code is the example code from Github.



Alan Childs

unread,
Dec 30, 2024, 11:23:09 AM12/30/24
to Norwich Hackspace
#if !defined(UBRR1H)
That's probably a check to see if the serial is already setup as hardware for this build, and if not then set it up as software. So, if you have a ESP32 that supports more that one Hardware UART, then it has already configured  mySerial as hardware somewhere else?

Alan Childs

unread,
Dec 30, 2024, 11:29:21 AM12/30/24
to Norwich Hackspace
Actually yes, if defined(UBRR1H) which on a ESP32-WROOM it is then you have Serial1 as a default hardware interface, so the compiler uses myMP3.begin(Serial1, true); 

So you already are using hardware serial.

Puddleraker

unread,
Dec 30, 2024, 11:30:59 AM12/30/24
to norwich-...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages