ESP32 S2 serial2 not working

PepeTheGreat
Posts: 20
Joined: Mon Sep 18, 2023 3:54 am

ESP32 S2 serial2 not working

Postby PepeTheGreat » Mon May 06, 2024 5:35 am

The S2 has usb plus 2 hardware uart.
Serial usb
Serial1 = hardware first
Serial2 = hardware second

Arduino IDE:
  1. void setup()
  2. {
  3.   Serial1.begin(9600,SERIAL_8N1,13,12);
  4.   Serial2.begin(4800,SERIAL_8N1,10,11);
  5. }
  6.  
  7.  
  8. void loop()
  9. {
  10.   Serial1.println("hello 1");
  11.   Serial2.println("hello 2");
  12.   delay(100);  
  13. }
gives compile error "'Serial2' was not declared in this scope"
Last edited by PepeTheGreat on Mon May 06, 2024 5:46 am, edited 1 time in total.

PepeTheGreat
Posts: 20
Joined: Mon Sep 18, 2023 3:54 am

Re: ESP32 S2 serial2 not working

Postby PepeTheGreat » Mon May 06, 2024 5:42 am

This compiles, has outpunt on pin 12 but no output pin 11

Code: Select all

#include <hardwareserial.h>
HardwareSerial Seriala(1);
HardwareSerial Serialb(2);

void setup() 
{
  Seriala.begin(9600,SERIAL_8N1,13,12);
  Serialb.begin(4800,SERIAL_8N1,10,11);
}


void loop() 
{
  Seriala.println("hello 1");
  Serialb.println("hello 2");
  delay(100);   
}
Output tested on 2-chan oscilloscope.

lbernstone
Posts: 831
Joined: Mon Jul 22, 2019 3:20 pm

Re: ESP32 S2 serial2 not working

Postby lbernstone » Mon May 06, 2024 6:34 am

They start counting at zero. Serial == Serial0 (aka the default console). Serial1 == HardwareSerial(1)

PepeTheGreat
Posts: 20
Joined: Mon Sep 18, 2023 3:54 am

Re: ESP32 S2 serial2 not working

Postby PepeTheGreat » Mon May 06, 2024 7:02 am

Serial == Serial0 (aka the default console). <-- not hardwareserial, but USB
Serial1 == HardwareSerial(1)
Serial2 == HardwareSerial(2) <-- this one not working in arduino ide

I interpret "2 uart" as "2 hardwareserial".
See "uart" here:
https://docs.espressif.com/projects/esp ... rison.html
or doc here – 2 × UART
https://www.espressif.com/sites/default ... eet_en.pdf

Does the S2 have only one hardwareserial?

lbernstone
Posts: 831
Joined: Mon Jul 22, 2019 3:20 pm

Re: ESP32 S2 serial2 not working

Postby lbernstone » Mon May 06, 2024 4:47 pm

There are two UART devices- 0 & 1. UART0 (Serial0) is still attached to console at boot, even if you are using HWCDC. This means that you would need to burn the UART_PRINT_CONTROL eFuse if the boot messages will interrupt your device on that port.

PepeTheGreat
Posts: 20
Joined: Mon Sep 18, 2023 3:54 am

Re: ESP32 S2 serial2 not working

Postby PepeTheGreat » Mon May 06, 2024 9:42 pm

Me thinks console, serial0, is not uart but usb.

I did read with esp-idf both serial do work. Seems an arduino-ide problem.

lbernstone
Posts: 831
Joined: Mon Jul 22, 2019 3:20 pm

Re: ESP32 S2 serial2 not working

Postby lbernstone » Tue May 07, 2024 12:07 am

You are, quite simply, incorrect.
If you have an S3 devkit with both UART and USB, you can test this easily. Set CDC On Boot to enabled, and run the following (requires v3.0.0). You will get different prints on the different ports.

Code: Select all

void setup() {
  Serial0.begin(115200);
  Serial.begin(9600);
  Serial1.begin(115200);
}

void loop() {
  if (HWCDCSerial) Serial.println("Testing USB");
  Serial0.println("Testing UART0");
  Serial1.println("Testing UART1");
  delay(1000);
}

PepeTheGreat
Posts: 20
Joined: Mon Sep 18, 2023 3:54 am

Re: ESP32 S2 serial2 not working

Postby PepeTheGreat » Tue May 07, 2024 9:36 am

How do you explain this works?

Code: Select all

#include "driver/uart.h"

// ESP32S2 usb cdc on boot on, usb dfu on boot off, upload mode internal usb.

const int VEBUS_RXD1=13, VEBUS_TXD1=12;  
const int VEBUS_RXD2=10, VEBUS_TXD2=11;  

const int UART_USED1 = 0; //UART_NUM_1; // serial1
const int UART_USED2 = 1; // serial2

const int LED = 15;

esp_err_t err11,err12,err13,err21,err22,err23;

void setup() 
{
  pinMode(LED, OUTPUT);
  digitalWrite(LED, LOW);
  Serial.begin(115200);
  delay(5000);
  Serial.println("Start");

  uart_config_t uart_config1 = 
  {
        .baud_rate = 9600,
        .data_bits = UART_DATA_8_BITS,
        .parity = UART_PARITY_EVEN,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, 
        .rx_flow_ctrl_thresh = 32,
  };
  err11 = uart_driver_install(UART_USED1, 256, 256, 0, NULL, 0);
  err12 = uart_param_config(UART_USED1, &uart_config1);   
  err13 = uart_set_pin(UART_USED1, VEBUS_TXD1, VEBUS_RXD1, -1, -1);

  Serial.println(err11);
  Serial.println(err12);
  Serial.println(err13);
  
  uart_config_t uart_config2 = 
  {
        .baud_rate = 9600,
        .data_bits = UART_DATA_8_BITS,
        .parity = UART_PARITY_EVEN,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, 
        .rx_flow_ctrl_thresh = 32,
  };
  err21 = uart_driver_install(UART_USED2, 256, 256, 0, NULL, 0);
  err22 = uart_param_config(UART_USED2, &uart_config2);   
  err23 = uart_set_pin(UART_USED2, VEBUS_TXD2, VEBUS_RXD2, -1, -1);
  
  Serial.println(err21);
  Serial.println(err22);
  Serial.println(err23);

  Serial.println("Done");
}

char buf1[3] = { 0xaF, 0x00, 0x55 };
char buf2[3] = { 0xa0, 0xFF, 0x55 };

void loop() 
{
  uart_write_bytes(UART_USED1, buf1, 3);
  buf1[2]++;
  uart_write_bytes(UART_USED2, buf2, 3);
  buf2[2]++;
  delay(100);   
  digitalWrite(LED, !digitalRead(LED));  
}
usb output

Code: Select all

Start
0
0
0
0
0
0
Done
This shows two hardware UART and the usb console working by circumnavigating the arduino functions.
"You are, quite simply, incorrect."

pic measured pin 11 and 12
Attachments
IMG_20240507_113132.jpg
IMG_20240507_113132.jpg (2.34 MiB) Viewed 2925 times

PepeTheGreat
Posts: 20
Joined: Mon Sep 18, 2023 3:54 am

Re: ESP32 S2 serial2 not working

Postby PepeTheGreat » Thu May 09, 2024 3:23 pm

I wonder if this will be corrected or if arduino usesrs will be limited to one serial on the ESP32S2 for eternity?

Who is online

Users browsing this forum: No registered users and 52 guests