13. Embedded Networking and Communications

Group assignment:

Send a message between two projects

Linked to the group assignment page:

Individual assignment:

design, build and connect wired or wireless node(s) with network or bus addresses and a local interface

For this week, I decided to communicate an Arduino Uno and two Xiao RP2040s. I designed one of them during week 10, and we received the other one in week 4.

Then Iuploaded the code to the Xiao RP2040 and to Arduino Uno.

Programming process XIAO RP2040

Including the Library and defining the LED Pins:
The Wire.h library is included to enable I2C communication, the pins connected to the transmission (TX) and reception (RX) LEDs are defined.

...

Initial Setup in 'setup()' and Main Loop 'loop()'
In the setup() function:
The LED pins are configured as outputs using pinMode. The device is initialized as an I2C slave with address 8 using Wire.begin(8). If multiple slaves are used, each must have a unique I2C address. It is specified that the receiveEvent function will be called whenever the slave receives data from the master using Wire.onReceive(receiveEvent).
In the 'loop()':
The loop() function is empty because I2C communication and LED control logic are handled in the receiveEvent function.

...

Handling Reception Events in 'receiveEvent()'

In the receiveEvent(int howMany) function:
Wire.available() checks if there is data available to read. Wire.read() reads one byte of data. Depending on the character received: If the character is 'T', the state of the LED connected to the LED_TX_PIN is toggled. If the character is 'R', the state of the LED connected to the LED_RX_PIN is toggled.

...

CODE

...

Programming process ARDUINO UNO

Including the Wire.h library and Setting up the I2C master in setup():

This library is necessary for I2C communication.
Wire.begin() initializes the Arduino Uno as the I2C master. Serial.begin(115200) starts serial communication to output debug messages at a baud rate of 115200. delay(1000) provides a second of delay for complete initialization before proceeding.

...

Main loop for sending commands:

if (Serial.available()) checks if there are available data on the serial port. char command = Serial.read(); reads a character from the serial port, which represents the command to send to the slave. sendCommandToSlave(8, command); calls the sendCommandToSlave function to send the command to the RP2040 slave with address 8.

...

Function to send the command to the slave:

Wire.beginTransmission(address) starts the transmission to the slave with the specified address (8 in this case). Wire.write(command) sends the command to the slave through the I2C bus. Wire.endTransmission() finishes the transmission and releases the I2C bus. delay(100) introduces a short pause to ensure the transmission completes successfully.

...

CODE

...

Then Iuploaded the code to the Xiao RP2040 and to Arduino Uno and tested it.

...

When you press the 'T' key, the TX LED lights up as shown in the image on the left, and when you press the 'R' key, the RX LED lights up as shown in the photo on the right.

Files:

Get in touch

Follow