programacion ESP32 IDE arduino

FelipeSole
Posts: 1
Joined: Wed Oct 02, 2024 11:01 pm

programacion ESP32 IDE arduino

Postby FelipeSole » Wed Oct 02, 2024 11:18 pm

Good afternoon, I need to program an ESP32S3 with the Arduino environment but I want to program it from the USB pins because I need the UART ports available, and I also need the USB port to receive and send data with the 3 UART ports. thank you so much. I attach a code that I already have but it does not work correctly

#include <Arduino.h>

void setup() {
// Initialize USB communication (Serial)
Serial.begin(115200); // USB port at 115200 bps

// Initialize UART0, UART1, and UART2
Serial1.begin(9600, SERIAL_8N1, 16, 17); // UART0 on pins 16 (RX) and 17 (TX)
Serial2.begin(9600, SERIAL_8N1, 18, 19); // UART1 on pins 18 (RX) and 19 (TX)
Serial3.begin(9600, SERIAL_8N1, 22, 23); // UART2 on pins 22 (RX) and 23 (TX)
}

void loop() {
// Read data from the USB port and send it to UART0
if (Serial.available()) {
char incomingByte = Serial.read();
Serial1.write(incomingByte);
}

// Read data from UART0 and send it to the USB port
if (Serial1.available()) {
char incomingByte = Serial1.read();
Serial.write(incomingByte);
}

// Similar for UART1 and UART2
if (Serial2.available()) {
char incomingByte = Serial2.read();
Serial.write(incomingByte);
}

if (Serial3.available()) {
char incomingByte = Serial3.read();
Serial.write(incomingByte);
}
}

aliarifat794
Posts: 141
Joined: Sun Jun 23, 2024 6:18 pm

Re: programacion ESP32 IDE arduino

Postby aliarifat794 » Thu Oct 03, 2024 1:16 pm

You can edit your code like this:

Code: Select all

#include <Arduino.h>

// Native USB Serial (for USB communication via the USB-Serial-JTAG interface)
#define USBSerial Serial

void setup() {
  // Initialize USB communication (USB CDC)
  USBSerial.begin(115200); // USB port at 115200 bps

  // Initialize UART0, UART1, and UART2
  Serial1.begin(9600, SERIAL_8N1, 16, 17); // UART0 on pins 16 (RX) and 17 (TX)
  Serial2.begin(9600, SERIAL_8N1, 18, 19); // UART1 on pins 18 (RX) and 19 (TX)
  Serial3.begin(9600, SERIAL_8N1, 22, 23); // UART2 on pins 22 (RX) and 23 (TX)

  // Wait for the USB serial port to be ready before continuing
  while (!USBSerial) {
    delay(100); // Wait for the USB connection
  }
}

void loop() {
  // Read data from the USB port and send it to UART0
  if (USBSerial.available()) {
    char incomingByte = USBSerial.read();
    Serial1.write(incomingByte);
  }

  // Read data from UART0 and send it to the USB port
  if (Serial1.available()) {
    char incomingByte = Serial1.read();
    USBSerial.write(incomingByte);
  }

  // Similar for UART1 and UART2
  if (Serial2.available()) {
    char incomingByte = Serial2.read();
    USBSerial.write(incomingByte);
  }

  if (Serial3.available()) {
    char incomingByte = Serial3.read();
    USBSerial.write(incomingByte);
  }
}
The while (!USBSerial) line ensures that the code waits for the USB connection to be established before moving forward. You have to enable USB CDC on boot and select the correct board settings for ESP32-S3.
You will program the ESP32-S3 via its native USB port (D+ and D-), and the UARTs will work independently for your other communication needs.

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

Re: programacion ESP32 IDE arduino

Postby lbernstone » Fri Oct 04, 2024 4:07 am

Note that using a while !USBSerial loop like that means that it WILL wait for the USB to establish. If there is no host there, the device will appear hung.

Who is online

Users browsing this forum: No registered users and 64 guests