This week, after learning I2C communication, I used my own PCB with a XIAO-ESP32-C3 (master) to communicate. It is connected to a computer to read serial input (keyboard 1 or 2) and control the LED connected to D7 on the Arduino (slave). I also used an Arduino UNO to create an RFID door lock system.
I2C communication(XIAO-ESP32-C3 & Arduino Uno)
An RFID Door Lock System
I used AI-assisted tools to help me learn and understand Embedded Networking and Communications, as well as I2C communication.
Refers to how these systems exchange information via wired or wireless networks to achieve collaboration and control.
| Type | Common Protocol | Features | Applicable Scenarios |
|---|---|---|---|
| Wired | UART | Simple, point-to-point | Direct communication between Arduino UNO, Xiao ESP32 |
| I2C | Supports multi-master/multi-slave, two-wire bus | Multiple sensors, multi-slave control | |
| SPI | High-speed, full duplex | Sensors, high-speed peripherals | |
| Wireless | Wi-Fi | Long distance, Internet access | Smart home, remote monitoring |
| Bluetooth/BLE | Low power, short distance | Mobile interaction, data synchronization | |
| LoRa | Ultra long distance, low speed | IoT, sensor networks | |
| Zigbee | Self-organizing, low power | Smart home, sensor networks |
I2C (Inter-Integrated Circuit) is a two-wire, synchronous, half-duplex, master-slave serial communication bus developed by Philips (now NXP).
| Wire | Function |
|---|---|
| SDA (Serial Data) | Data line |
| SCL (Serial Clock) | Clock line |
Master board: connected to computer, reads serial input (keyboard A or B), sends it via I2C to the slave.
Slave board: receives I2C message and controls onboard LED (D13) accordingly.
Step 1: Upload Slave code
Step 2: Upload Master code
| Master UNO | Slave UNO | Note |
|---|---|---|
| A4 (SDA) | A4 (SDA) | I2C data line |
| A5 (SCL) | A5 (SCL) | I2C clock line |
| GND | GND | Common ground |
| 5V | 5V | Power optional |
Press A on the keyboard → LED turns on;Press B on the keyboard → LED turns off.
video
After learning I2C communication, I used my own PCB with a XIAO-ESP32-C3 (master) to communicate. It is connected to a computer to read serial input (keyboard 1 or 2) and control the LED connected to D7 on the Arduino (slave)
LED connection
code
Serial output: I2C Slave Ready
code
The serial monitor shows: ESP-ROM: esp32c3-api1-20210207 I2C Master is ready. Press 1 to turn the LED ON, 2 to turn it OFF.
| Signal | ESP32-C3 | Arduino Uno |
|---|---|---|
| SDA | D6 | SDA |
| SCL | D7 | SCL |
| GND | GND | GND |
| LED | — | D7 → LED → GND |
Press 1 on the keyboard → LED turns on; Press 2 on the keyboard → LED turns off.
Video
In an access control system, the RC522 module is used to detect RFID cards and read their UID. The node sends the UID to the master controller through a wired I2C connection. The master controller then parses the UID and instructs the display to record and show the information.
RFID uses electromagnetic waves in the radio frequency spectrum for communication and data transfer. RFID tags are used in many industries and we can find them in product tags from stores to security access cards. RFID tags can expedite the checkout and can be used for antitheft measures.
Components of an RFID System An RFID system typically consists of several main components, each playing a different role in the system.
| Component | Description |
|---|---|
| RFID Tag | Used to store and transmit information. |
| RFID Reader | Reads the data from the RFID tag by emitting radio waves. It communicates with a microcontroller or computer. |
| Antenna | Transmits and receives radio waves between the RFID tag and reader, influencing the system's range and performance. |
| Middleware | Software that manages and processes the data between the RFID system and application, facilitating communication with the database. |
| Software | Used to process and manage the data from RFID tags and integrate it into applications (e.g., security, inventory management). |
| Database | Stores the RFID tag information and related data, enabling real-time updates and retrieval of relevant data for processing. |
| Power Supply | Provides necessary power for the RFID system, especially for active tags and readers. |
The RC522 RFID is a popular RFID reader/writer module that communicates with microcontrollers like Arduino. It uses the MFRC522 chip and supports reading and writing of 13.56 MHz RFID cards/tags.
RC522 RFID Module Pins and Descriptions
| Pin | Description |
|---|---|
| SDA / SCL | I2C Communication pins: Data (SDA) and Clock (SCL). |
| SS / SCK / MOSI / MISO | SPI Communication pins: Slave Select (SS), Clock (SCK), Master Out Slave In (MOSI), and Master In Slave Out (MISO). |
| RX / TX | UART Communication pins for serial data transmission. |
| IRQ | Interrupt signal from the module to indicate RFID tag detection. |
| GND | Ground pin that needs to be connected to the GND pin on the Arduino. |
| RST | Reset pin for the module. |
| VCC | Supply pin for the module. The supply voltage ranges from 2.5V to 3.3V and must be connected to the 3.3V pin on the Arduino. |
RC522 RFID Reader/Writer Module|Parts&Circuit Diagram
RFID Module & Arduino Uno Board & RFID Key Tag & Relay Module & I2C LCD1602 & Breadboard & Jumper Wires
| RFID Module | Arduino Uno |
|---|---|
| 3.3V | 3.3V |
| GND | GND |
| RST | 2 |
| SDA (SS) | 10 |
| MOSI | 11 |
| MISO | 12 |
| SCK | 13 |
| IRQ | 7 |
| I2C LCD1602 | Arduino Uno |
|---|---|
| GND | GND |
| VCC | 5V |
| SDA | A4 |
| SCL | A5 |
| Relay Module | Arduino Uno |
|---|---|
| SIG | 8 |
| VCC | 5V |
| GND | GND |
MFRC522.Notes:Place the RFID key tag near the center of the reader
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 2
MFRC522 rfid(SS_PIN, RST_PIN);
void setup() {
Serial.begin(9600);
SPI.begin();
rfid.PCD_Init();
Serial.println("Scan RFID Card...");
}
void loop() {
if (!rfid.PICC_IsNewCardPresent()) {
return;
}
if (!rfid.PICC_ReadCardSerial()) {
return;
}
Serial.print("Card ID: ");
for (byte i = 0; i < rfid.uid.size; i++) {
Serial.print(rfid.uid.uidByte[i], HEX);
Serial.print(" ");
}
Serial.println();
rfid.PICC_HaltA();
}
Serial Debugging: Viewing scanned card IDs in the Serial Monitor.
Card ID 1: 73 B2 63 D
Card ID 2: D3 E1 5C A3
| Serial Monitor Output | Possible Issue | Suggested Solution |
|---|---|---|
| ⚠ ERROR: RFID module not detected | Wiring issue or incorrect power supply | ensure standard SPI pins and 3.3V power are used |
| No card detected | No RFID card detected near reader | Try a different RFID card, adjust scanning angle, or ensure the card is 13.56MHz. |
| Failed to read card | Read failure due to card or hardware issue | Try another card, adjust timing (add delay), or test another RFID module. |
| Card ID: xx xx xx xx | Success | RFID card read successfully. Use this ID for verification logic. |
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 2
#define RELAY_PIN 8
MFRC522 rfid(SS_PIN, RST_PIN);
LiquidCrystal_I2C lcd(0x27, 16, 2);
byte allowedUID[4] = {0x73, 0xB2, 0x63, 0xD};
void setup() {
Serial.begin(9600);
SPI.begin();
rfid.PCD_Init();
lcd.init();
lcd.backlight();
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW);
lcd.setCursor(0, 0);
lcd.print("Welcome !");
}
void loop() {
if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()) {
return;
}
bool isAuthorized = true;
for (byte i = 0; i < rfid.uid.size; i++) {
if (rfid.uid.uidByte[i] != allowedUID[i]) {
isAuthorized = false;
}
}
if (isAuthorized) {
digitalWrite(RELAY_PIN, HIGH);
lcd.setCursor(0, 1);
lcd.print("Welcome home !");
delay(2000);
digitalWrite(RELAY_PIN, LOW);
} else {
lcd.setCursor(0, 1);
lcd.print("Unknown Guy !");
delay(2000);
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Welcome !");
rfid.PICC_HaltA();
}
Run Steps
Run Results
At the beginning, we can see "Welcome!" displayed on the I2C LCD1602.
Swipe Card 1, and the relay's normally open contact will activate. The LCD will display:
After two seconds, it will return to displaying "Welcome!".
Swipe Card 2, and if the ID is incorrect, the relay's normally open contact will deactivate. The LCD will display:
After two seconds, it will return to displaying "Welcome!".
video
This project demonstrates how RFID technology can be used for access control. The combination of an RFID reader, LCD display, and relay allows for a secure and user-friendly door lock system.
Tools → Board and select your Arduino Uno.Tools → Port and select the correct port COM5.Tools → Serial Monitor .(shift+alt+M)