Built-in project · ESP32-C3-DevKitM-1

ESP32-C3 serial

The board on its own, printing its uptime once a second on UART0 and echoing anything typed into the serial monitor. The whole build, an ESP32-C3, 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.

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

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

ESP32-C3 serial · ESP32-C3-DevKitM-1live0.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

  • Printing with Serial.println
  • Timing without blocking, using millis
  • Reading typed input with Serial.available
  • Showing a character as a hex code
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: ESP32-C3-DevKitM-1
  • Parts: 2

How it works

This project is just the board, with no extra parts. It talks to you through the serial monitor on UART0. At start it prints a greeting and shows pi to five decimal places.

After that it reports its uptime in milliseconds once a second. It also listens: type something into the monitor and the board replies with each character and its hex code. A newline is shown as a word.

Parts list

2 parts, plus the jumper wires. They retail for about $13; on Mokxi they're free.

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-C3-DevKitM-1
// Serial: print once a second, echo what arrives.
//
// Serial is UART0, which is the board's channel to the serial monitor at the
// bottom of the window. Type into the monitor and the characters come back
// with a prefix.
//
// Serial.print blocks while the 128-byte transmit FIFO is full, so a long
// line costs simulated time at the configured baud rate, exactly as it would
// on the real board.

unsigned long lastReport = 0;

void setup() {
  Serial.begin(115200);
  Serial.println("Mokxi ESP32-C3: serial example");
  Serial.print("pi to five places is ");
  Serial.println(3.14159265, 5);
}

void loop() {
  unsigned long now = millis();
  if (now - lastReport >= 1000) {
    lastReport = now;
    Serial.print("up ");
    Serial.print(now);
    Serial.println(" ms");
  }

  while (Serial.available() > 0) {
    int c = Serial.read();
    if (c == '\r') {
      continue;
    }
    Serial.print("you typed: ");
    if (c == '\n') {
      Serial.println("<newline>");
    } else {
      Serial.print((char)c);
      Serial.print(" (0x");
      Serial.print(c, HEX);
      Serial.println(")");
    }
  }
}

Code walkthrough

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

setup sketch.ino

Opens serial at 115200 baud, then prints a greeting and pi. The 3.14159265, 5 form tells println to show five decimal places.

void setup() {
  Serial.begin(115200);
  Serial.println("Mokxi ESP32-C3: serial example");
  Serial.print("pi to five places is ");
  Serial.println(3.14159265, 5);
}

loop sketch.ino

Does two jobs. First it compares millis() to lastReport, and once 1000 ms have passed it prints the uptime. Then a while loop reads every waiting byte with Serial.read, skips carriage returns, and prints each character with its hex value.

void loop() {
  unsigned long now = millis();
  if (now - lastReport >= 1000) {
    lastReport = now;
    Serial.print("up ");
    Serial.print(now);
    Serial.println(" ms");
  }

  while (Serial.available() > 0) {
    int c = Serial.read();
    if (c == '\r') {
      continue;
    }
    Serial.print("you typed: ");
    if (c == '\n') {
      Serial.println("<newline>");
    } else {
      Serial.print((char)c);
      Serial.print(" (0x");
      Serial.print(c, HEX);
      Serial.println(")");
    }
  }
}

Troubleshooting

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

  • Garbled text in the serial monitor This project
    The monitor speed must match Serial.begin(115200).
  • Typed characters get no reply This project
    Send the text from the monitor's input box, and make sure the sketch is running.
  • Upload fails or the board is not found by the computer. ESP32-C3-DevKitM-1
    Pick the ESP32-C3 board in your tools and the right port. If it still fails, hold BOOT, tap RESET, then release BOOT to enter download mode and try again.
  • GPIO pins act strangely or the board resets. ESP32-C3-DevKitM-1
    GPIO is 3.3 V only, so never feed it 5 V. GPIO 18 and 19 are USB, and GPIO 2, 8 and 9 are boot strapping pins. Avoid loading them at reset.

Change it and keep it

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