Skip to content

14. Networking and Communication

This week’s documentation is divided into:
1. Xiao RP2040 to Arduino UNO using I2C
2. NodeMCU (ESP8266) to NodeMCU using wireless connection
3. NodeMCU to Xiao ESP32 S3 using wireless connection

Xiao RP2040 to Arduino UNO (I2C)

I started out by doing some research online about I2C. And referring to Jesal Sir’s documentation.

Based on his documentation and in-class explanation, I realized two things: One, RP2040 to RP2040 does not work due to a bug in the chip itself, and Two, When connecting RP2040 to anything else, to be cautious of the voltage difference. Quoting Jesal Sir’s documentation:

“One has to be careful here, since the XIAO operates at 3.3 V while the Uno operates at 5 V. The I2C connection operates at whatever the Master is operating at. So the XIAO being the Primary or Master would work for the Uno but not the other way !”

I began by using the xiao RP2040 as the primary and the Arduino UNO as the secondary.

I made the following connections according to our instructors’ recommendation:

I2C

  • SDA (D4) [XIAO] to SDA (A4) [Uno]
  • SCL (D5) [XIAO] to SCL (A5) [Uno]
  • common GND

Built-in LED:

First, I made the built-in LED of the UNO blink along with the RGB built-in of the RP2040. The RP2040 acted as the primary, where it began the transmission and sent across a variable ‘x’ to the UNO. In the loop, ‘x’ would increase in value and subsequently the built-in RGB would light up. Using the same logic, I played around with the code, adding LEDs to the UNO in subsequent iterations (Read below). The RP primary code remains the same throughout.

The RP2040 I have used is part of the Seeed Studio Xiao family. The PCB for the same is the one I had designed and milled in my Week 8: Electronics Design

The Primary code here is taken from Jesal Sir’s documentation along with the overall logic of my explorations.

RP2040 Code
UNO code

Single LED:

Next, I connected a single LED to the Arduino and coded it such that this LED blinks when the transimssion is received. Here, I tried changing the value of the delay() to try to match the UNO LED to the RP Led.

RP2040 Code
UNO code

Double LED:

I did the same with two LEDs.

RP2040 Code
UNO code

RGB syncing:

Next, I tried replicating what Jesal Sir had tried- he connected an external RGB to the UNO and tried to sync that to the built-in RGB on the Xiao RP2040.
However, he hadn’t able to achieve this entirely. So, I decided to try to crack this one myself.

I started by using his code:

Jesal’s UNO code

Here, when the variable ‘x’ is received, it is read and depending on the value of ‘x’, an LED lights up.

I changed the code to give out the same colors as the Xiao RGB for each of the values of ‘x’ from 0 to 5.

The ‘if’ block:

if (x == 0) {
//    digitalWrite(ledPin3, HIGH);
//    digitalWrite(LED_BUILTIN, HIGH);
//    delay(200);
//    digitalWrite(ledPin3, LOW);
//    digitalWrite(LED_BUILTIN, LOW);
////    delay(400);
}

As you can see in the code, the LED outputs a HIGH voltage when the If statement is met, then after a delay of 200 milliseconds, the same LED goes LOW.
There is a delay after the LED goes LOW of 400 milliseconds. This means that in the delay between the LOW of LED1 and the HIGH of LED2, the four-pin LED does not emit light.

I modified this ‘if’ block to be such that the LED emits light in a continuous manner such as: RED – GREEN – BLUE .. and so on

Instead of:
RED – OFF – GREEN – LOW ..and so on

New ‘if’ block:

if (x == 0) {
    digitalWrite(ledB, LOW);
    digitalWrite(ledR, HIGH);
    digitalWrite(LED_BUILTIN, HIGH);
    delay(200);
    digitalWrite(LED_BUILTIN, LOW);
}

if (x == 1) {
    digitalWrite(ledR, LOW);
    digitalWrite(ledG, HIGH);
    digitalWrite(LED_BUILTIN, HIGH);
    delay(200);
    digitalWrite(LED_BUILTIN, LOW);
  }

Here, when the if condition is met, the first thing that happens is that the previous LED goes OFF, then the new LED turns ON. And this repeats in each of the six IF blocks.

This way, the RGB LED transitions smoothly from one color to another:

Next, I played around with the delay to sync them:

Here, as you can see, at the start, the LEDs are in sync. However, once a few seconds pass, the LEDs go out of sync. A lag develops in the RGB connected to the UNO.
My best guess is that this happens because of a tiny lag in the I2C protocol which may be compounding in each successive loop thereby resulting in the sync breaking off.

The final sync attempt, which turned out pretty well:

RP2040 Code
UNO code

Guide to doing I2C between two UNOs:[Useful]
https://www.instructables.com/I2C-between-Arduinos/

A discussion on why I2C does not work with Xiao RP2040[Useful]
https://forum.seeedstudio.com/t/xiao-rp2040-i2c/261907/5

A report on understanding I2c Bus by Texas Instruments:
https://www.ti.com/lit/an/slva704/slva704.pdf?ts=1714442071803#:~:text=To%20write%20on%20the%20I2C,it%20wishes%20to%20write%20to.

A Basic guide on I2C application by Texas Instruments:
https://www.ti.com/lit/an/sbaa565/sbaa565.pdf?ts=1712979034869&ref_url=https%253A%252F%252Fwww.google.com%252F#:~:text=I2C%20is%20a%20two%2Dwire,and%20receive%20commands%20and%20data.

Basic coding of input/output devices using the Xiao RP2040:
https://how2electronics.com/getting-started-with-seeed-xiao-rp2040-with-projects/

Connecting the NodeMCU to UNO:[Useful]
https://www.electronicwings.com/nodemcu/nodemcu-i2c-with-arduino-ide

NodeMCU to NodeMCU

As a part of our college curriculum, we had the subject “IoT” or Internet of Things. Here we learnt about wireless communication using the NodeMCU.

My gratitude to the professors at college who showed us the steps to executing this and guided us- Chinmay Sir and Jesal Sir. You can find the pdf that was shared with us in class HERE.

The NodeMCU uses an ESP8266 microcontroller which can communicate wirelessly over wi-fi or bluetooth. In this week, we communicate between the nodeMCUs using a wireless communication protocol called ESPNow, which runs on top of WiFi.
Some useful links about ESPNow shared by my global evaluator:

Link 1 and Link 2

Setting up the IDE

In order to program the NodeMCU, we need to first install the necessary drivers. This can be found on this link: https://www.silabs.com/developers/usb-to-uart-bridge-vcp-drivers?tab=downloads

I installed the Universal windows driver and ran the program on my computer.

Next, I opened up Arduino IDE and went to “Preferences” under File.

There, under “Additional Boards Manager URL” I added the following link:

https://arduino.esp8266.com/stable/package_esp8266com_index.json

And finally, I went to the Board Manager under Tools and searched for ESP8266 by Community and installed it.

Quick note here: I stuck to choosing the “Generic ESP8266” board as the “NodeMCU 1.0” board was giving a compilation error – Access denied.

First Step to any Wireless communication: Mac Address

To start with any wireless communication, we need to first find the Mac Address of the microcontroller we are using. Mac Address has nothing to do with the MacBook or Apple and actually stands for “Media Access Control Address”. It is the unique address/identifier of a controller that can communicate wirelessly.

You simply run the Mac Address code after connecting your microcontroller and once uploaded, hit the “Reset” button on your microcontroller and look at your Serial Monitor. You will find the unique 6-figure address of your controller.

1st Node:

2nd Node:

Xiao ESP32:

Make sure to note down the mac address. You can stick a small piece of masking tape on your controller itself with the numbers written down on it.

Mac Address Code

Programming:

I first tried running the basic blink code on my NodeMCU to get the built-in led to blink:

NodeMCU blink Code

Uni-direcitonal LED/Button

Next, I ran the Unidirectional LED-Button code that was shared with us in class.

The logic for this goes as such:

1st Node – Connect Button

2nd Node – Connect LED

Now, choose one Node as your Main and the other as the Secondary. In the code for the main, you should add the Mac Address of the secondary. This is because the Main Node is the one that starts the communication and sends the signal, where the Secondary one just performs a specific function when it receives a signal (in this case it lights up the LED).

The Uni-Directional Node to Node in action:

Serial monitor:

MAIN Node Code
Secondary Node Code

ESP32 to NodeMCU - Fail

Here, I tried using a Xiao ESP32 S3 and connecting it to the NodeMCU ESP8266. I wanted to try running the same code that I tried in the NodeMCU as both are capable of connecting wirelessly.

I started by installing the ESP32 library on Arduino UNO.

The ESP32 shows a lot of error messages when you upload code onto it, such as this:

Most of these are compilation errors or exit status.
Hence, to prevent this from happening, we need to get the ESP32 in bootloader mode.

I looked it up online and on Adrians Quentorres documentation and found that you can enter bootloader mode by doing the following:

1. Press and hold the BOOT button on the XIAO ESP32S3 without releasing it.  

2. Keep the BOOT button pressed and then connect to the computer via the data cable. Release the BOOT button after connecting to the computer.  

3. Upload the Blink program to check the operation of the XIAO ESP32S3.  

The above steps are from https://wiki.seeedstudio.com/xiao_esp32s3_getting_started/

Adrian’s QT doc contains the bootlaoder mode for the Xiao RP2040.

Next, I ran the Macaddress code on the ESP32 to receive its mac address

I took this Mac address and put it in the code for the NodeMCU, and then took the MAc address of the Node I was using and put in the ESP32 code.

Finally, I uploaded the code and it worked without giving any compiling error/exit status

However, the code did not work as intended!

I figured that this was maybe because I was using the same code lines as the ESP8266 and that may not be compatible. I realized that I needed to do more research into the ESP32 and therefore set it aside.

ESP 32 Code
ESP 8266 Code

Group Assignment

This week, we tried to communicate between two projects as a group.

We tried wireless communication using ESP Now between our two boards:
Siddharth’s Xiao ESP32 S3 attached to his Quentorres and Tejas’ ESP32 CAM module attached to his Final project PCB.

First, we start by individually finding the Mac Address of each of the two microcontrollers.

For that, we asked chatGPT to give us a suitable code.

Mac Address Code

ESP32 CAM MAC address:

Xiao ESP32 S3 MAC address:

Next, we asked chatGPT to give us a code which would send a message between two spicific ESPs using ESPNow.

The Xiao ESP32 S3 QT acted as the Sender while the ESP CAM board acted as the Receiver.

Then, we uploaded each of the codes on their respective boards. And we gave the correct Mac Address to each board’s code. For example, the CAM boards’ Receiver code had the Mac address of the ESP32 QT board and vice-versa. This ensured that they would connect wirelessly.

Quick note here: Be sure to hold the RESET button on the ESPs while inserting the cable connector in order to get it into the bootloader mode. Else, you will get an error message or compilation will become endless as it will not be able to detect the board.

Finally, we ran the code!

The code in action! Here, you can see that the wireless communication via ESPNow was successful and we were able to send a message between our two boards!

Xiao ESP32 S3 Sender Code

ESP32 CAM module Receiver Code