ESP32 uart doesn't work in Arduino CH340

Hello everyone,

I have a ESP32-S CAM board and 2 Arduino MEGA2560, one with CH340 serial ic and another with original ATMEGA serial 16u2. This 3 boards is working perfectly, i have that for a long time.

Now, i'm trying to get the data in serial of Arduino and transmit using ESP32 Bluetooth.
I found in IDE examples the "SerialtoSerialBT" , and it work perfectly using ESP32 and the original Arduino(16u2 serial IC).

I'm using TX0 and RX0 pins on Arduino and UOT and UOR pin on ESP32

But when i try to connect to CH340 arduino, the arduino TX led just blinks every 0,5S. Ok, it looks like a HW problem, alright?! I thought the same.

So i did a different test:

I connected the ESP32 to 16u2 Arduino, stabilish the comunication, and after that, i just changed the TX and RX wires from 16U2 Arduino to the CH340 Arduino, and it work! :face_with_raised_eyebrow:

So it could'nt a HW problem, because if i do this procedure, the CH340 Arduino work with ESP32.

I read a lot on google and could'nt find an answer. Does ESP32 need to send a reset command or a specific byte before start running? Why 16U2 don't need this?

The code:

#include "BluetoothSerial.h"

//#define USE_PIN // Uncomment this to use PIN during pairing. The pin is specified on the line below
const char *pin = "1234"; // Change this to more secure PIN.

String device_name = "ESP32-BT-Slave";

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

#if !defined(CONFIG_BT_SPP_ENABLED)
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
#endif

BluetoothSerial SerialBT;

void setup() {
  Serial.begin(115200);
  SerialBT.begin(device_name); //Bluetooth device name
  Serial.printf("The device with name \"%s\" is started.\nNow you can pair it with Bluetooth!\n", device_name.c_str());
  //Serial.printf("The device with name \"%s\" and MAC address %s is started.\nNow you can pair it with Bluetooth!\n", device_name.c_str(), SerialBT.getMacString()); // Use this after the MAC method is implemented
  #ifdef USE_PIN
    SerialBT.setPin(pin);
    Serial.println("Using PIN");
  #endif
}

void loop() {
  if (Serial.available()) {
    SerialBT.write(Serial.read());
  }
  if (SerialBT.available()) {
    Serial.write(SerialBT.read());
  }
  delay(20);
}

I'm getting crazy with that. :weary:

Thank you guys.

The only ESP32 with classic bluetooth using BluetoothSerial.h is the original model without an S2 or S3 designator. I do not thing the ESP32 CAM board has either BLE or BT classic. Can you explain more about your ESP32 CAM board and why you think it has Serial Bluetooth?

For connecting the CAM board to the Mega, you are better to use Serial 1,2 or 3 on the Mega instead of usingTX0 and RX0 on pins 0 and 1 which goes through the TTL/USB convertor.

You also will have to consider the voltage level difference between the Mega and ESP32 uarts. You will need a level shifter or voltage divider on the Mega Tx to ESP Rx. The Esp TX may or may not have adequate voltage levels for the Mega Rx.

have a look at esp32-cam-intro
both Bluetooth Classic and BLE work on my ESP-CAM - module has lable ESP32-S

Hi. Thank you for the answer.

My board is a ESP32S. I just want to read the data on serial and transmit via bluetooth.

It already worked with Arduino 16u2, but not with CH340.

Due a project limitations, i can only use the Serial 0 of Arduinos(The same that is used by USB converter).

Regarding the voltage levels, i consired that, and as i said, the Arduino 16U2 worked with ESP32, and CH340 worked too (if i connect ESP32 first in 16U2 and after in CH340 without stoping the comunication). :smiling_face_with_tear:

Yes, I appears I made a mistake and confused the -S with a -S2.

Can you explain more about this.

It would be unusual for a Mega with the multiple hardware Serial Uarts, but perhaps you could use Software Serial.

if you need to use serial IO on the ESP-CAM you can use a hardware serial port, e.g. using Serial1 pin 14 and 15

// ESP32-CAM  Serial1 test

// for loopback test connect pins 14 and 15

#define RXD1 14
#define TXD1 15

void setup() {
  // initialize both serial ports:
  Serial.begin(115200);
  Serial1.begin(115200, SERIAL_8N1, RXD1, TXD1);
  Serial.println();
  Serial.println("serial1  test Rx pin 14 Tx pin 15");
}

void loop() {
  // read from port 1, send to port 0:
  if (Serial1.available()) {
    int inByte = Serial1.read();
    Serial.write(inByte);
  }

  // read from port 0, send to port 1:
  if (Serial.available()) {
    int inByte = Serial.read();
    //Serial.write(inByte);
    Serial1.write(inByte);
  }
}

My project board has connection to bluetooth only in Serial 0 of ATMEGA. I cannot change any hardware specification.

The question is: Why CH340 Board work with ESP32 if i connect it first to 16u2 board and after change to CH340 board?

It proof that is not a hardware problem. CH340 Arduino can communicate with ESP32. But why i need to start it in 16u2 board?

Hi Horace,

I'm using 1 and 3 pin of ESp32 to comunicate with serial. According this image is the only hardware serial on esp32-CAM, or not?

the ESP32 has 3 hardware serial ports - Usually UART0 is used to program the device (GPIO1 U0TXD and GPIO3 U0RXD on the ESP32-CAM)
Serial1 and Serial2 can be mapped to pins as shown in post 7, e.g.

Serial1.begin(115200, SERIAL_8N1, RXD1, TXD1);

this is how I cannected it to Mega hardware Serial1

the Mega uses 5Volt logic the ESP32 3.3V so any 5V signals from the Mega (e.g. Mega TX1) require a level converter or potential divider to convert 5V signals to 3.3V

@luismiguel_fco says that he can not use anything other than Hardware Serial 0 on the Mega and this is interactive with the USB converter used on the board.

My project board has connection to bluetooth only in Serial 0 of ATMEGA. I cannot change any hardware specification.

why?
are the boards built into an existing system where the interconnections cannot be changed?
did you not prototype the system before going to this stage?

Because the project is done, and i cannot change software and hardware of this CH340 Arduino board.

I need to know why sometimes bluetooth work and another don't.

An update: Now sometimes it work without that procedure(Connect in 16u2 and after in CH340), but sometimes it doesn't work too. I'm very confused.

Why confused? It's not certain, but the level translation problem is a huge red flag.

The fact you built without prototype, doesn't prohibit you from building one now. Then you could test it with a correct serial interface.

Also you have to examine the particular serial I/O circuit on the particular, exact Arduino and ESP32 that you are using, to confirm the electrical characteristics. That would support or prevent certain types of interface circuit, from working well.

This project call Speeduino.

This use the serial 0 to send and receive data(that is used by ch340 usb converter) and serial 3 that is used to only send data.

The fw has hundred thousands code lines, and is not useful change the code because it has thousand variables and algorithm that depend on each other.

I'm trying to develop a new bluetooth to this project, but i'm facing this problem.

The question is: If i wouldn't know what is the problem, i cannot solve this in new board in the future.

Before modify the hardware, i would like to understand why this is not working, not only modify it... It can be useful to avoid of new problems in the future.

Does anyone has a guess why this is not working?
Impedance, bytes, protocol, pin state??

Maybe do i need to separate the ch340 with diodes or resistor to assure that the line impedance is correct?

https://wiki.speeduino.com/en/Bluetooth

Does the Mega with the HC05 bluetooth module work correctly with both the CH340 and the 16u2 interface chips?

If it's written top down and modular, that is never a problem, you can test modules independently. In such code the variables dependency is explicitly managed and controlled. "encapsulation".

Ok, what do you suggest to do/test in the code?

As i said, the hardware is done. I cannot change the connection.

In your situation, you have no way to move forward, other than software or hardware testing. Make a choice. Typically the way forward would involve trying to make the hardware work with a simple test sketch.

If the hardware fails, then you know it can't be "done", and you will have to change it, or live with the unreliable operation.

If it passes, you know the chances are very high, it's a software problem and you will have to either:

  • find the problem and fix it
  • re-write it from scratch, being more careful

If I recall this thread correctly, there is a potential serial interface issue which should be included with simplified radio testing.