COMPUTER SCIENCE CAFÉ
  • WORKBOOKS
  • BLOCKY GAMES
  • GCSE
    • CAMBRIDGE GCSE
  • IB
  • A LEVEL
  • LEARN TO CODE
  • ROBOTICS ENGINEERING
  • MORE
    • CLASS PROJECTS
    • Classroom Discussions
    • Useful Links
    • SUBSCRIBE
    • ABOUT US
    • CONTACT US
    • PRIVACY POLICY
  • WORKBOOKS
  • BLOCKY GAMES
  • GCSE
    • CAMBRIDGE GCSE
  • IB
  • A LEVEL
  • LEARN TO CODE
  • ROBOTICS ENGINEERING
  • MORE
    • CLASS PROJECTS
    • Classroom Discussions
    • Useful Links
    • SUBSCRIBE
    • ABOUT US
    • CONTACT US
    • PRIVACY POLICY
Picture
ARDUINO | FLEX SENSOR
In this project, you'll use 2 flex sensors to detect how much they're being bent, and display this bending visually using 4 LEDs per sensor. The more you bend the strip, the more LEDs will light up — like a bar graph.
You'll learn how to:
  • Use analog input from a flex sensor
  • Interpret analog values and apply threshold logic
  • Use digital output to control LEDs
  • Apply the concept of mapping sensor values to levels​
SECTION 1 | MATERIAL NEEDED
  • Uno or compatible board - Any Arduino will work
  • Flex sensors x 2
  • 10kΩ resistors - 2 For voltage divider with the flex sensors
  • LEDs x 8 - 4 per flex sensor
  • 220Ω resistors x 8 - To protect LEDs
  • Breadboard x 1 or 2 small ones
  • Jumper wires x 20 Male-to-male
SECTION 2| THE PRINCIPLES
Each flex sensor acts as a variable resistor: its resistance increases as it bends. To measure this, we use a voltage divider circuit, which allows the Arduino to read a changing voltage on an analog pin.

​As the resistance increases, the voltage read increases. We compare this voltage to thresholds to determine how many LEDs should light up.

Each flex sensor's value is broken into 4 levels, and for each level, an additional LED is lit.
SECTION 3 | THE CONNECTIONS
Picture
Here is the circuit diagram for the first flex sensor with 4 LEDs
SECTION 3 | THE CODE
  • SINGLE FLEX SENSOR
  • TWO FLEX STRIPS
<
>

    

    
Key code concepts explained
  • analogRead(pin): Reads a voltage between 0–5V and converts it to a number between 0 and 1023.
  • digitalWrite(pin, HIGH/LOW): Sends voltage to an output pin to turn things ON or OFF.
  • for (int s = 0; s < 2; s++): A for loop with a counter variable s used to repeat code for each sensor.
  • s++: A shorthand for s = s + 1. It increases the variable s by 1 each loop.
  • Nested loops: Loops inside other loops. Used here to set up and update each sensor's group of LEDs.
  • level++: Increases the level variable each time the sensor reading passes a threshold, determining how many LEDs light up.
  • Serial.print() and Serial.println(): Used to display real-time values in the Serial Monitor for testing and calibration.
SECTION 4 | CHALLENGE
Can you duplicate the circuit to add a second flex sensor?

​Use analog pin A1 for the new flex sensor and connect digital pins 6, 7, 8, and 9 to four additional LEDs. Your goal is to create a working project with two flex sensors, where each sensor controls its own set of four LEDs, lighting up based on how much the sensor is bent.
Picture
SUGGESTIONS
We would love to hear from you
SUBSCRIBE 
To enjoy more benefits
We hope you find this site useful. If you notice any errors or would like to contribute material then please contact us.