Week 13. 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
I2C communication between Xiao Rp2040 and Arduino UNO¶
For I2C communication we have to connect SDA and SCL pin of respective boards to SDA and SCL pins in of the receiver board.
I2C(Inter-Integrated Circuit)
- Synchronous communication protocol
- Uses SDA(Data Line) and SCL(Clock Line)
- Data can be transmitted adn Received but not simultaneouslyon same data line.
For this week, we tried sending message from Arduino to Xiao RP2040 and observe the message on Serial monitor.
According to the pin out diagrams below the connection were made. Arduino Uno SDA pin 18 and SCL pin 19 where connected to XIAO SDA pin 4 and SCL pin 5 respectively using jumpers. Ground on both the boards were also connected together.
image from this source
image source
Here are codes we used.
Arduino code which works as a transmitter, which initiates the communiation with Xiao.
#include <Wire.h>
const int ledPin = 26; // 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() {
// No need for a loop function in this case
}
void receiveEvent(int bytesReceived) {
// Read the received data
String data = "";
while (Wire.available()) {
data += (char)Wire.read();
}
// Check the received data and perform the desired action
if (data == "hello") {
digitalWrite(ledPin, HIGH); // Turn on the LED
Serial.println("Received: " + data); // Print the received data to the serial monitor
delay(1000); // Wait for 1 second
digitalWrite(ledPin, LOW); // Turn off the LED
}
}
XIAO code where we can receive the message sent by Arduino can be viewed in serial monitor.
#include <Wire.h>
const int slaveAddress = 0x08; // Set the I2C address for the slave device
void setup() {
Wire.begin(); // Initialize I2C communication as master
Serial.begin(115200); // Initialize serial communication for debugging
}
void loop() {
Wire.beginTransmission(slaveAddress); // Start I2C transmission to the slave device
Wire.write("hello"); // Send data over I2C
Wire.endTransmission(); // End I2C transmission
Serial.println("Sent: hello I am sangays xiao"); // Print a message to the serial monitor
delay(1000); // Wait for 1 second
}
Each board was connected to different computers.