13. Networking and communications
On this week we weill dive into the world of communications between boards, there are many communication protocols on boards, the must famous once probably are SPI, I2C and UART but some boards like the ESP32 can communicate with integrated Wi-Fi, Bluetooth and it's own protocol ESP-now, other boards can also use Wi-Fi and bluetooth through UART but need external modules. For this I will focus on the first 3 protocols. Check Networking and communications Guide Ibero Puebla Fablab for support material.
Feature | SPI (Serial Peripheral Interface) | I2C (Inter-Integrated Circuit) | UART (Universal Asynchronous Receiver/Transmitter) |
---|---|---|---|
Communication Type | Full-duplex | Half-duplex | Full-duplex |
Number of Wires | Minimum 4 (including SS) | 2 | 2 |
Master-Slave Relationship | Supports multiple slaves | Supports multiple slaves | Point-to-point or multi-drop |
Speed | High | Medium | Low to Medium |
Addressing | Not standardized, set by hardware or software | 7-bit or 10-bit addressing | Point-to-point addressing |
Applications | High-speed communication between microcontrollers and peripheral devices | Sensors, EEPROMs, and other low to medium-speed devices | Serial communication over longer distances |
I decided to use the I2C communication since it has a good speed, and only needs two cables for each device. I will be using 3 boards, an arduino uno as a drone, and two XIAO-ESP32-C3 one as hive and the other as drone.
Arduino uno | XIAO-ESP32-C3 | XIAO-ESP32-C3 |
---|---|---|
Drone 1 | Hive | Drone 2 |
Has two push buttons, sends their states (high/low) to the hive through I2C. | Recieves button states from drone 1, sends instructions to turn on/off LEDs | Recieves data from the hive, turns off LEDs when buttons on drone 1 are pushed. |
In summary, the Arduino drone has two buttons and the XIAO drone has two LEDs (the code is made for 3 so you can plug in more), and the hive sends info from one drone to the other to turn off the LEDs (They stay on otherwise).
We must connect SDA and SCL with the corresponding ports (not inverted like RX/TX), and all of the boards have to share GND if they don't it wont work.
This is the code for the hive. Click here to download.
This is the code for the drone that has buttons. Click here to download.
This is the code for the drone that has LEDs. Click here to download.
I also designed and milled this small PCB for two buttons. Here you can download the traces and the outline.
Afterwards we can move on to the connections, here's an image that can help us understand the connections IRL.
Then everything is connected and it should look like this.
Finally this is how it works, you can notice a little delay, that might because of the buttons, not necessarilly the communication protocol because it's very fast. It's a simple aplication but assigning adresses for I2C devices and communicating them is very important so we can use multiple microcontrollers on a same project, this principles are also followed on other communications. I also used I2C for communication with sensors on my inputs page, check it out to learn a little more on this.