Skip to content

13. Networking and communication

Introduction!

In this project, we connect a Seeed Xiao RP2040 with an Arduino UNO to synchronize an LED on the Arduino UNO with an inbuilt Neopixel on the RP2040. This setup demonstrates basic networking and communication between two microcontrollers, showcasing the principles of data exchange and synchronization.

Components and Tools

Hardware:

  • Seeed Xiao RP2040 microcontroller with inbuilt Neopixel

  • Arduino UNO microcontroller

  • Standard LED

  • Resistors

  • Jumper wires

Wiring the Components

Arduino UNO to Standard LED

Anode (Long Leg): Connect to a digital pin (12) on the Arduino UNO through a current-limiting resistor.

Cathode (Short Leg): Connect to the GND pin on the Arduino UNO.

I2C Communication:

SDA: Connect the SDA pin of the RP2040(D4) to the SDA pin of the Arduino UNO(A4).

SCL: Connect the SCL pin of the RP2040 (D5) to the SCL pin of the Arduino UNO(A5).

GND: Connect the GND pins of both microcontrollers.

Code for Arduino UNO (Secondary)

// Include the required Wire library for I2C

#include <Wire.h>

int x;
int led = 12;

void setup() {

  pinMode (LED_BUILTIN, OUTPUT);
  pinMode(led, OUTPUT);
  Serial.begin(9600);

  Wire.begin(9);
  Wire.onReceive(receiveEvent);
}
void receiveEvent(int bytes) {
  x = Wire.read();
  Serial.println("recieved");
}
void loop() {
  Serial.println(x);

    digitalWrite(led, HIGH);
    digitalWrite(LED_BUILTIN, HIGH);
    delay(200);
    digitalWrite(LED_BUILTIN, LOW);
    digitalWrite(led, LOW);
//    delay(400);

}

Code for the Rp2040

// I took this code from Jesal Sirs documentation

#include <Wire.h>
#include <Adafruit_NeoPixel.h>


// Include the required Wire library for I2C<br>
#define numpixels 1
#define pixelpower 11
#define pixelpin 12

long int setMillis;

Adafruit_NeoPixel pixel(numpixels, pixelpin, NEO_GRB + NEO_KHZ800);
int x = 0;

void setup() {

  Wire.begin();
  Serial.begin();
  pixel.begin();
  pinMode(pixelpower, OUTPUT);
  digitalWrite(pixelpower, HIGH);
  setMillis = millis();
}
void loop() {
  Wire.beginTransmission(9); 
  Wire.write(x);             
  Wire.endTransmission(); 

//  long int setMillis = millis();
  if (x == 0)
  {
    pixel.setPixelColor(0, pixel.Color(255, 0, 0));
    pixel.show();
       delay(200);
  }
  if (x == 1)
  {
    pixel.setPixelColor(0, pixel.Color(0, 255, 0));
    pixel.show();
       delay(200);
  }
  if (x == 2)
  {
    pixel.setPixelColor(0, pixel.Color(0, 0, 255));
    pixel.show();
       delay(200);
  }
  if (x == 3)
  {
    pixel.setPixelColor(0, pixel.Color(255, 255, 0));
    pixel.show();
       delay(200);
  }
  if (x == 4)
  {
    pixel.setPixelColor(0, pixel.Color(0, 255, 255));
    pixel.show();
       delay(200);
  }
  if (x == 5)
  {
    pixel.setPixelColor(0, pixel.Color(255, 0, 255));
    pixel.show();
       delay(200);
  }

  long int nowMillis = millis();

  // if (nowMillis - setMillis > 200)
  // {
    x++; // Increment x
    // setMillis = millis();
  // }
 if(x>5)
  {
    x = 0;
  } // `reset x once it gets 6

 delay(200);


}

Steps Involved

Hardware Setup:

  • Connect the standard LED to the Arduino UNO as described in the wiring section.

  • Ensure all connections are secure and correct.

Programming the Microcontrollers:

  • Write the Arduino code to read the LED state and send it over I2C from the Arduino UNO.

  • Write the Arduino code to receive the LED state and control the inbuilt Neopixel on the RP2040.

  • Upload the respective codes to the Seeed Xiao RP2040 and the Arduino UNO using the Arduino IDE.

Establishing Communication:

  • The Arduino UNO (slave) sends the LED state (‘1’ for on, ‘0’ for off) over the I2C bus.

  • The Seeed Xiao RP2040 (master) requests the LED state from the Arduino UNO and uses this information to synchronize the Neopixel.

Running the Application:

  • Power both microcontrollers and observe the synchronization between the LED on the Arduino UNO and the Neopixel on the RP2040.

  • The Neopixel should turn green when the LED on the Arduino UNO is on and turn off when the LED is off.

Debugging:

Use Serial.print() statements to print debugging information and ensure proper data flow and synchronization. Check connections and code logic if synchronization does not work as expected.

Conclusion

This project demonstrates the basics of networking and communication between two microcontrollers using I2C. By synchronizing an LED on the Arduino UNO with an inbuilt Neopixel on the Seeed Xiao RP2040, we showcase how simple data exchange and synchronization can be achieved. This project is a practical example of how microcontrollers can communicate and work together to perform coordinated tasks, making it a valuable learning experience in the realm of networking and embedded systems.

Hero shots

Group work

This weeks group assignment is on Siddharth Agrawal’s Websiste Siddharth’s Webpage

Code files

Code For RP2040

Code for Arduino UNO