Professional Documents
Culture Documents
Code Explaination
Code Explaination
The sketch begins with the declaration of the Arduino pin to which the flex sensor is connected.
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.
In the setup, we establish serial communication and configure the flexPin as an INPUT.
void setup() {
Serial.begin(9600);
pinMode(flexPin, INPUT);
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:
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);
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.
Serial.println();