11. Networking and Communication - Amalia, Kathryn, Andrew¶
Overview¶
Our assignment was to send a message between two projects. We chose to communicate between a touch sensor on a XIAO ESP32C3 and a servo motor on a XIAO ESP32S3.
Distribution of Work¶
Member | Role |
---|---|
Kathryn, Amalia, Andrew | Documentation |
Kathryn and Amalia | Breadboard and coding |
Andrew Puky | PCB and Soldering |
Breadboard¶
We first got the two microcontrollers connected on a breadboard.
Coding¶
We first used code to ensure the two microcontrollers could communicate with each other, checked with serial monitor.
For the C3:
void setup() {
Serial.begin(115200); // For debug
Serial1.begin(9600, SERIAL_8N1, 20, 21); // RX=20, TX=21
delay(1000);
}
void loop() {
Serial1.println("Hello from C3!");
Serial.println("Sent message to S3.");
delay(1000);
}
For the S3:
void setup() {
Serial.begin(115200); // For debug
Serial1.begin(9600, SERIAL_8N1, 44, 43); // RX=44, TX=43
}
void loop() {
if (Serial1.available()) {
String msg = Serial1.readStringUntil('\n');
Serial.print("Received: ");
Serial.println(msg);
}
}
This is a video of what it resulted in. In serial monitor, we were able to see the change in output when the sensor sensed touch, but the servo motor did not move. However, it was a step in the right direction.
Amalia suggested we use code that worked for the servo for our individual assignments and have Chat edit it be compatible with UART for networking. Here is a public link to the ChatGPT conversations for troubleshooting the coding.
This approach worked and this was the final code we used for the sender side, the touch sensor on the ESP32C3:
#define TOUCH_PIN D4 // GPIO5 for touch sensor
void setup() {
Serial.begin(115200);
Serial1.begin(9600, SERIAL_8N1, 20, 21); // TX=21, RX=20 — adjust as needed
pinMode(TOUCH_PIN, INPUT);
}
void loop() {
int touchValue = digitalRead(TOUCH_PIN); // Read sensor value
Serial.println(touchValue); // Debug print
if (touchValue == HIGH) {
Serial1.println("TOUCH"); // Send command to other ESP
Serial.println("Sent: TOUCH");
delay(500); // Debounce
}
delay(100); // Small delay to reduce spam
}
This was the final code we used for the receiver side, the servo motor on the ESP32S3:
#include <ESP32Servo.h>
#define SERVO_PIN D5 // Set this to your actual servo control pin
Servo myServo;
bool triggered = false;
unsigned long lastTriggerTime = 0;
void setup() {
Serial.begin(115200);
Serial1.begin(9600, SERIAL_8N1, 44, 43); // RX=44, TX=43
myServo.attach(SERVO_PIN);
myServo.write(0); // Initial position
}
void loop() {
if (Serial1.available()) {
String msg = Serial1.readStringUntil('\n');
msg.trim(); // Remove any trailing newline or whitespace
if (msg == "TOUCH") {
Serial.println("Touch received — spinning servo");
myServo.write(90); // Rotate servo
triggered = true;
lastTriggerTime = millis();
}
}
// Reset servo after 1 second
if (triggered && millis() - lastTriggerTime > 1000) {
myServo.write(0); // Return to start
triggered = false;
}
}
This is a video of these two pieces of code working to get the two microcontrollers in communication on the breadboard.
PCB¶
The task to build the PCB was a relativley simple one, I tried to make it as small as possible, in order to conseve the ammount of copper boards our lab was burning through due to machine week.
Design¶
Like all good boards, it began with a skematic. Using the Fab libriary for KiCad I added the footprints for the Esp32-C3 and Esp32-S3. I also added two 3-pin connectors for the servo and touch sensor. Noah Smith taught me how to use sheet pins in my design, which greatly sped up the wiring process.
After I had my skematic complete I moved it over to the PCB Editor. I then arranged my components into a satisfactory order, and used a plugin I found called “Freerouting”. This made the PCB editing experience super fast, as after it routed everything, all I had to do was go back and widen the trace width and clean up and janky bits.
This is the what the Final design looks like.
Testing the PCB¶
Here is our working PCB: