Notifications
Clear all

ESP 32 serial1 or serial2 map to different pins than default

5 Posts
2 Users
0 Likes
18.4 K Views
(@avrphreak)
Member
Joined: 3 years ago
Posts: 7
Topic starter  

Hello,

maybe someone has encountered this before, i did search online but did not find any similar cases.

For my weather station with a Nextion display I wanted the display to be connected to a different serial port than the default port. In doing so I would not have to disconnect the RX and TX pins while uploading a new sketch. With the display connected the upload would fail. The ESP32 has 3 uarts so that should not be a problem, the only thing is that on my particular ESP's only one uart is broken out to the devboard pins.

So I tried to remap using Serial1.begin(9600, SERIAL_8N1, 26, 27); in the setup.
This did not work.
After some fussing about I discovered by chance that with the TX on pin 27 and the RX on the default ESP RX pin, it did work...
Further fussing about resulted in the conclusion that the RX connection can be connected to pin0, default RX and strangely enough also the default TX pin.

I tried the same with serial2, same result. I also tried different pins since I know that some are not really available but no change. 

All I am doing is sending values to the display, not vice-versa. Also I am doing this by straight serial comms, no library is used. I did try a barebones sketch with no other functionality in it with the same result. Including the Hardwareserial.h library did not change things.
I could edit the Hardwareserial.cpp to change the pins there or opt for Softwareserial but that should not really be necessary.

So for now things will work for me as is, but i found it strange that I was not able to remap the pins for a 100 percent. 

I am using PlatformIO, ESP32 WROVER (I tried WEMOS LOLIN and LILYGO versions), Arduino core (if that's how you call that).

Has anyone had a similar experience and if so, were you able to correct this?

Curious to hear your experiences!

Best regards, Willem.


   
Quote
MadMisha
(@madmisha)
Member
Joined: 4 years ago
Posts: 340
 

I posted this on your other post but maybe I should put it here in case someone finds it here.

 

#include <SoftwareSerial.h>

#define RXD2 9
#define TXD2 10


void setup() {

Serial1.begin(9600, SERIAL_8N1, RXD2, TXD2);

}


void loop() {

Serial2.print("Hello World");

}

 

I have mine in TX2 and RX2. I failed with hardware serial. I remember looking for the actual pin numbers to use for a really long time. Then I realized I needed to use the GPIO numbers(I was new).

ESP32Wrover

It looks like 9 and 10 will work if you have the one I posted here. It would be hooked up to pins 17 and 18. As much as I do really love the ESP32, I hate the fact that you never really know if you have the right pinout for your model.

 

Edit: It looks like the file I pulled that example from was when I was messing around to figure out what works. SoftwareSerial is noted by arduino as using this

 

const byte rxPin = 9;
const byte txPin = 10;

// set up a new serial object
SoftwareSerial mySerial (rxPin, txPin);

 

Then you can call it by mySerial, or whatever you name it. I remember looking up these libraries and seeing that some of them had been discontinued. My first example did work so this is just another option. I think you have an issue with the pins you are using/calling.


   
ReplyQuote
(@avrphreak)
Member
Joined: 3 years ago
Posts: 7
Topic starter  

Thank you again.

I did confirm that i used the correct gpio pins according to the pinout diagrams.

Really weird, this is.

Will have a look at software serial in the near future! 


   
ReplyQuote
MadMisha
(@madmisha)
Member
Joined: 4 years ago
Posts: 340
 

@avrphreak

Well, if you are using any ESP32 Wrover kit, you won't be getting serial on gpio 26 and 27. Here is the datasheet for that chip. https://www.espressif.com/sites/default/files/documentation/esp32-wrover_datasheet_en.pdf

 

U1RXD is on 9 and U1TXD is on 10. You would even be able to use your code exactly the way it was other than changing the pins.


   
ReplyQuote
(@avrphreak)
Member
Joined: 3 years ago
Posts: 7
Topic starter  

@madmisha, thanks!

No indeed, pins 26 and 27 are not serial by default, which is why i wanted to map the serial port to these pins. This *should* be possible by means of the method described in my initial post.

The pin labels on my dev boards match with the esp's gpio labels, according to the pin diagrams i have. I am not using a bare esp chip, but rather the solutions provided by Lilygo and Wemos integrated on a pcb board together with batt connection, sd slot, usb-to-serial etc.

The default pins for serial1 supposedly crash the esp due to them being used also for SRAM, the default pins for serial2 are not broken out from the esp chip on my dev boards i.e. they are not available for use.

Neither are the serial1 pins by the way, so if i want to use either serial i need to remap.

In any case thank you for your effort!


   
ReplyQuote