πŸ“‘ Embedded Networking & Communications



🐲 1. Project Overview

This week I dove into embedded networking by getting two microcontroller boards to talk to each other. I used my custom Grove-style board built around the XIAO ESP32C3, which made it really easy to plug things in and start experimenting.

The goal was simple: use I2C communication to send data from one board (the master) to another (the slave) and have that data control an LED on the receiving end.



🐲 2. Communication Type & Setup

I went with I2C for this β€” a reliable two-wire protocol that’s perfect for communication between microcontrollers over short distances.

Wiring Setup:

  • SDA (Data): GPIO6 on both boards
  • SCL (Clock): GPIO7 on both boards
  • GND: Shared between boards
  • Power: Each board powered via USB to avoid shared current issues

One board played the role of master, sending commands, while the other acted as the slave, responding by turning an LED on or off.



🐲 3. Programming & Testing

Here’s what each board does:

  • Master: Sends the character '1' or '0' every 2 seconds to tell the slave whether to turn the LED on or off.
  • Slave: Reads the character and switches the LED accordingly.

πŸ‘¨β€πŸ’» Master Code



#include <Wire.h>

void setup() {
  Wire.begin(); // Join I2C bus as master
  Serial.begin(115200);
}

void loop() {
  Wire.beginTransmission(8); // Address of the slave
  Wire.write("1");       	// Command to turn on LED
  Wire.endTransmission();
  Serial.println("Sent: 1");
  delay(2000);

  Wire.beginTransmission(8);
  Wire.write("0");       	// Command to turn off LED
  Wire.endTransmission();
  Serial.println("Sent: 0");
  delay(2000);
}

πŸ‘¨β€πŸ’» Slave Code



#include <Wire.h>
#define LED_PIN 20

void setup() {
  Wire.begin(8);
  Wire.onReceive(receiveEvent);
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(115200);
}

void loop() {
  // Main logic handled in receiveEvent
}

void receiveEvent(int howMany) {
  while (Wire.available()) {
    char c = Wire.read();
    if (c == '1') {
      digitalWrite(LED_PIN, HIGH);
      Serial.println("LED ON");
    } else if (c == '0') {
      digitalWrite(LED_PIN, LOW);
      Serial.println("LED OFF");
    }
  }
}


🐲 4. Results & Observations

Once everything was connected and flashed, the setup worked perfectly:

  • LED Response: The slave board's LED turned on and off every 2 seconds based on commands from the master.
  • Serial Feedback: I was able to verify communication on both boards using the serial monitor, confirming proper data transfer.
  • I2C Stability: The connection was reliable and didn’t require any extra configuration beyond the basics.

What I Learned:

  • I2C is a great protocol for connecting multiple microcontrollers with just two data lines.
  • Internal pull-up resistors were sufficient for my setup β€” no extra components needed.
  • The Grove connectors helped me keep everything organized and eliminated wiring mistakes.


🐲 5. Hero Shot

⬆ Top 🏠 Home