Outcome: Reverse Engineering a Flex Sensor - reynold5/ECE387_Midterm GitHub Wiki

Purpose: The purpose of this information is to provide the user with a greater understanding of the overall operation of a flex sensor. The goal is to work backwards from the sensor code used with the Arduino to better understand the mathematical operations used to calculate resistance and angle as the flex sensor is bent. Refer to the "FS Simple Circuit Code" under the "Code" tab above for the exact source code referenced in this document.

Circuit Setup: Picture Source: https://learn.sparkfun.com/tutorials/flex-sensor-hookup-guide/all

Considerations: In the case of flex sensors, it is important to realize that the resistance of the flex sensor when straight and when bent can vary significantly from flex sensor to flex sensor. Most references regarding flex sensors suggest that when straight most flex sensors will measure resistance at approximately 30k Ohm and 70k Ohms went bent to 90 degrees. Realistically, these numbers will vary. For example, the flex sensor that I used registered at approximately 9200 Ohms when straight and 16k Ohms went bent to 90 degrees.

Before building your flex sensor circuit, use a multimeter to measure the resistance of your particular flex sensor when bent and when straight. Record these measurements for later use within the source code.

Also, the pins of a flex sensor aren't as thick as most other sensors or basic components normally used with a breadboard. This difference in thickness causes the flex sensor to easily slip out of the breadboard during testing. Be sure to firmly hold the flex sensor in place during testing, or if possible solder the flex sensor in place.

Examination of Source Code: The following provides important information relating how the source code provided works in relation to the flex sensor simple circuit. Multiple variable within the code can be adjusted to improve measurement data and overall sensitivity of the flex sensor circuit.

Code taken and modified from: https://learn.sparkfun.com/tutorials/flex-sensor-hookup-guide/all

const int sensorINPUT = A0; // Pin connected to voltage divider output

const float VCC = 4.98; // Measured voltage of Ardunio 5V line

const float resistorVALUE = 10968;

In the line of code above, the resisorVALUE should be the total resistance of all resistors within your circuit (excluding any resistance taken from your flex sensor). This should be a value between the measured resistance values of your straight (straightR) and bent (bentR) flex sensor. Picking a resistance value between your measured straight and bent values for your flex sensor will allow for the appropriate static resistance within the voltage division circuit you will create on your breadboard.

const float straightR = 9140.0; // resistance when straight

const float bentR = 16000; // resistance at 90 deg

When you take your initial bent resistance measurement for your flex sensor, you will notice an exact value is hard to obtain because it is difficult to keep the sensor perfectly still when bending. An approximate measurement will have to be used for the bentR variable above. The variables straightR and bentR will most likely need to be adjusted later to achieve more consistent angular measurements.

void setup() {

Serial.begin(9600);

pinMode(sensorINPUT, INPUT); }

void loop()

{ int flexADC = analogRead(sensorINPUT); //Reads voltage change made by bending of sensor

float flexV = flexADC * VCC / 1023.0; //Converts ADC value into a voltage

float flexR = resistorVALUE * (VCC / flexV - 1.0); //Calculates resistance of flex sensor

Serial.println("Resistance: " + String(flexR) + " ohms");

The variable flexADC above takes the voltage change caused by the flexing of the flex sensor (which causes a resistance change in the voltage division circuit, thus creating a voltage change on pin A0), and converts this voltage change through an ADC converter into a number between 0-1023. Generally, as the flex sensors resistance increases as it bends, the voltage on A0 goes to zero, and it is moved back into the straight position the voltage increases back to 5V. In the equation for flexV, the ADC conversion equation is used to convert the ADC binary value back into a voltage value which is stored in variable flexV. The resistance equation for flexR, then uses the voltage value stored in flexV to calculate the resistance output of the flex sensor as it bends. The resistance (in Ohms) of the flex sensor is then read out via the measurement window in the Arduino software.

float angle = map(flexR, straightR, bentR, 0, 90.0);

Serial.println("Bend: " + String(angle) + " degrees");

Serial.println();

delay(500);

After the flexR calculation has been made, this resistance value is used to with the map function to correlate the angle associated with each resistance value. The value for 0 degree is associated with the resistance value when the flex sensor is straight (straightR) and the value fir 90 degrees is associated with the resistance of the flex sensor bent at approximately 90 degrees (bentR). This map function may be adjusted depending on the users desired system response. For example, if the user wants to map the flex sensors resistance at the 135 degree mark then the initial bentR resistance of the flex sensor should be taken again with the sensor independent of the circuit. Once bentR is updated with the new resistance, the angle variable with the map function would be changed to: angle = map(flexR, straightR, bentR, 0, 135.0). This would then read out angle values from 0-135 degrees.