Built-in project · ESP32 DevKit V1 and Arduino Uno R3

An ESP32 and an Arduino Uno talking over serial: an ESP32 DevKit and an Arduino Uno

An ESP32 reads a knob and sends the number to an Arduino Uno over a serial wire; the Uno sets an LED’s brightness from it. The whole build, an ESP32 DevKit and an Arduino Uno and 3 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Intermediate About 25 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $37. On Mokxi they're free.

An ESP32 and an Arduino Uno talking over serial · ESP32 DevKit V1live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

What you'll learn

  • Sending data between two boards with software serial
  • Why two boards must share a ground
  • Scaling a 12-bit reading (0 to 4095) to PWM (0 to 255)
  • Why 3.3 V and 5 V boards need care when wired together
  • Difficulty: Intermediate
  • Time: About 25 minutes
  • Board: ESP32 DevKit V1, Arduino Uno R3
  • Parts: 5

How it works

The ESP32 reads a potentiometer on pin D34 and sends the number to the Uno as a line of text, four times a second. The wire runs from ESP32 pin D4 (its transmit pin) to Uno pin 2 (its receive pin), and the two boards share ground.

The Uno collects characters until it sees a newline, checks the line starts with knob , and turns the number into a PWM level for the LED on pin 9.

The link goes one way only. The ESP32 sends 3.3 V signals, which the Uno reads as high. The Uno sends 5 V, which a real ESP32 pin is not rated for.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Potentiometer pin 2ESP32 DevKit V1 pin D34
Potentiometer pin 3ESP32 DevKit V1 pin GND
Arduino Uno R3 pin GNDESP32 DevKit V1 pin GND
Arduino Uno R3 pin 2ESP32 DevKit V1 pin D4
Potentiometer pin 1ESP32 DevKit V1 pin 3V3
Resistor, 220 Ω pin 1Arduino Uno R3 pin 9
LED, green pin CArduino Uno R3 pin GND
LED, green pin AResistor, 220 Ω pin 2

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · ESP32 DevKit V1
// ESP32: reads a potentiometer on GPIO 34 and sends the reading to an
// Arduino Uno as a line of text, four times a second.
//
//   ESP32 GPIO 4 (TX)  ->  Uno pin 2 (RX)
//   ESP32 GND          --  Uno GND
//
// One way only, on purpose. The ESP32 drives 3.3 V, which an Uno reads as
// HIGH. The Uno drives 5 V, which a real ESP32 pin is not rated for, so a
// link back would need a divider or a level shifter on that wire.

#include <SoftwareSerial.h>

SoftwareSerial link(-1, 4);  // no RX, TX on GPIO 4

void setup() {
  Serial.begin(115200);
  link.begin(9600);
  Serial.println("ESP32: sending the knob to the Uno");
}

void loop() {
  int knob = analogRead(34);  // 0 to 4095 on the ESP32's 12-bit ADC
  link.print("knob ");
  link.println(knob);
  Serial.print("sent knob ");
  Serial.println(knob);
  delay(250);
}
sketch.ino · Arduino Uno R3
// Arduino Uno: reads "knob N" lines from the ESP32 on pin 2 and turns the
// LED on pin 9 up and down to match, with PWM.

#include <SoftwareSerial.h>

SoftwareSerial link(2, 3);  // RX on pin 2 (pin 3 is not used)

char line[24];
byte len = 0;

void setup() {
  Serial.begin(9600);
  link.begin(9600);
  pinMode(9, OUTPUT);
  Serial.println("Uno: listening to the ESP32");
}

void loop() {
  while (link.available()) {
    char c = link.read();
    if (c == '\n') {
      line[len] = 0;
      len = 0;
      if (strncmp(line, "knob ", 5) != 0) continue;
      int knob = atoi(line + 5);
      // The ESP32 counts to 4095; analogWrite takes 0 to 255.
      analogWrite(9, map(knob, 0, 4095, 0, 255));
      Serial.print("Uno got knob ");
      Serial.println(knob);
    } else if (c != '\r' && len < sizeof(line) - 1) {
      line[len++] = c;
    }
  }
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

On the ESP32, it opens the normal serial port at 115200 for the monitor, and starts the software serial link at 9600 baud. The link is made with no receive pin and transmit on GPIO 4, because this board only talks.

void setup() {
  Serial.begin(115200);
  link.begin(9600);
  Serial.println("ESP32: sending the knob to the Uno");
}

loop sketch.ino

On the Uno, it reads whatever has arrived on the link one character at a time. A newline ends a line. A line that starts with knob gets its number read with atoi, scaled with map, and sent to the LED with analogWrite.

void loop() {
  int knob = analogRead(34);  // 0 to 4095 on the ESP32's 12-bit ADC
  link.print("knob ");
  link.println(knob);
  Serial.print("sent knob ");
  Serial.println(knob);
  delay(250);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The LED never changes This project
    Check the ESP32 D4 wire goes to Uno pin 2, and that both boards share a ground.
  • Garbled text on the Uno This project
    Both sides must use 9600 baud on the link, which is what link.begin(9600) sets.
  • You want the Uno to answer the ESP32 This project
    Do not connect a 5 V Uno pin straight to the ESP32. Use a voltage divider or a level shifter on that wire.
  • The reading jumps around or only covers part of the range. Potentiometer
    Wire the two outer legs to 5 V and ground and the middle leg (the wiper) to an analog pin. Using the wrong two legs gives a dead or partial range.
  • Turning the knob does nothing, or the value is stuck at 0 or 1023. Potentiometer
    Check that the wiper goes to an analog input (A0 to A5 on an Uno), not a digital pin. Make sure both ends are connected, since a floating end gives noise.
  • The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
    Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
  • A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
    Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground.
  • The LED does not light, or it lights once and then dies. LED
    LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.