Wearables
Understanding¶
Wearables are something you wear with a specific purpose, most of the times these include electronics.
They can be jewelry, accessories, medical devices, clothing or elements of clothing. 1
Some analogue cases can be:
- Prothesis
- Security equipment
- Rehabilitation equipment
References, research & inspiration¶
Wearables can also be used for performance, an area in which people get really creative and do stuff that for me was very interesting.
This was some of my inspiration for this week, but considering how much time I had I decided to experiment with easier things and also inspired myself by Mari Cruz wearables assigments which consisted in a screen where a heart beated at the time as the pulse sensor detected it.
Experimenting¶
Exploration #1¶
The first exploration I had with this topic was with an IMU sensor and a servomotor, what I intended to do was making the servomotor move in the same angle as the sensor.
Materials
- Adafruit ICM-20948 9-DoF Inertial Measurement Unit
- Xiao RP2040
- Micro Servo tower pro sg90
- Cables
- Breadboard
- Alligator clip
Resources¶
XIAO RP2040¶
Breadboard¶
- I first connected the XIAO to the breadboard
- Then connected the sensor to the XIAO like this:
```text ICM-20948 -> XIAO RP2040 VIN / VCC -> 3V3 GND -> GND SDA -> D4 (GPIO6) SCL -> D5 (GPIO7)
- When adding the Micro servo, I got a bit confused with many cables, so I decided to connect everything on the breadboard.

- I connected the micro servo following this:
```text
PWM (orange cable) -> D0
+ (red cable) -> 5V
GND (black cable) -> GND
- Then entered Arduino software, selected the XIAO RP2040 board, downloaded the Adafruit ICM20X library with it's dependancies.
The code I used was:
#include <Adafruit_ICM20948.h>
#include <Wire.h>
#include <Servo.h>
Adafruit_ICM20948 icm;
Servo servo;
// ---------- AJUSTES ----------
const int PIN_SERVO = D0;
const int MODO = 0; // 0, 1 o 2: cambia si el servo no responde al inclinar
const bool INVERTIR = false; // true si gira al revés
const float SUAVIZADO = 0.8; // 0.7 = rápido, 0.95 = muy suave
const float GANANCIA = 1.0; // 2.0 = el servo se mueve el doble que el sensor
// -----------------------------
float referencia = 0;
float filtrado = 0;
float leerAngulo(float ax, float ay, float az) {
if (MODO == 0) return atan2(ax, az) * 180.0 / PI;
if (MODO == 1) return atan2(ay, az) * 180.0 / PI;
return atan2(ax, ay) * 180.0 / PI;
}
bool leer(float &ang) {
sensors_event_t accel, gyro, mag, temp;
icm.getEvent(&accel, &gyro, &temp, &mag);
float ax = accel.acceleration.x;
float ay = accel.acceleration.y;
float az = accel.acceleration.z;
float m = sqrt(ax * ax + ay * ay + az * az);
if (m < 7.0 || m > 13.0) return false; // lectura falsa
ang = leerAngulo(ax, ay, az);
return true;
}
void setup() {
Serial.begin(115200);
delay(2000);
Wire.setSDA(D4);
Wire.setSCL(D5);
Wire.begin();
Wire.setClock(100000);
delay(300);
bool ok = false;
for (int i = 0; i < 10 && !ok; i++) {
ok = icm.begin_I2C(0x69, &Wire);
if (!ok) delay(500);
}
if (!ok) {
Serial.println("No se pudo iniciar el sensor");
while (1) delay(10);
}
servo.attach(PIN_SERVO, 500, 2400);
servo.write(90);
Serial.println("Deja el sensor quieto: calibrando 3 segundos...");
delay(3000);
// Promedio de varias lecturas como referencia (cero)
float suma = 0; int n = 0;
for (int i = 0; i < 50; i++) {
float a;
if (leer(a)) { suma += a; n++; }
delay(20);
}
referencia = (n > 0) ? suma / n : 0;
Serial.println("Listo, inclina el sensor");
}
void loop() {
float ang;
if (!leer(ang)) { delay(10); return; }
float rel = ang - referencia;
if (rel > 180) rel -= 360; // evita saltos al cruzar ±180°
if (rel < -180) rel += 360;
rel *= GANANCIA;
filtrado = filtrado * SUAVIZADO + rel * (1.0 - SUAVIZADO);
float destino = INVERTIR ? 90 - filtrado : 90 + filtrado;
destino = constrain(destino, 0, 180);
servo.write((int)destino);
Serial.print("Angulo: "); Serial.print(filtrado);
Serial.print(" Servo: "); Serial.println((int)destino);
delay(20);
}
- Verify -> Upload
This took me a lot of time because something wasn't working, first I changed the cables, I moved everything around on the breadboard and there was still something wrong, I changed the code, tried different codes to find the error, and after a long time, I figured out it was the breadboard, so I connected everything just between each of them with the help of some alligator clips and finally, made it work.
Result¶
Exploration #2¶
The second trial objective was to make a pulse sensor to light an LED, then, according to the heart rate, make a relaxed or stressed face appear in an OLED screen.
Materials
- Pulse sensor
- Lilypad LED
- OLED SSD1306
- Conductive thread
- Conductive fabric
- XIAO RP2040
- First I tested the circuit by itself: connecting everything to the XIAO microcontroller like this:
Pulse sensor:
Signal (S) → D0
Power (+) → 3V3
Ground (-) → GND
OLED SSD1306 128x32:
SDA → D4
SCL → D5
VCC → 3V3
GND → GND
LilyPad LED:
(+) → D2
(-) → GND
- For this one I used Arduino software, selected the XIAO RP2040 board, downloaded the Adafruit SSD1306, Adafruit GFX libraries with it's dependancies, as well as the PulseSensor playground.
- The code I used was:
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define PULSE_PIN A0
#define LED_PIN D2
Adafruit_SSD1306 display(128, 32, &Wire, -1);
int umbral = 2000; // ajusta según tu señal (RP2040: 0-4095 con analogReadResolution(12))
bool enLatido = false;
unsigned long ultimoLatido = 0;
int bpm = 0;
unsigned long ledApagarEn = 0;
const int BPM_ESTRES = 100; // arriba de esto = estresado (ajústalo)
void setup() {
pinMode(LED_PIN, OUTPUT);
analogReadResolution(12);
Wire.begin();
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.display();
}
void carita(bool estresada, int bpm) {
display.clearDisplay();
// cara
display.drawCircle(16, 16, 14, SSD1306_WHITE);
display.fillCircle(11, 12, 2, SSD1306_WHITE); // ojo izq
display.fillCircle(21, 12, 2, SSD1306_WHITE); // ojo der
if (estresada) {
// boca triste / tensa
display.drawLine(10, 23, 22, 23, SSD1306_WHITE);
display.drawLine(8, 6, 13, 8, SSD1306_WHITE); // cejas
display.drawLine(24, 6, 19, 8, SSD1306_WHITE);
} else {
// sonrisa
display.drawCircleHelper(16, 17, 8, 0x0C, SSD1306_WHITE);
}
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(45, 2);
display.print(bpm);
display.setTextSize(1);
display.setCursor(45, 22);
display.print(estresada ? "BPM estres" : "BPM calma");
display.display();
}
void loop() {
int lectura = analogRead(PULSE_PIN);
unsigned long ahora = millis();
if (lectura > umbral && !enLatido) {
enLatido = true;
unsigned long intervalo = ahora - ultimoLatido;
if (intervalo > 300 && intervalo < 2000) { // filtra ruido
int nuevo = 60000 / intervalo;
bpm = (bpm == 0) ? nuevo : (bpm * 3 + nuevo) / 4; // suavizado
}
ultimoLatido = ahora;
digitalWrite(LED_PIN, HIGH); // parpadeo con cada latido
ledApagarEn = ahora + 100;
if (bpm > 0) carita(bpm > BPM_ESTRES, bpm);
}
if (lectura < umbral - 200) enLatido = false;
if (ahora > ledApagarEn) digitalWrite(LED_PIN, LOW);
}
-
The screen was working sporadically and I thought the pulse sensor was working, now I know it wasn't. The LED was what was working better, but still not great performance.
-
I thought this was because of the instability in which I was working, so I tried on a top. The connections where the same, in this case I used conductive thread and conductive fabric for the shared connections.
Here I discovered nothing was really working so I rearranged the connections and tried again, and again, it seemed like each time I fixed something, other thing disconnected, after many trials the LED was turning on and the pulse sensor was on, but they weren't dependent on each other. The screen wasn't turning on even though all the tests indicated it was fine.
Result¶
Exploration #3¶
For this one I just tried to do each task I tried for exploration #2 which were:
- Make the LED light according to heart pulse through the sensor
- Make the OLED screen show different faces
3.1¶
I connected the pulse sensor and the LED to the XIAO like this:
- And in Arduino considering the libraries I already installed, I used this code:
#define PULSE_PIN A0 // D0
#define LED_PIN D7
bool enLatido = false;
unsigned long ultimoLatido = 0;
int bpm = 0;
int brillo = 0;
unsigned long ultimoFade = 0;
int maxV = 0, minV = 4095;
unsigned long ultimoAjuste = 0;
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
analogReadResolution(12);
}
void loop() {
unsigned long ahora = millis();
int lectura = analogRead(PULSE_PIN);
if (lectura > maxV) maxV = lectura;
if (lectura < minV) minV = lectura;
if (ahora - ultimoAjuste > 2000) { // reinicia el rango cada 2 s
ultimoAjuste = ahora;
maxV = lectura;
minV = lectura;
}
int amplitud = maxV - minV;
int umbralAlto = minV + amplitud * 0.6;
int umbralBajo = minV + amplitud * 0.4;
if (amplitud > 50) { // ignora si no hay dedo
if (lectura > umbralAlto && !enLatido) {
enLatido = true;
unsigned long intervalo = ahora - ultimoLatido;
if (intervalo > 300 && intervalo < 2000) {
int nuevo = 60000 / intervalo;
bpm = (bpm == 0) ? nuevo : (bpm * 3 + nuevo) / 4;
Serial.print("BPM: ");
Serial.println(bpm);
}
ultimoLatido = ahora;
brillo = 255;
}
if (lectura < umbralBajo) enLatido = false;
}
// fade del LED
if (ahora - ultimoFade > 8) {
ultimoFade = ahora;
if (brillo > 0) brillo -= 6;
if (brillo < 0) brillo = 0;
analogWrite(LED_PIN, brillo);
}
}
Result¶
The result was positive, even though I consider the pulse sensor was a bit weak and was sometimes skipping some pulses, either way, it worked better than before. As you can see in the right video I discovered it could light when touching it with was interesting and something I could later explore.
3.2¶
For this I just connected the OLED screen to the XIAO like this:
- And in Arduino, I used this code:
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// I2C a 100 kHz (más tolerante con contactos sin soldar)
Adafruit_SSD1306 display(128, 32, &Wire, -1, 100000, 100000);
bool pantallaOk = false;
unsigned long ultimoIntento = 0;
bool parpadeando = false;
unsigned long proximoParpadeo = 0;
unsigned long finParpadeo = 0;
unsigned long ultimoFrame = 0;
unsigned long inicioEstado = 0;
int frame = 0;
bool estresada = false;
int bpmDemo = 72;
bool iniciarPantalla() {
Wire.begin();
Wire.setClock(100000);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) return false;
Wire.setClock(100000);
display.ssd1306_command(SSD1306_SETCONTRAST);
display.ssd1306_command(255);
display.clearDisplay();
display.display();
return true;
}
void dibujarCara(bool estres, int f) {
display.clearDisplay();
display.drawCircle(16, 16, 15, SSD1306_WHITE);
// ojos
int movOjos = estres ? 0 : (int)(sin(f * 0.15) * 1.5);
int ojoY = 12;
if (parpadeando) {
display.drawLine(8 + movOjos, ojoY, 13 + movOjos, ojoY, SSD1306_WHITE);
display.drawLine(19 + movOjos, ojoY, 24 + movOjos, ojoY, SSD1306_WHITE);
} else {
int r = estres ? 3 : 2;
display.fillCircle(11 + movOjos, ojoY, r, SSD1306_WHITE);
display.fillCircle(21 + movOjos, ojoY, r, SSD1306_WHITE);
}
if (estres) {
display.drawLine(7, 6, 14, 9, SSD1306_WHITE); // cejas fruncidas
display.drawLine(25, 6, 18, 9, SSD1306_WHITE);
int temblor = (f % 2) ? 1 : 0; // boca ondulada
for (int x = 9; x < 23; x++) {
int y = 24 + (int)(sin((x + f) * 0.9) * 1.5) + temblor;
display.drawPixel(x, y, SSD1306_WHITE);
}
int gotaY = 4 + (f * 2) % 22; // gota de sudor
display.fillCircle(30, gotaY, 1, SSD1306_WHITE);
display.drawPixel(30, gotaY - 2, SSD1306_WHITE);
} else {
int respira = (int)(sin(f * 0.2) * 1); // sonrisa que respira
display.drawCircleHelper(16, 18 + respira, 8, 0x0C, SSD1306_WHITE);
}
display.setTextColor(SSD1306_WHITE);
display.setTextSize(2);
display.setCursor(46, 2);
display.print(bpmDemo);
display.setTextSize(1);
display.setCursor(46, 22);
display.print(estres ? "BPM estres" : "BPM calma");
display.display();
}
void setup() {
Serial.begin(115200);
pantallaOk = iniciarPantalla();
Serial.println(pantallaOk ? "OLED OK" : "OLED no encontrada (reintentando)");
proximoParpadeo = millis() + 2000;
inicioEstado = millis();
}
void loop() {
unsigned long ahora = millis();
// Si falló, reintenta cada 2 s (útil con contactos flojos)
if (!pantallaOk) {
if (ahora - ultimoIntento > 2000) {
ultimoIntento = ahora;
pantallaOk = iniciarPantalla();
Serial.println(pantallaOk ? "OLED OK" : "Sigue sin detectarse");
}
return;
}
// Cambia entre relajada y estresada cada 6 s
if (ahora - inicioEstado > 6000) {
inicioEstado = ahora;
estresada = !estresada;
bpmDemo = estresada ? 118 : 72;
}
// Parpadeo
if (!parpadeando && ahora > proximoParpadeo) {
parpadeando = true;
finParpadeo = ahora + 150;
}
if (parpadeando && ahora > finParpadeo) {
parpadeando = false;
proximoParpadeo = ahora + (estresada ? random(600, 1200) : random(2500, 5000));
}
// Animación (~12 fps)
if (ahora - ultimoFrame > 80) {
ultimoFrame = ahora;
frame++;
dibujarCara(estresada, frame);
}
}
Result¶
I was really happy with this result, it was the first time during the week where the screen showed something, so it made me confirm that it worked.
Weekly reflection & critical thinking¶
This week was one of the hardest for me, considering the microcontrollers world is new for me I take everything as information that can help me understand better, it was a bit frustrating but it's also fun to try figuring what the problem is and try new ways to make it work. Something that was really interesting for me is noticing how most of the times it was something related to the hardware what was complicating the tasks, not really the code or the wiring. This connects to my final project because I want it to include a microcontroller so I consider every interaction I can have with it useful, so I learn more and have more information that could help me during it.
















