Two boards, one wire: an ESP32-C3 and an Arduino Uno
An ESP32-C3 pulses a number down one wire and an Arduino Uno counts it back out on the serial monitor. The whole build, an ESP32-C3 and an Arduino Uno and 4 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.
AdvancedRuns in your browser. Free, and no account needed.
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Talker: one board sending a number to another down a single wire.
//
// There is no bus and no protocol chip here: the number is simply that many
// pulses on GPIO 4, followed by a gap long enough that the far end knows the
// message has ended. GPIO 5 lights while a message is going out, so you can see
// the sender talking as well as the receiver listening.
//
// The line idles low and every pulse is 40 ms, which is glacial for a wire and
// exactly right for watching it happen. The receiver on the Arduino Uno counts
// rising edges on its interrupt pin and prints the total after 300 ms of
// silence.
//
// The two boards must share a ground for any of this to mean anything: 3.3 V
// on this board's pin is only 3.3 V at the Uno's input if both agree where
// zero is.
const int LINK = 4;
const int LAMP = 5;
const int PULSE_MS = 40;
const int GAP_MS = 900;
int value = 1;
void setup() {
pinMode(LINK, OUTPUT);
pinMode(LAMP, OUTPUT);
digitalWrite(LINK, LOW);
digitalWrite(LAMP, LOW);
Serial.begin(115200);
Serial.println("Mokxi ESP32-C3: sending on GPIO 4");
}
void loop() {
Serial.print("sending ");
Serial.println(value);
digitalWrite(LAMP, HIGH);
for (int i = 0; i < value; i++) {
digitalWrite(LINK, HIGH);
delay(PULSE_MS);
digitalWrite(LINK, LOW);
delay(PULSE_MS);
}
digitalWrite(LAMP, LOW);
value = value % 6 + 1;
delay(GAP_MS);
}
// Listener: the other half of the two-board link.
//
// pin 2 the wire from the other board's GPIO, counted on INT0
// pin 9 a lamp that follows the line, so you can see the traffic
//
// The sender puts out a number as that many pulses and then goes quiet. There
// is no framing and no clock: what separates one message from the next is
// silence, so this counts rising edges and prints the total once nothing has
// arrived for GAP_MS.
//
// The count is touched by the interrupt and by loop(), so it is volatile and
// loop() takes its copy with interrupts off, since an unsigned long is four bytes
// and this chip moves one byte at a time, so an interrupt landing halfway
// through the read would hand back a number that never existed.
//
// pin 2 has no pull-up here on purpose. The far board drives the line both
// ways, and a pull-up fighting a push-pull output is current spent for nothing.
const int LINK = 2;
const int LAMP = 9;
const unsigned long GAP_MS = 300;
volatile unsigned long pulses = 0;
volatile unsigned long lastEdge = 0;
static void onEdge() {
pulses++;
lastEdge = millis();
}
void setup() {
pinMode(LINK, INPUT);
pinMode(LAMP, OUTPUT);
Serial.begin(115200);
attachInterrupt(digitalPinToInterrupt(LINK), onEdge, RISING);
Serial.println("Mokxi Arduino Uno: counting pulses on pin 2");
}
void loop() {
digitalWrite(LAMP, digitalRead(LINK) == HIGH ? HIGH : LOW);
noInterrupts();
unsigned long count = pulses;
unsigned long last = lastEdge;
interrupts();
if (count > 0 && millis() - last > GAP_MS) {
Serial.print("received ");
Serial.println(count);
noInterrupts();
pulses -= count;
interrupts();
}
delay(2);
}
Parts list
7 parts, plus the jumper wires. Every one is in the editor's parts bin.
- 1 × ESP32-C3-DevKitM-1
- 1 × Arduino Uno R3
- 1 × Full-size breadboard
- 2 × Resistor, 220 Ω
- 1 × LED, blue
- 1 × LED, green
How it is wired
6 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.
- Ground: ESP32-C3-DevKitM-1 pin GND; Arduino Uno R3 pin GND; LED, blue pin C; LED, green pin C
- ESP32-C3-DevKitM-1 pin 4; Arduino Uno R3 pin 2
- ESP32-C3-DevKitM-1 pin 5; Resistor, 220 Ω (1) pin 1
- Arduino Uno R3 pin 9; Resistor, 220 Ω (2) pin 1
- Resistor, 220 Ω (1) pin 2; LED, blue pin A
- Resistor, 220 Ω (2) pin 2; LED, green pin A
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.