This week is about making boards talk to each other.
For my node I connected two of my XIAO RP2040 boards over the I2C bus. I2C is a wired bus where every device has its own address, so it fits the assignment of a node with a bus address. One board is the sender and one is the receiver, and each board has its own local device. The sender reads my touch sensor from the Input Devices week and sends that value over the bus, and the receiver turns its LED on or off depending on the message it gets.
I joined the two boards with three wires: SDA to SDA, SCL to SCL, and GND to GND. The shared ground is important, without it the two boards do not agree on the signal levels. I gave the receiver the bus address 0x08.
The sender starts the I2C bus as a controller, reads the touch sensor, and sends a single byte to address 0x08, a 1 when the sensor is touched and a 0 when it is not.
#include <Wire.h>
#define TOUCH_PIN D9
#define RECEIVER 0x08
void setup() {
Wire.begin(); // join the bus as the controller
pinMode(TOUCH_PIN, INPUT);
}
void loop() {
byte msg = digitalRead(TOUCH_PIN); // 1 = touched, 0 = not
Wire.beginTransmission(RECEIVER);
Wire.write(msg);
Wire.endTransmission();
delay(100);
}
The receiver joins the same bus with its address 0x08. Whenever a byte arrives it reads it and lights its LED if the byte is 1.
#include <Wire.h>
#define LED_PIN D1
#define MY_ADDRESS 0x08
void setup() {
pinMode(LED_PIN, OUTPUT);
Wire.begin(MY_ADDRESS); // join the bus with my address
Wire.onReceive(onMessage);
}
void onMessage(int howMany) {
byte msg = Wire.read();
digitalWrite(LED_PIN, msg); // 1 turns the LED on, 0 off
}
void loop() {}
I uploaded the sender code to the first board and the receiver code to the second, kept them joined on the bus, and touched the sensor. When the message reaches the receiver at address 0x08 its LED turns on, which shows the two boards are talking on the bus.
As a group we sent a message between two student projects and checked that the data arrived correctly. The full write up is on our group page: group assignment page.
The two XIAO RP2040 boards wired together, SDA to SDA, SCL to SCL, and a shared ground
The receiver LED turning on when I touch the sensor on the sender board
The Serial Monitor showing the message arriving at address 0x08
Solution: I had forgotten the shared ground wire. Once I joined the grounds the messages came through.
Solution: I made sure the address in the sender and the receiver was the same value, 0x08.