PAGE 08 PAGE 10
WEEK #09

INPUT DEVICES

Giving senses to the machine! Measuring distance with IR and pressure with custom e-textiles.

ALL ACCESS MISSION FILES (GITLAB REPO)

MISSION BRIEFING

This week's directive is to add sensors to a microcontroller board that I have designed and read them. I decided to tackle two different types of inputs for my custom XIAO RP2350 board: a Sharp GP2Y0A41SK0F Infrared Distance Sensor and a custom-made piezoresistive fabric sensor using Velostat.

01. GROUP ASSIGNMENT

For the group assignment, we probed the analog levels and digital signals of an input device using an oscilloscope and a multimeter.

📂 OPEN GROUP ASSIGNMENT

02. PREPARING ARDUINO IDE (XIAO CORE)

Since the Seeed Studio XIAO RP2350 is a very new microcontroller, the Arduino IDE does not recognize it by default. Before writing any code, I had to install the specific Board Manager Core. Here are the 7 steps I followed:

03. THE HARDWARE BASE (PCB V2.0)

For this assignment, I am required to read a sensor using a board I designed myself. I am using the custom PCB I milled and soldered during Electronics Production week. At its core is the Seeed Studio XIAO RP2350. Since my design exposed 7 additional free pins via male headers, I had plenty of options. For these analog sensors, I decided to use the analog pin D2 (A2) to read the data, tapping into the available 5V and GND pads to power the modules.

🛠️ VIEW MY CUSTOM BOARD DESIGN PROCESS (WEEK 08)
Custom PCB V2.0 Setup
My custom V2.0 board. Ready to receive analog signals.

04. TROUBLESHOOTING THE COM PORT

Before I could upload any code to read the sensors, I ran into a major roadblock: the Arduino IDE was not detecting the COM port of my XIAO RP2350. The board was powered on, but the computer couldn't communicate with it to upload the sketch. After some research, I found out this is a common issue with new RP2040/RP2350 chips.

⚙️ HOW TO ENTER BOOTLOADER MODE (XIAO RP2350)

To solve this, we need to force the XIAO into "Bootloader" mode. This bypasses the COM port issue and makes the microcontroller appear as a standard USB flash drive on your computer. Here is the exact physical button sequence required:

  1. Press and HOLD the 'B' (Boot) button on the board.
  2. While keeping 'B' held down, press the 'R' (Reset) button once.
  3. Release the 'R' button.
  4. Finally, release the 'B' button.

*Result: A new folder will pop up on your computer screen. Your board is now ready to receive the compiled `.uf2` file manually!

The solution involved manually compiling the code and flashing a binary file to upload the code to the Xiao without Arduino. Here is the step-by-step fix:

05. SENSOR 1: SHARP IR DISTANCE

I tested the Sharp GP2Y0A41SK0F. The sensor needs 5V to power on, but its output signal peaks at ~3.1V, which connects directly to the XIAO's 3.3V logic perfectly!

HOW IT WORKS: OPTICAL TRIANGULATION

Unlike ultrasonic sensors that measure the time it takes for a sound wave to bounce back, the Sharp IR sensor uses Optical Triangulation. It shoots a pulse of infrared light and measures the angle at which the light reflects back into its Position Sensitive Detector (PSD). Because it measures the angle rather than the strength of the light, it is not easily tricked by the color or reflectivity of the object!

Sharp Sensor connected to XIAO
Hero Test: The Serial Monitor displaying accurate distance in centimeters!

05. THE PIEZORESISTIVE FORCE (HOW IT WORKS)

Before diving into programming, the mission required understanding the secret energy behind Velostat. The diagram below (from the excellent Fab Lab León documentation) decrypts the data.

Our sewn t-shirt sensor operates on this exact principle. When pressure is applied to the Velostat layer between the conductive threads, the resistance between the conductors drops, allowing more voltage to pass to our analog input pin D2 (A2), which the machine converts into data.

06. SENSOR 2: VELOSTAT FABRIC SENSOR

For my second input, I designed an e-textile sensor using faux leather and Velostat. Velostat is a piezoresistive material whose electrical resistance decreases when compressed. I built a "shirt" structure with sewn contacts to create a complete circuit loop.

To connect this soft sensor to my rigid PCB, I used a modular approach: alligator clips paired with jumper wires. I clamped one alligator clip to the copper contact pad on the sensor and plugged its jumper end into the analog pin D2 (A2) on my board. The second alligator clip was attached to the conductive thread on the opposite side of the sensor, with its jumper connected to a GND pin.

By utilizing the XIAO's internal pull-up resistor (INPUT_PULLUP) in my code, I created a voltage divider without needing to solder any external resistors to my PCB. As pressure increases on the fabric, the resistance drops, and the analog value read by the ADC goes down.

Custom Velostat Leather Sensor
Hero Test: Plotter visualizing pressure waves!

07. THE CODE VAULT

// CODE 1: SHARP IR

Understanding the Math: The Sharp IR sensor does NOT output a linear signal. To convert the raw voltage into actual centimeters, we must use an approximation formula derived from its datasheet curve. The formula distancia_cm = 13.0 / (voltaje - 0.1); is a standard algebraic fit used for the GP2Y0A41SK0F model to translate the non-linear voltage drop into a reliable distance reading (valid between 4cm and 30cm).

const int PIN_SHARP = A2; 
const int LED_PLACA = D0; 

void setup() {
  Serial.begin(115200); 
  analogReadResolution(12); 
  pinMode(LED_PLACA, OUTPUT);
}

void loop() {
  int lectura = analogRead(PIN_SHARP);
  float voltaje = lectura * (3.3 / 4095.0);
  
  float distancia_cm = 0;
  if (voltaje > 0.4) {
    distancia_cm = 13.0 / (voltaje - 0.1); 
  }

  Serial.println(distancia_cm);

  if (distancia_cm > 4 && distancia_cm < 10) { 
    digitalWrite(LED_PLACA, HIGH);
  } else {
    digitalWrite(LED_PLACA, LOW);
  }
  delay(100); 
}

// CODE 2: VELOSTAT

const int PIN_CAMISA = A2; 
const int LED_PLACA = D0; 

void setup() {
  Serial.begin(115200); 
  pinMode(LED_PLACA, OUTPUT);
  pinMode(PIN_CAMISA, INPUT_PULLUP); 
}

void loop() {
  int presion = analogRead(PIN_CAMISA);
  Serial.println(presion);

  if (presion < 2000) { 
    digitalWrite(LED_PLACA, HIGH);
  } else {
    digitalWrite(LED_PLACA, LOW);
  }
  delay(50); 
}