Try Embedded

ESP32 and Arduino over Serial

An Arduino Uno reads a knob and sends it to an ESP32-C3 over a serial link; the ESP32 dims an LED to match and answers.

Communication · Intermediate

In a real product: Smart appliance (Smart home): A small MCU runs the appliance while a Wi-Fi module connects it, over a serial link.

A common split in real products: a 5 V Arduino does the I/O and an ESP32 does the networking. They talk over a serial link. Here the ESP32-C3 runs sketch.ino and the Uno runs uno.ino.

What you'll learn

  • Serial1 on an ESP32: a second UART on pins you choose, Serial1.begin(9600, SERIAL_8N1, RX, TX)
  • SoftwareSerial on the Uno, so its own Serial stays free for the monitor
  • Sending numbers as text lines and parsing them with toInt()

How it works

Every 200 ms the Uno reads the knob on A0 and sends the value as a line from pin 11 to the ESP32's RX (GPIO 2). The ESP32 reads the line, writes a quarter of it to the LED on GPIO 10 with analogWrite(), prints what it did and sends ok and the brightness back from its TX (GPIO 3) to the Uno's pin 10. The Uno prints every answer. Pick each board in the serial monitor's list to see both sides.

The ESP32's pins take at most 3.6 V, but the Uno's put out 5 V. So the Uno's TX goes through a voltage divider: 1 kΩ from pin 11 to the ESP32's RX, and 2 kΩ from there to GND, which brings 5 V down to about 3.3 V. Wire pin 11 straight to GPIO 2 and the simulator warns you, as a real ESP32 pin could be damaged. The other way needs nothing: the ESP32's 3.3 V high is enough for the Uno.

Try this

  • Turn the knob while it runs and watch both serial monitors.
  • Change the ESP32 to Serial1.begin(19200, ...) and see what arrives on each side.
  • Send a second value (a button on the Uno) and light another LED on the ESP32.

sketch.ino

// The ESP32-C3: knob readings arrive from the Uno on Serial1 (RX 2, TX 3), one per line. It dims the LED on GPIO 10
// to match and answers "ok" and the brightness.
String line;

void setup() {
  Serial.begin(115200);
  Serial1.begin(9600, SERIAL_8N1, 2, 3);
  pinMode(10, OUTPUT);
  Serial.println("Waiting for the Uno");
}

void loop() {
  while (Serial1.available()) {
    char c = Serial1.read();
    if (c == '\r') continue;
    if (c != '\n') { line += c; continue; }
    int knob = line.toInt();
    analogWrite(10, knob / 4);
    Serial.printf("knob %d, LED %d\n", knob, knob / 4);
    Serial1.printf("ok %d\n", knob / 4);
    line = "";
  }
}

uno.ino

// The Uno: sends the knob on A0 to the ESP32 every 200 ms over SoftwareSerial (TX 11, RX 10) and prints its answers.
#include <SoftwareSerial.h>

SoftwareSerial link(10, 11);  // RX, TX
unsigned long last = 0;

void setup() {
  Serial.begin(9600);
  link.begin(9600);
}

void loop() {
  if (millis() - last >= 200) {
    last = millis();
    link.println(analogRead(A0));
  }
  while (link.available()) Serial.write(link.read());
}

Lessons behind it