- send a message between two projects
- design, build, and connect wired or wireless node(s) with network or bus addresses and local input &/or output device(s)
Here is my organization to finish on time:
LINK TO THE ASSIGNMENT WEEKI will create two boards
The design of my two boards is based on the development and improvement of my two previous boards.
The two boards created have generic connections at 3 points, I have added an LED and a switch to better complement the design of the same, so that they can be controlled by that means.
The initial design
To develop the design, I based one of the boards on the Xiao ESP32-C3 and the other on the RP2040, so that both have UART communication through the TX and RX pins, but with a wired Jumper connection.
For this, I added to my previous work, a switch with its 4990ohm resistor and the connection of this to one of the pins, leaving the others free to be used with different Outputs.
BOM and Schematic for Maryo-Esp
Maryo-Esp |
Where to buy? | Amount |
Seeed Studio XIAO RP2040 | Seeed Studio | 1 |
1kΩ resistor | Digikey | 4 |
LED | Digikey | 4 |
Male 4 row vertical header | Digikey | 1 |
Maryo-RP |
Where to buy? | Amount |
Seeed Studio XIAO RP2040 | Seeed Studio | 1 |
1kΩ resistor | Digikey | 4 |
LED | Digikey | 4 |
Male 4 row vertical header | Digikey | 1 |
Soldering and programming tests with Output
For this case, I first arranged all my components to start soldering, such as: soldering iron, resistors, LEDs, Header connectors, Xiao and Flux.
Plate milling
To get here, I passed my PNGs to the MODS program, I attach a photo of my traces to export and send to milling.
From here I transferred the PNGs to MODS to get the traces, SML files and some background settings to achieve the cut of my board.
Here is the SML file and some videos of the process! everything went correctly, remember that there are two plates that we milled and soldered.
Here is the root of the total configuration
I leave you the download link of my SML files so you can mill it if you wish!
From here the milling process:
Here is the result of the milling of my boards
Component soldering
To make the soldering of the board, I used the soldering iron and tin, little by little I was accommodating the components
- I used the soldering iron at 310°C and Flux on the board and the component.
Something that helped me to solder faster my two boards was to prepare the components and solder at the same time, the first resistor of board 1 and the same on board 2, so that I was advancing in an accelerated way and finishing on time.
Codification
Something important to consider is the installation of the Xiao ESP32-C3 library from the ARDUINO.IDE library, consider that it is also important to add in preferences the link of the RAW:
https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_dev_index.json
Blink
Starting with the code now, to verify that my board works, I used the basic ARDUINO.IDE code to get my LED on PIN D3 of the Xiao ESP32-C3 to light up, here the code and the result.
/* Blink Turns an LED on for one second, then off for one second, repeatedly. Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to the correct LED pin independent of which board is used. If you want to know what pin the on-board LED is connected to on your Arduino model, check the Technical Specs of your board at: https://www.arduino.cc/en/Main/Products modified 8 May 2014 by Scott Fitzgerald modified 2 Sep 2016 by Arturo Guadalupi modified 8 Sep 2016 by Colby Newman This example code is in the public domain. https://www.arduino.cc/en/Tutorial/BuiltInExamples/Blink */ // the setup function runs once when you press reset or power the board void setup() { // initialize digital pin LED_BUILTIN as an output. pinMode(D3, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(D3, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(D3, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second } /* Blink Turns an LED on for one second, then off for one second, repeatedly. Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to the correct LED pin independent of which board is used. If you want to know what pin the on-board LED is connected to on your Arduino model, check the Technical Specs of your board at: https://www.arduino.cc/en/Main/Products modified 8 May 2014 by Scott Fitzgerald modified 2 Sep 2016 by Arturo Guadalupi modified 8 Sep 2016 by Colby Newman This example code is in the public domain. https://www.arduino.cc/en/Tutorial/BuiltInExamples/Blink */ // the setup function runs once when you press reset or power the board void setup() { // initialize digital pin LED_BUILTIN as an output. pinMode(D3, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(D3, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(D3, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second }
The test with the board and the ESP32
The same test but with the XIAO RP2040
Ultrasonic Sensor
Here with the test for the Ultrasonic sensor, I attach the code, previously used in Machine Week Assignment 10, explaining its origin.
#define TRIG_PIN 10 // Pin TRIG conectado al pin D10 en la Xiao ESP32-C3 #define ECHO_PIN 19 // Pin ECHO conectado al pin D19 en la Xiao ESP32-C3 void setup() { Serial.begin(9600); // Iniciar comunicación serial para mostrar resultados en el monitor serial pinMode(TRIG_PIN, OUTPUT); // Configurar el pin TRIG como salida pinMode(ECHO_PIN, INPUT); // Configurar el pin ECHO como entrada } void loop() { long duration; float distance_cm; // Generar un pulso corto en el pin TRIG para activar el sensor ultrasónico digitalWrite(TRIG_PIN, LOW); delayMicroseconds(2); digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10); digitalWrite(TRIG_PIN, LOW); // Medir el tiempo que tarda en llegar el eco (en microsegundos) duration = pulseIn(ECHO_PIN, HIGH); // Calcular la distancia en centímetros utilizando la fórmula de conversión distance_cm = duration * 0.034 / 2; // La velocidad del sonido es 340 m/s (0.034 cm/microsegundo) // Mostrar la distancia medida en el monitor serial Serial.print("Distancia: "); Serial.print(distance_cm); Serial.println(" cm"); delay(1000); // Esperar un segundo antes de realizar la próxima lectura }
Here is a video showing how it works!
Neopixel
Here with the Neopixel the intention was to test the code, first time that I made the coding and it worked, I helped myself with the documentation of Adrian Torres.
#include#define PIN_NEOPIXEL 3 // Pin D3 en la Xiao ESP32-C3 #define NUM_PIXELS 8 // Número de LEDs en tu tira Neopixel Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_PIXELS, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800); void setup() { strip.begin(); // Inicializar la tira Neopixel strip.show(); // Apagar todos los LEDs al inicio } void loop() { // Cambiar el color de los LEDs gradualmente for (int i = 0; i < NUM_PIXELS; i++) { // Establecer el color de cada LED (rojo, verde, azul) strip.setPixelColor(i, 255, 0, 0); // Rojo strip.show(); // Mostrar el color en el LED delay(50); // Pequeña pausa } delay(1000); // Esperar un segundo for (int i = 0; i < NUM_PIXELS; i++) { strip.setPixelColor(i, 0, 255, 0); // Verde strip.show(); delay(50); } delay(1000); // Esperar un segundo for (int i = 0; i < NUM_PIXELS; i++) { strip.setPixelColor(i, 0, 0, 255); // Azul strip.show(); delay(50); } delay(1000); // Esperar un segundo }
Here is the result!
Servo Motor
For this other case I made the coding of a Servo motor with my board with the ESPC3-C3.
const int servoPin = D2; // Pin D3 en la Xiao ESP32C3 unsigned long tiempoInicio; // Variable para guardar el tiempo de inicio del pulso int angulo = 0; // Ángulo inicial del servo void setup() { pinMode(servoPin, OUTPUT); // Configurar el pin del servo como salida } void loop() { // Mover el servo de 0 grados a 180 grados y viceversa for (angulo = 0; angulo <= 180; angulo++) { moverServo(angulo); // Llamar a la función para mover el servo al ángulo deseado delay(15); // Pequeña pausa para dar tiempo al servo a alcanzar la posición } delay(1000); // Esperar un segundo en la posición final for (angulo = 180; angulo >= 0; angulo--) { moverServo(angulo); // Llamar a la función para mover el servo al ángulo deseado delay(15); // Pequeña pausa para dar tiempo al servo a alcanzar la posición } delay(1000); // Esperar un segundo en la posición inicial } void moverServo(int angulo) { // Calcular el ancho del pulso PWM necesario para el ángulo dado (entre 0 y 180 grados) int anchoPulso = map(angulo, 0, 180, 500, 2400); // Mapear ángulo a duración del pulso en microsegundos // Generar el pulso de control PWM durante el tiempo requerido para alcanzar el ángulo deseado digitalWrite(servoPin, HIGH); // Establecer el pin en alto delayMicroseconds(anchoPulso); // Mantener el pin en alto durante el ancho del pulso digitalWrite(servoPin, LOW); // Establecer el pin en bajo }
Here is how it works!
Communication between my boards
What do I want to connect?
What I want to connect are my two boards using the UART connection through the TX and RX pins between an RP2040 and an ESP32-C3, so I leave again the image of the two boards and their more detailed connections.
Does it work?
The intention is to use the ultrasonic sensor to run the motor, when the first one is less than 10cm away, it will automatically turn on the servo.
Here I leave a video of its operation and the link to the ARDUINO.IDE file for programming.
In the other example I use the same ultrasonic sensor to turn on the Neopixel, in the same way at a distance less than 10cm.
Here I leave a video of its operation and the link to the ARDUINO.IDE file for programming.
What we wanted to do was to connect two different boards through the Wifi network, so we installed the MQTTX program.
https://mqttx.app/downloads
So that we can connect, here I make a sample of the data created between two users, in this case with my partner Hans.
Does it work?
It works first placing the data following this image:
Inside the Serial we will place “a” to send a signal to the Xiao of my partner Hans, previously he has done the programming of his Xiao following this code:
#include#include // Replace the next variables with your SSID/Password combination const char* ssid = "HBP WIRELES"; const char* password = "923279923"; // Add your MQTT Broker IP address, example: //const char* mqtt_server = "192.168.1.144"; const char* mqtt_server = "broker.emqx.io"; WiFiClient espClient; PubSubClient client(espClient); long lastMsg = 0; char msg[50]; int value = 0; float numero = 0; // LED Pin const int ledPin = D3; void setup() { Serial.begin(115200); setup_wifi(); client.setServer(mqtt_server, 1883); client.setCallback(callback); pinMode(ledPin,OUTPUT); } void setup_wifi() { delay(10); // We start by connecting to a WiFi network Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } void callback(char* topic, byte* message, unsigned int length) { Serial.print("Message arrived on topic: "); Serial.print(topic); Serial.print(". Message: "); String messageTopic; for (int i = 0; i < length; i++) { Serial.print((char)message[i]); messageTopic += (char)message[i]; } Serial.println(); if (String(topic) == "hans/led") { Serial.print("Changing output to "); if(messageTopic == "a"){ Serial.println("on"); digitalWrite(ledPin, HIGH); } else if(messageTopic == "b"){ Serial.println("off"); digitalWrite(ledPin, LOW); } } } void reconnect() { // Loop until we're reconnected while (!client.connected()) { Serial.print("Attempting MQTT connection..."); // Attempt to connect if (client.connect("hdmsclient1025")) { Serial.println("connected"); // Subscribe client.subscribe("hans/led"); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); // Wait 5 seconds before retrying delay(5000); } } } void loop() { if (!client.connected()) { reconnect(); } client.loop(); }
From here you have to configure the name of your NETWORK from row 8.
Here is a video of how we were able to send a signal via Wifi between two boards and turn on the LED!