Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Code Explanation:

The sketch begins with the declaration of the Arduino pin to which the flex sensor is connected.

const int flexPin = A0;

Then, a few constants are defined, including the system voltage (VCC), the resistor used to make a
voltage divider (R_DIV), and the resistance offered by the flex sensor in its flat and bent configurations
(flatResistance and bendResistance, respectively). Make sure these constants are set correctly.

const float VCC = 5;

const float R_DIV = 47000.0;

const float flatResistance = 25000.0;

const float bendResistance = 100000.0;

In the setup, we establish serial communication and configure the flexPin as an INPUT.

void setup() {

Serial.begin(9600);

pinMode(flexPin, INPUT);

In the loop, we start by reading the ADC.

int ADCflex = analogRead(flexPin);

When the Arduino converts the analog output voltage of the sensor to a digital value, it converts it to a
10-bit number between 0 and 1023. Therefore, to calculate the actual output voltage, we use the following
formula:

float Vflex = ADCflex * VCC / 1023.0;

The resistance of the flex sensor is then calculated using the formula derived from the voltage divider
formula and displayed on the serial monitor.
float Rflex = R_DIV * (VCC / Vflex - 1.0);

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

We then use the calculated resistance to estimate the bend angle of the sensor. For this, we use the IDE’s
built-in map() function.

The map() function maps and converts the sensor’s resistance to its bend angle. When we call map(Rflex,
flatResistance, bendResistance, 0, 90.0), the value of flatResistance is mapped to 0°, the value of
bendResistance is mapped to 90°, and the values in-between are mapped to values in-between.

float angle = map(Rflex, flatResistance, bendResistance, 0, 90.0);

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

Serial.println();

You might also like