11. Embedded Networking and Communications¶
Group assignment: Send a message between two projects Document your work to the group work page and reflect on your individual page what you learned
Individual assignment: design, build and connect wired or wireless node(s) with network or bus addresses and a local input and/or output devices
Group assignment:¶
This week, we tested I2C communication with my instructor Saverio, using two Arduino Uno. Link to the group Assignment is here
Why Learn Embedded Networking & Comms¶
In the development of smart hardware(such as XIAO-ESP32-C3), Learning the three communication methods of serial communication, UAR,I2C, and WiFi is the core foundation for mastering the development of intelligent hardware. They correspond to different application scenarios and hardware interaction methods.
For me (a beginner), it’s difficult to distinguish them. therefore,I asked DEEPSEEK “Why learn UART, I2C, and WiFi communications when studying smart hardware (e.g., XIAO-ESP-32)?” and received the following answers.
1.Serial Communication (UART) - The “Basic Dialogue” for Hardware Development¶
-
The Most Basic Debugging Tool:When you write code to control a sensor or execute a function, how do you know if the hardware is working correctly? The serial port acts like a “chat window,” allowing you to print data in real time (e.g., temperature values, error messages) to quickly troubleshoot issues.
-
Simple Device-to-Device Communication:Many modules (e.g., Bluetooth, GPS) default to using the serial port to interact with the main control chip. Learning serial communication makes it easy to connect these devices.
2.I2C - Efficient Collaboration of Hardware Modules¶
-
Save Pins, Connect Multiple Devices :Arduino has a limited number of pins, while I2C only requires two wires (SDA, SCL) to control dozens of devices (each with a unique address), making it perfect for expanding functionality.
-
Standardized Protocol :Most sensors (such as accelerometers and displays) support I2C. Once you learn it, you can easily reuse code.
3.WiFi - The “Internet Gateway” for Smart Hardware¶
-
Connect hardware to the internet: WiFi is the core of IoT (Internet of Things). After learning it, you can make your XIAO-ESP32-C3:Remotely control devices (e.g., turn lights on/off via mobile app)、Upload data to cloud (e.g., record temperature/humidity to server)、Interact with other devices (e.g., automatically send email when temperature is too high)
-
Advanced applications: Such as building web servers, using MQTT protocol, connecting to smart home platforms (Home Assistant).
Introduction to Three Communication Methods¶
Next, I asked DeepSeek: “I want to learn communication through xiao-esp32-c3: serial communication I2C、WIFI, Can you introduce these three communication methods to me?
1. UART Communication(Like “two people whispering face-to-face”)¶
- Wiring: TX (Transmit) → RX (Receive), RX → TX (cross-connection)
- Characteristics:
- Simplest one-to-one communication (e.g., XIAO-ESP32-C3 talking to PC serial monitor)
- No addressing needed (but requires matching baud rate like 115200)
-
XIAO-ESP32-C3 Usage:
Serial.begin(115200); // Initialize serial Serial.println("Hello!"); // Send data
2. I2C Communication(Like “roll call in class”)¶
- Wiring: SDA (Data) + SCL (Clock) - all devices share these two wires
-
Characteristics:
- One-to-many (master controls multiple slaves like sensors)
- Each slave has unique address (e.g., OLED address 0x3C)
- Synchronized by clock signal
-
XIAO-ESP32-C3 Usage:
c #include <Wire.h> Wire.begin(); // Initialize I2C (master) Wire.beginTransmission(0x3C); // Device address Wire.write("ABC"); // Send data Wire.endTransmission();
3. WiFi Communication(Like “WeChat group chat”)¶
- Wiring: Wireless (no cables)
- Characteristics:
- Long range (whole room/house)
- Needs network protocols (TCP/IP)
- Connects multiple devices (phones, PCs, cloud)
- XIAO-ESP32-C3 Usage:
#include <WiFi.h> WiFi.begin("SSID", "password"); // Connect WiFi.localIP(); // Get IP address // Use WiFiClient/WebServer for data
Comparison Table¶
Feature | UART | I2C | WiFi |
---|---|---|---|
Connection | Wired | Wired | Wireless |
Devices | 1-to-1 | 1-to-many | Many-to-many |
Distance | Few meters | <1 meter | 10s of meters |
Best For | Debugging | Sensor clusters | IoT/remote control |
Individual assignment:¶
design, build and connect wired or wireless node(s) with network or bus addresses and a local input and/or output devices
Program Function Description:¶
This week, I learned about I2C communication and implemented the control of the D13 LED on an Arduino (Slave) by a XIAO-ESP32-C3 (Master) via I²C to turn it on and off.
- The master device (Master - XIAO-ESP32-C3)
- Receives user input via the serial port (Serial).
- Send “1” → The LED of the Slave device (Arduino) lights up.
- Send “0” → The LED of the Slave device (Arduino) turns off.
-
Sends instructions to the slave device (Arduino) via I²C communication.
-
The slave device (Slave - Arduino)
- Listens to the I²C bus and receives instructions sent by the Master:
- When 1 is received → Light up the D13 LED.
- When receiving 0 → Turn off D13 LED.
- Prints the received data through the serial port (for debugging purposes).
Hardware connection¶
I²C communication hardware wiring (Master: XIAO-ESP32-C3 → Slave: Arduino)
Master Pin | Slave Pin |
---|---|
D4 (SDA) | SDA |
D5 (SCL) | SCL |
GND | GND |
- | D13 → LED+ |
Program testing¶
Firstly, I will use the code of Bhutanese students as a reference.Then, added the serial read function in the master code and the led on/off function in the slave code.
1.esp master writelArduino lDE2.3.5¶
#include <Wire.h>
const int slaveAddress = 0x08; // Set the I2C address for the slave device
String incomingmessage = "0"; //this variable type is "string" just text
void setup() {
Wire.begin(); // Initialize I2C communication as master
Serial.begin(115200); // Initialize serial communication for debugging
}
void loop() {
if(Serial.available()) { //if some data appears on the serial port
incomingmessage=Serial.readString(); //read text data from serial
Serial.println(incomingmessage); //print the text on serial
}
if (incomingmessage == "1") { //if the text message is "1"
Wire.beginTransmission(slaveAddress); // Start I2C transmission to the slave device
Wire.write(1); // Send data over I2C
Wire.endTransmission(); // End I2C transmission
Serial.println("LED ON"); // Print a message to the serial monitor
}
if (incomingmessage == "0") { //if the text message is "0"
Wire.beginTransmission(slaveAddress); // Start I2C transmission to the slave device
Wire.write(0); // Send data over I2C
Wire.endTransmission(); // End I2C transmission
Serial.println("LED OFF"); // Print a message to the serial monitor
}
}
2.arduino_slave_receive led |Arduino lDE 2.3.5¶
#include <Wire.h>
int val = 0;
const int ledPin = 13; // Replace with the actual LED pin
const int I2C_ADDRESS = 0x08; // Set the same I2C address as the master device
void setup() {
Wire.begin(I2C_ADDRESS); // Initialize I2C communication as slave
Wire.onReceive(receiveEvent); // Register the receiveEvent function
pinMode(ledPin, OUTPUT); // Set LED pin as output
Serial.begin(115200); // Initialize serial communication for debugging
}
void loop() {
if (val == 0){
digitalWrite(ledPin, LOW);
}
else {
digitalWrite(ledPin, HIGH);
}// No need for a loop function in this case
}
void receiveEvent(int bytesReceived) {
// Read the received data
while (Wire.available()) {
val = Wire.read();
Serial.println(val);
}
}
Summary:¶
I used the final project board for this assignment because the other board I fabricated didn’t have D4 and D5 connected to the Xiao.
At the beginning , when I started to try I2C, I thought it was very simple. But in fact, it took me and my mentor a lot of time. We tried many times, but the XIAO-ESP32-C3 on my circuit board always failed to receive messages on the serial port. After troubleshooting many times, we realized that the SDA pin D5 of the XIAO was connected to a pull-down resistor. After I (temporarily) unsoldered the 10k ohm resistor the SDA line started working properly. (OH MY GOD!)