Flex Sensor Example

One of my students is working on a flex sensor, so I thought I would share some of the code.

int myInA0 = 0;

void setup() {
   pinMode(D7, OUTPUT); // onboard LED
   pinMode(A4, OUTPUT); // PWM
   //pinMode(A0, INPUT_PULLDOWN); do not set analogRead
   
   digitalWrite(D7,HIGH);
   delay(1000);
   digitalWrite(D7,LOW);
}

void loop() {

    delay(10);
    myInA0 = (analogRead(A0)-660)/10; // you decide the subtraction and division
    
    if (myInA0 <= 0) {myInA0=0;}
    if (myInA0 >= 250) {myInA0=250;}
    analogWrite(A4, myInA0);

}

The resistor in the voltage divider is 100K ohm. The weird circuits items are called SNAP circuits 750 from Elenco. Bend the flex sensor and the red LED becomes brighter.

2 Likes