On this tutorial, we will use the Arduino to read a flex sensor. The flex sensor is a device that changes its resistance proportional to its form, like this:
If the sensor is straight, it outputs 30k, it is flexed, 50k. With an Arduino board, make this circuit:
The resistor makes a voltage divider to make the reading possible. The analog input A0 is our signal.
Open the Arduino IDE and upload the following code. This code shows how many degrees the sensor is flexed.
// Flex sensor test program
// Mike Grusin, SFE, 2011
// This program is free, use it however you wish!
// Make the following connections between the Arduino and the flex sensor
// Note that the flex sensor pins are interchangeable
// Sensor pin - GND
// Sensor pin - Analog In 0, with 10K resistor to +5V
// Upload this sketch to your Arduino, then activate the Serial Monitor
// (set the Serial Monitor to 9600 baud)
// initialize serial communications
int sensor, degrees;
// read the voltage from the voltage divider (sensor plus resistor)
sensor = analogRead(0);
// convert the voltage reading to inches
// the first two numbers are the sensor values for straight (768) and bent (853)
// the second two numbers are the degree readings we'll map that to (0 to 90 degrees)
degrees = map(sensor, 768, 853, 0, 90);
// note that the above numbers are ideal, your sensor's values will vary
// to improve the accuracy, run the program, note your sensor's analog values
// when it's straight and bent, and insert those values into the above function.
// print out the result
Serial.print("analog input: ");
Serial.print(" degrees: ");
// pause before taking the next reading
Upload the code and open the serial monitor to see the results.