Main Protocol
I2C communication
Fab Academy 2026
Exploring communication protocols and integrating biometric sensors using I2C communication.
I2C communication
ESP32-C6 and MAX30102
Establish communication between embedded devices and sensors.
During the group assignment we explored different communication protocols commonly used in embedded systems and IoT projects.
We analyzed how each protocol works, its advantages, limitations and possible applications depending on the project requirements.
Understanding these protocols helped me decide which communication method was the most suitable for my wearable biometric project.
Different communication protocols offer different advantages depending on speed, distance, power consumption and system complexity.
During this week I researched and tested several common protocols used in embedded systems and IoT applications.
Connect the two boards like this:
Wire.h.Wire.begin(SDA_PIN, SCL_PIN, frequency).Wire.beginTransmission(address) and Wire.write(...).Wire.endTransmission().Wire.requestFrom(...).Wire.read().Wire.h.Wire.onReceive(...).Wire.onRequest(...).Wire.begin(address, SDA_PIN, SCL_PIN, frequency).I2C is recommended in situations where you need to connect multiple low-speed devices using only two wires (SDA and SCL). It is especially useful in embedded systems where pin availability is limited.
I2C is not ideal for high-speed or long-distance communication. In those cases, protocols like SPI or UART may be more suitable.
WiFi.h and WebServer.h.WiFi.begin(...).WebServer object.server.on(...).server.begin().server.handleClient() inside the main loop.Two boards can communicate through HTTP when both are connected to the same Wi-Fi network and one board knows the IP address of the other.
In this setup:
GET /sensor or GET /led/on to Board A.Wi-Fi with HTTP is recommended when you need wireless communication between devices that are already connected to the same network or have access to a router or hotspot.
Wi-Fi HTTP is not ideal for low-power applications or real-time critical systems, since it has higher latency and power consumption compared to simpler communication protocols.
WiFi.h and PubSubClient.h.MQTT over Wi-Fi is recommended for lightweight, efficient, and scalable communication between embedded devices and servers, especially in IoT systems.
MQTT is not ideal for direct device-to-device communication without a broker, or for applications that require complex request-response logic like HTTP.
Each receiver has a unique MAC address. The sender must know the receiver’s MAC address in order to communicate with it.
A simple way to find it is to upload the following temporary sketch to the receiver board once:
#include <WiFi.h>
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
Serial.println(WiFi.macAddress());
}
void loop() {
}
After uploading, open the Serial Monitor to read and copy the MAC address. Then, place that MAC address in the sender code.
WiFi.h and esp_now.h.WIFI_STA.esp_now_send(...).WiFi.h and esp_now.h.WIFI_STA.ESP-NOW is recommended for fast, low-power, and direct communication between ESP devices without the need for a Wi-Fi router or internet connection.
ESP-NOW is not suitable for large data transfers, internet-based communication, or systems that require standard IP networking and web integration.
BLE is recommended for short-range, low-power wireless communication between embedded devices, smartphones, and tablets.
BLE is not suitable for high data-rate applications, long-range communication, or continuous streaming of large datasets.
Design one board in KiCad with a button, an LED and a XIAO ESP32-C3.
Make the same PCB twice, one for each side of the conversation.
Read the MAC address of each ESP32-C3.
Send letters from one board to the other using ESP-NOW.
Check that both boards can send and receive.
First, I designed a PCB in KiCad. It has two 330 ohm resistors, a button, an LED and a XIAO ESP32-C3.
Since the goal this week was to make two projects talk to each other, I made the same board two times. Each board has its own microcontroller, so each one works as an independent node.
A button to write the Morse code.
An LED to show the message.
Two 330 ohm resistors.
The 2 boards ready to use.
Before using the main code, I needed the MAC address of each ESP32-C3. ESP-NOW uses it to know exactly which board should receive the message.
So I uploaded this short sketch to each board and read the address in the Serial Monitor.
#include <WiFi.h>
void setup() {
Serial.begin(115200);
delay(2000);
WiFi.mode(WIFI_STA);
Serial.print("MAC: ");
Serial.println(WiFi.macAddress());
}
void loop() {}
The two boards communicate using ESP-NOW. Each board detects when I press the button.
A quick press is a dot.
Holding the button is a dash.
If I stop pressing for a while, the letter is complete.
When that pause happens, the ESP32-C3 understands the letter and sends it to the other board. The other board receives the letter directly.
This is the final code I used on both boards:
#include <WiFi.h>
#include <esp_now.h>
#include <esp_wifi.h>
const int BUTTON_PIN = D0;
uint8_t MAC_A[] = {0xAC, 0x27, 0x6E, 0x7E, 0xCC, 0x24};
uint8_t MAC_B[] = {0xAC, 0x27, 0x6E, 0x80, 0xFD, 0x10};
const uint8_t WIFI_CHANNEL = 1;
const unsigned long DEBOUNCE_MS = 40;
const unsigned long DASH_MIN_MS = 250; // it waits 250 ms or more to consider the press a dash
const unsigned long LETTER_GAP_MS = 800; // if I stop pressing for this long, the letter is done
const int MAX_SYMBOLS = 7;
uint8_t peerAddress[6];
struct MorseEntry {
const char *code;
char letter;
};
const MorseEntry table[] = {
{".-", 'A'}, {"-...", 'B'}, {"-.-.", 'C'}, {"-..", 'D'}, {".", 'E'},
{"..-.", 'F'}, {"--.", 'G'}, {"....", 'H'}, {"..", 'I'}, {".---", 'J'},
{"-.-", 'K'}, {".-..", 'L'}, {"--", 'M'}, {"-.", 'N'}, {"---", 'O'},
{".--.", 'P'}, {"--.-", 'Q'}, {".-.", 'R'}, {"...", 'S'}, {"-", 'T'},
{"..-", 'U'}, {"...-", 'V'}, {".--", 'W'}, {"-..-", 'X'}, {"-.--", 'Y'},
{"--..", 'Z'},
{"-----", '0'}, {".----", '1'}, {"..---", '2'}, {"...--", '3'}, {"....-", '4'},
{".....", '5'}, {"-....", '6'}, {"--...", '7'}, {"---..", '8'}, {"----.", '9'}
};
char decodeMorse(const char *code) {
for (const auto &e : table) {
if (strcmp(code, e.code) == 0) return e.letter;
}
return '?';
}
char symbols[MAX_SYMBOLS + 1] = "";
int symCount = 0;
bool lastState = HIGH; // HIGH means the button is not pressed
unsigned long lastChange = 0;
unsigned long pressStart = 0;
unsigned long lastRelease = 0;
#if ESP_ARDUINO_VERSION_MAJOR >= 3
void onDataRecv(const esp_now_recv_info_t *info, const uint8_t *data, int len) {
const uint8_t *src = info->src_addr;
#else
void onDataRecv(const uint8_t *src, const uint8_t *data, int len) {
#endif
if (memcmp(src, peerAddress, 6) != 0) return; // ignore anything that is not from its brother
char code[16];
int n = min(len, 15);
memcpy(code, data, n);
code[n] = '\0';
Serial.print(decodeMorse(code));
}
void setup() {
Serial.begin(115200);
pinMode(BUTTON_PIN, INPUT_PULLUP);
WiFi.mode(WIFI_STA);
WiFi.disconnect();
esp_wifi_set_channel(WIFI_CHANNEL, WIFI_SECOND_CHAN_NONE);
// Check which board this is, so I know who to send to
uint8_t myMac[6];
WiFi.macAddress(myMac);
if (memcmp(myMac, MAC_A, 6) == 0) {
memcpy(peerAddress, MAC_B, 6);
} else if (memcmp(myMac, MAC_B, 6) == 0) {
memcpy(peerAddress, MAC_A, 6);
} else {
while (true) delay(1000); // if the MAC address is unknown the code stops here
}
if (esp_now_init() != ESP_OK) while (true) delay(1000);
esp_now_register_recv_cb(onDataRecv);
esp_now_peer_info_t peerInfo = {};
memcpy(peerInfo.peer_addr, peerAddress, 6);
peerInfo.channel = WIFI_CHANNEL;
peerInfo.ifidx = WIFI_IF_STA;
peerInfo.encrypt = false;
if (esp_now_add_peer(&peerInfo) != ESP_OK) while (true) delay(1000);
}
void loop() {
bool state = digitalRead(BUTTON_PIN);
if (state != lastState && millis() - lastChange > DEBOUNCE_MS) {
lastChange = millis();
lastState = state;
if (state == LOW) { // button pressed
pressStart = millis();
} else { // button released
char s = (millis() - pressStart >= DASH_MIN_MS) ? '-' : '.';
if (symCount < MAX_SYMBOLS) {
symbols[symCount++] = s;
symbols[symCount] = '\0';
}
lastRelease = millis();
}
}
// After a long pause the letter is finished, so I send it
if (symCount > 0 && lastState == HIGH && millis() - lastRelease > LETTER_GAP_MS) {
esp_now_send(peerAddress, (const uint8_t *)symbols, symCount + 1);
symCount = 0;
symbols[0] = '\0';
}
}
After uploading the code, I tested both PCBs and confirmed that both of them work.
In this video you can see the two boards communicating with each other at the same time.