This week we needed to send a message between two projects. Sounds simple enough, right?
Well, it took us a few tries to get it working properly, but we figured it out in the end!
We wanted to make something pretty cool - press a button on one board and have LEDs light up on BOTH boards. The idea was to use two XIAO RP2040 boards and make them communicate using serial communication (UART). We thought this would be easier than trying to do wireless stuff, and honestly, we were right!
Instead of going with WiFi or Bluetooth (which seemed complicated), we decided to use good old UART serial communication. It's basically like having a conversation between two boards using just two wires. Pretty neat actually!
Think of it like walkie-talkies, but with wires:
We used our custom XIAO RP2040 boards from previous weeks. Each one already had an LED and button wired up, which made things easier.
Board 1 Pin | Board 2 Pin | What it does |
---|---|---|
TX (Pin 0) | RX (Pin 1) | Board 1 talks to Board 2 |
RX (Pin 1) | TX (Pin 0) | Board 2 talks back to Board 1 |
GND | GND | Shared ground (super important!) |
LED on D2 | LED on D2 | LEDs that light up |
Button on D3 | Button on D3 | Buttons to press |
The programming part was actually pretty straightforward once we figured out the logic. Both boards run almost the same code, just with tiny differences.
#define LED_PIN D2 #define BTN_PIN D3 // These were the same on both boards
Serial1.begin(9600); // Start talking at 9600 baud // We tried other speeds but 9600 worked best
if (digitalRead(BTN_PIN) == LOW) { Serial1.println("LED_ON"); // Tell the other board to turn on its LED digitalWrite(LED_PIN, HIGH); // Turn on our own LED too }
if (Serial1.available()) { String message = Serial1.readString(); if (message.indexOf("LED_ON") >= 0) { digitalWrite(LED_PIN, HIGH); // Turn on LED when we get the message } }
Let's be honest - it didn't work the first time. We had to debug a few things:
When we finally got it working, it was pretty satisfying. Press one button, both LEDs light up almost instantly. The communication was fast and reliable.
Communication Method | Good stuff | Not so good stuff |
---|---|---|
UART (what we used) | Simple, reliable, fast, low power | Need wires, short distance only |
ESP-NOW | No wires, works far apart | Way more complicated, need ESP32 boards |
I2C | Can connect many devices | More complex addressing system |
SPI | Super fast | Need more wires, overkill for our project |
This project taught us a lot about communication between microcontrollers:
Overall, this was a really fun project! We managed to get two boards talking to each other using serial communication. It might not be as fancy as wireless, but it's reliable and easy to understand. Plus, seeing both LEDs light up when you press one button never gets old!
Pro tip: Always check your wiring twice before blaming the code. We learned this the hard way!