.INPUT DEVICES.

We are now equipped to a. understand inputs and how basic electronics read data, ad b. design our own sensors for specific use cases

This week was all about that

Since it was complete new worlds for us we started by re visting all sensors and diving slightly deeper into individual topics
These were all the sensors we had on board that we could test out

Testing out sensors

This is a flex sensor

Flex Sensor

A flex sensor is a component that changes its resistance when bent. It is commonly used in wearable tech, gloves, and motion detection systems.

How It Works

Inside the flex sensor, there is a conductive material. When the sensor is straight, the resistance is low. When you bend it, the conductive particles move apart, increasing the resistance.

👉 More bending = More resistance
👉 Less bending = Less resistance

Usage with Arduino

The flex sensor is usually connected in a voltage divider circuit. The Arduino reads the voltage change using an analog pin and converts it into values that represent how much the sensor is bent.

Applications

  • Gesture control gloves
  • Robotics
  • Medical devices
  • Gaming controllers
As you can see the resistance reading jumps slightly when the flex sensor is bent
I started by checking the development bard as to if the GPIO pins are working and not shorted

GPIO Pins

GPIO stands for General Purpose Input/Output. These are programmable pins on microcontrollers like Arduino and Raspberry Pi that allow you to interact with electronic components.

How It Works

Each GPIO pin can be set as either an input or an output.

👉 INPUT = Reading signals
👉 OUTPUT = Sending signals

Digital vs Analog

GPIO pins can work in different ways:

Usage Example

For example, you can connect a button to a GPIO pin as input and an LED to another pin as output. When the button is pressed, the microcontroller reads the input and turns on the LED.

Applications

For a new challange I tried micropython this time

Installing Editor for micropyhton

Link to install thonny is below
https://thonny.org
Once it opens we need to conect pur board the right way

On the bottom right click the loacl pyhton3, then click configure interpreter

On the drop box that appears select your board

In our case RP2040

You can also update your micropython bootloader on yout board by clicking below
Choose the family, then choose the variant, latest version and hit install
It will copy the update to the vonnected board
For some reason my xiao was only connecting in FS mode, making uploading code difficult
This was later figured out to be a board issue, once I swapped the xiao for a fresh one it connected and updated smoothly
Since it was a new chip, I had to resort to using a bread board

Sorry Neil .·°՞(っ-ᯅ-ς)՞°·.

Im using a 10k resistor to filter out noise and get more accurate readings from my flex sensor

As you can see the wave change when I flex or release the sensor

There is still plenty of noise in this setup but still a good consistent reading


int flexPin = A0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  int value = analogRead(flexPin);
  Serial.println(value);
  delay(100);
}

Code for the above test

Capacitive sensor

Making my own sensor

So my campus had a collaboration with navrachna labs for which they were making a touc sensitive fabric, using thin copper wire weaved into old and waste fabric

They reached out to us for bringing this idea to life

This was a protype level display piece so there is no actual weaved thin copper strips, but a different approach to create a sensor.

My approach to achieve this touch sensitivity was to read the bodies resistance, when it touches the circuit and give an output based on the readings

This is my basic plan to read the change in resistance, sending a signal through pin 4 through a resisitor to nullify noise
Sample That I recd to work on
This was the first test with thin cheap copper wire and a 10k ohm resistor
This test was pretty noisy and not reliable but a good first direction in reading resistance of the body
#include CapacitiveSensor.h>

CapacitiveSensor touchSensor = CapacitiveSensor(4,2);

int buzzer = 8;

void setup() {
  Serial.begin(9600);
  pinMode(buzzer, OUTPUT);

}
void playTune() {

  int melody[] = {262, 294, 330, 392, 330, 294, 262}; // C D E G E D C
  int duration[] = {200, 200, 200, 300, 200, 200, 300};

  for(int i = 0; i < 7; i++) {
    tone(8, melody[i]);
    delay(duration[i]);
    noTone(8);
    delay(50); // small gap = softer feel
  }
}

void loop() {

  long val = touchSensor.capacitiveSensor(300);

  Serial.println(val);

  if(val > 500) {
    playTune();
  }
  else {
    noTone(buzzer);
  }

  delay(50);
}

Code for this output

#include CapacitiveSensor.h> CapacitiveSensor touchSensor = CapacitiveSensor(4,2); void setup() { Serial.begin(9600); } void loop() { long val = touchSensor.capacitiveSensor(300); Serial.println(val); if(val > 500) { Serial.println("ON"); } else { Serial.println("OFF"); } delay(50);

Code to test if the resistance value set to test is sufficient

Change the if statement to where your readings are stable around

This test gave me a baseline for a few things

  • Higher resistance needed
  • Better conductror needed at touch side
  • Seperate send and recive pins for all touch pads
  • Trying with new conductor

    I took a single strand wire and stripped it down as i though it would be the best conductor

    The conductor here is a 1 mega ohm which helped me reduce noise alot

    #include CapacitiveSensor.h>
    
    // ----------- TOUCH PADS -----------
    CapacitiveSensor pad1 = CapacitiveSensor(4, 2);
    CapacitiveSensor pad2 = CapacitiveSensor(8, 5);
    CapacitiveSensor pad3 = CapacitiveSensor(9, 6);
    CapacitiveSensor pad4 = CapacitiveSensor(10, 7);
    
    int buzzer = 13;
    int threshold = 500;
    
    // ----------- NOTES -----------
    #define C4 262
    #define D4 294
    #define E4 330
    #define F4 349
    #define G4 392
    #define A4 440
    #define B4 494
    #define C5 523
    #define D5 587
    #define E5 659
    #define G5 784
    
    #define FS4 370
    #define GS4 415
    #define CS5 554
    
    // ----------- SETUP -----------
    void setup() {
      Serial.begin(9600);
      pinMode(buzzer, OUTPUT);
    
      pad1.set_CS_AutocaL_Millis(0xFFFFFFFF);
      pad2.set_CS_AutocaL_Millis(0xFFFFFFFF);
      pad3.set_CS_AutocaL_Millis(0xFFFFFFFF);
      pad4.set_CS_AutocaL_Millis(0xFFFFFFFF);
    }
    
    // ----------- CORE FUNCTION -----------
    void playWhileTouched(CapacitiveSensor &pad, int melody[], int duration[], int size) {
    
      while (pad.capacitiveSensor(40) > threshold) {
    
        for (int i = 0; i < size; i++) {
    
          // Stop instantly if released
          if (pad.capacitiveSensor(40) <= threshold) {
            noTone(buzzer);
            return;
          }
    
          if (melody[i] == 0) {
            noTone(buzzer); // rest
          } else {
            tone(buzzer, melody[i]);
          }
    
          delay(duration[i]);
          noTone(buzzer);
          delay(20);
        }
      }
    
      noTone(buzzer);
    }
    
    // ----------- SONGS -----------
    
    // 🎮 Mario (simplified)
    int melody1[] = {E5,E5,0,E5,0,C5,E5,0,G5,0,0,0,G4};
    int dur1[]    = {120,120,120,120,120,120,120,120,200,200,200,200,200};
    
    // 📱 Nokia ringtone
    int melody2[] = {E5,D5,FS4,GS4,CS5,B4,D4,E4,B4,A4,CS5,E4,A4};
    int dur2[]    = {150,150,150,150,150,150,150,150,150,150,150,150,300};
    
    // 😄 Fun loop
    int melody3[] = {C4,E4,G4,E4,C4,E4,G4,E4};
    int dur3[]    = {120,120,120,120,120,120,120,200};
    
    // 🚀 Arcade vibe
    int melody4[] = {C4,D4,E4,G4,E4,D4,C4,G4};
    int dur4[]    = {100,100,120,200,120,100,150,250};
    
    // ----------- LOOP -----------
    void loop() {
    
      long val1 = pad1.capacitiveSensor(40);
      long val2 = pad2.capacitiveSensor(40);
      long val3 = pad3.capacitiveSensor(40);
      long val4 = pad4.capacitiveSensor(40);
    
      Serial.print(val1); Serial.print(" | ");
      Serial.print(val2); Serial.print(" | ");
      Serial.print(val3); Serial.print(" | ");
      Serial.println(val4);
    
      if (val1 > threshold) {
        playWhileTouched(pad1, melody1, dur1, sizeof(melody1)/sizeof(int));
      }
      else if (val2 > threshold) {
        playWhileTouched(pad2, melody2, dur2, sizeof(melody2)/sizeof(int));
      }
      else if (val3 > threshold) {
        playWhileTouched(pad3, melody3, dur3, sizeof(melody3)/sizeof(int));
      }
      else if (val4 > threshold) {
        playWhileTouched(pad4, melody4, dur4, sizeof(melody4)/sizeof(int));
      }
      else {
        noTone(buzzer);
      }
    
      delay(10);
    }
    

    above code is to try all 4 buzzers

    Next i thought the buzzer was too jarring, I wanted a more refined sound coming out of the laptop speakers maybe

    For this I needed to take the usb modem read it via a python script to play a mp3 sound file when resistance is read as high

    This was the final result with the output through my laptop speakers using a script on my terminal.
    comb test 19 mm ply wood
    parametric fusion file for comb test
    Download File