← Back to All Weeks
Week 11

Networking and Communications

Assignment

This week is about making boards talk to each other.

  • Group assignment: send a message between two projects.
  • Individual assignment: design, build and connect a wired or wireless node with a network or bus address and a local input and/or output device.

What I Did

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.

Tools & Software Used

  • Two XIAO RP2040 boards: one sender, one receiver.
  • Arduino IDE: to write and upload the code to both boards.
  • Wire library: the Arduino library that runs the I2C bus.
  • Jumper wires: to connect the two boards.

Process

Step 1: Wire the two boards on one bus

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.

Step 2: Program the sender

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);
}

Step 3: Program the receiver

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() {}

Step 4: Test the link

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.

Group Assignment

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.

Documentation

What I Learned

  • On the I2C bus every device has its own address, which is how the right board picks up the message.
  • The two boards must share a ground or the signals do not line up.
  • A node is just a board with an address and its own local input or output on the bus.

Challenges & Solutions

Challenge 1: the receiver got nothing at first.

Solution: I had forgotten the shared ground wire. Once I joined the grounds the messages came through.

Challenge 2: the address did not match.

Solution: I made sure the address in the sender and the receiver was the same value, 0x08.