How to interface Arduino Nano 33 IoT with flex sensors?

I am trying to work with an Arduino Nano 33 IoT and have it work with flex sensors to calculate the resistance and bend angle. However, the readings seem to be way off. Here is my code:

const int ANALOG_FLEX_PIN7 = A0;  // Flex sensor 7 connected to a voltage divider output
const int ANALOG_FLEX_PIN9 = A1;  // Flex sensor 6 connected to a voltage divider output
const int ANALOG_FLEX_PIN20 = A2; // Flex sensor 20 connected to a voltage divider output
const int ANALOG_FLEX_PIN26 = A3; // Flex sensor 26 connected to a voltage divider output
const int ANALOG_FLEX_PIN27 = A6; // Flex sensor 27 connected to a voltage divider output

const float RESISTANCE_VALUE = 99700.0; // Measured constant resistance values of 100K resistors
const float VOLTAGE_VALUE = 4.98;       // Measured constant voltage value of 5V Arduino output

const float STRAIGHT_RESISTANCE9 = 40400.0; // Measured resistance value of flex sensor when straight
const float BEND_RESISTANCE9 = 96600.0;     // Measured resistance value of flex sensor when bent

const float STRAIGHT_RESISTANCE7 = 31100.0; // Measured resistance value of flex sensor when straight
const float BEND_RESISTANCE7 = 90500.0;     // Measured resistance value of flex sensor when bent

const float STRAIGHT_RESISTANCE20 = 27500.0; // Measured resistance value of flex sensor when straight
const float BEND_RESISTANCE20 = 72500.0;     // Measured resistance value of flex sensor when bent

const float STRAIGHT_RESISTANCE26 = 35100.0; // Measured resistance value of flex sensor when straight
const float BEND_RESISTANCE26 = 98300.0;     // Measured resistance value of flex sensor when bent

const float STRAIGHT_RESISTANCE27 = 33900.0; // Measured resistance value of flex sensor when straight
const float BEND_RESISTANCE27 = 95700.0;     // Measured resistance value of flex sensor when bent

void setup()
{
  Serial.begin(9600); // Begin communication to serial line

  pinMode(ANALOG_FLEX_PIN7, INPUT);  // Set pin mode to input
  pinMode(ANALOG_FLEX_PIN9, INPUT);  // Set pin mode to input
  pinMode(ANALOG_FLEX_PIN20, INPUT); // Set pin mode to input
  pinMode(ANALOG_FLEX_PIN26, INPUT); // Set pin mode to input
  pinMode(ANALOG_FLEX_PIN27, INPUT); // Set pin mode to input
}

void loop()
{
  int flexSensor7ADC = analogRead(ANALOG_FLEX_PIN7);   // Read the ADC
  int flexSensor9ADC = analogRead(ANALOG_FLEX_PIN9);   // Read the ADC
  int flexSensor20ADC = analogRead(ANALOG_FLEX_PIN20); // Read the ADC
  int flexSensor26ADC = analogRead(ANALOG_FLEX_PIN26); // Read the ADC
  int flexSensor27ADC = analogRead(ANALOG_FLEX_PIN27); // Read the ADC

  float flexSensor7Voltage = flexSensor7ADC * VOLTAGE_VALUE / 1023.0;   // Calculate the sensor voltage
  float flexSensor9Voltage = flexSensor9ADC * VOLTAGE_VALUE / 1023.0;   // Calculate the sensor voltage
  float flexSensor20Voltage = flexSensor20ADC * VOLTAGE_VALUE / 1023.0; // Calculate the sensor voltage
  float flexSensor26Voltage = flexSensor26ADC * VOLTAGE_VALUE / 1023.0; // Calculate the sensor voltage
  float flexSensor27Voltage = flexSensor27ADC * VOLTAGE_VALUE / 1023.0; // Calculate the sensor voltage

  float flexSensor7Resistance = RESISTANCE_VALUE * (VOLTAGE_VALUE / flexSensor7Voltage - 1.0);   // Calculate the sensor resistance
  float flexSensor9Resistance = RESISTANCE_VALUE * (VOLTAGE_VALUE / flexSensor9Voltage - 1.0);   // Calculate the sensor resistance
  float flexSensor20Resistance = RESISTANCE_VALUE * (VOLTAGE_VALUE / flexSensor20Voltage - 1.0); // Calculate the sensor resistance
  float flexSensor26Resistance = RESISTANCE_VALUE * (VOLTAGE_VALUE / flexSensor26Voltage - 1.0); // Calculate the sensor resistance
  float flexSensor27Resistance = RESISTANCE_VALUE * (VOLTAGE_VALUE / flexSensor27Voltage - 1.0); // Calculate the sensor resistance

  float bendAngle7 = map(flexSensor7Resistance, STRAIGHT_RESISTANCE7, BEND_RESISTANCE7, 0, 90.0);     // Use the calculated resistance to estimate bend angle for sensor
  float bendAngle9 = map(flexSensor9Resistance, STRAIGHT_RESISTANCE9, BEND_RESISTANCE9, 0, 90.0);     // Use the calculated resistance to estimate bend angle for sensor
  float bendAngle20 = map(flexSensor20Resistance, STRAIGHT_RESISTANCE20, BEND_RESISTANCE20, 0, 90.0); // Use the calculated resistance to estimate bend angle for sensor
  float bendAngle26 = map(flexSensor26Resistance, STRAIGHT_RESISTANCE26, BEND_RESISTANCE26, 0, 90.0); // Use the calculated resistance to estimate bend angle for sensor
  float bendAngle27 = map(flexSensor27Resistance, STRAIGHT_RESISTANCE27, BEND_RESISTANCE27, 0, 90.0); // Use the calculated resistance to estimate bend angle for sensor

  Serial.println("Resistance for Sensor 7: " + String(flexSensor7Resistance) + " ohms"); // Print the resistance of the sensor
  Serial.println("Bend Angle for Sensor 7: " + String(bendAngle7) + " degrees\n");       // Print the bend angle of the sensor

  Serial.println("Resistance for Sensor 9: " + String(flexSensor9Resistance) + " ohms"); // Print the resistance of the sensor
  Serial.println("Bend Angle for Sensor 9: " + String(bendAngle9) + " degrees\n");       // Print the bend angle of the sensor

  Serial.println("Resistance for Sensor 20: " + String(flexSensor20Resistance) + " ohms"); // Print the resistance of the sensor
  Serial.println("Bend Angle for Sensor 20: " + String(bendAngle20) + " degrees\n");       // Print the bend angle of the sensor

  Serial.println("Resistance for Sensor 26: " + String(flexSensor26Resistance) + " ohms"); // Print the resistance of the sensor
  Serial.println("Bend Angle for Sensor 26: " + String(bendAngle26) + " degrees\n");       // Print the bend angle of the sensor

  Serial.println("Resistance for Sensor 27: " + String(flexSensor27Resistance) + " ohms"); // Print the resistance of the sensor
  Serial.println("Bend Angle for Sensor 27: " + String(bendAngle27) + " degrees\n");       // Print the bend angle of the sensor

  Serial.println("Flex sensor 7 ADC: " + String(flexSensor7ADC));
  Serial.println("Flex sensor 7 voltage: " + String(flexSensor7Voltage));

  Serial.println("----------------------------------------\n"); // Print a separator line

  delay(1000); // Delay program execution for 2 seconds
}

This code works perfectly fine as it is with an arduino uno or an arduino nano classic. However, when I switch to the nano 33 IoT, thats when problems with inaccuracy start happening. I think that it has something to do with my ADC values being inaccurate on the 33 IoT, because when I try to read those values, the value is consistently at 1023, thus causing the measurements to be inaccurate. Any reason why? Thank you so much!

Have you taken into account that the classic Nano runs at 5V whereas the Nano 33 IOT runs at 3.3V ?

Yea lol I realized this a little after I posted this, however, the measurements are still not as accurate as I would like them to be, or how accurate they were on the nano classic I was previously using with the 5V run voltage. Is it a possibility that because the nano 33 IoT has a lower run voltage at just 3.3V, it doesn't produce as accurate readings as the nano 33 IoT and the arduino uno, both of which run at 5V? Sorry I'm still a newbie to this kind of stuff and constantly learning. Thank you!

The voltage that the board runs at should have no effect on the accuracy of the measurements.

const float VOLTAGE_VALUE = 4.98;       // Measured constant voltage value of 5V Arduino output

Have you changed this when using the Nano 33 IOT ?

Yes, I changed that value to 3.30 when using the nano 33 IoT. But doesn't the voltage of the board affect the ADC values which then affects my measurements? I printed the ADC values on the serial monitor while powering the sensors with the +5V output and the ADC values were constantly at 1023. However, when I changed it to power the sensors with the +3.3V output, the ADC values were around 600. Maybe that info helps?

Were you using the 5V pin previously ? If so, have you enabled it by shorting the pads on the bottom of the board ?

Be careful that you do not, or have not already, exceed 3.3V on the 33 IOT input pins or damage might be caused

Yes I was using the +5V pin previously and I did short the pads at the bottom to enable it, but the ADC readings were at a steady 1023, thus messing up my measurements. I am not sure what you mean by exceeding 3.3V. I am only powering the nano 33 IoT via a micro USB that hooks up to my computer.

What value of pullup (or pulldown) resistor are you using?

I am using 100kOhm resistors for all of the five sensors.

Resistor between 3.3V and sensor or between GND and sensor?

Resistor between GND and the sensor!! Sorry for not specifying.

I was alluding to the fact that if you powered the sensors with 5V, which it appears that you did, then it was possible for 5V to be present on the input pins of the board via the sensor when the inputs are not 5V tolerant and could, therefore, have been damaged

Oh shoot! I do have a question about that though, how would the +5V output to the sensors be present on the analog pins as well? Because doesn't that mean that you can't power anything with the +5V output that needs to connect back to an analog pin? I don't know that's kind of confusing.

Please post a diagram of how the sensors are connected to the analogue pins. A 'photo of a pencil and paper drawing is good enough

Please do not try and describe the connection as that tends to be difficult to follow

@husseinkhodor03
From your code
5.0V*(99700/(27500+99700)) = 3.92V
Way to much!

This is basically how it was hooked up. You can ignore the module on the bottom thats cut off nearly halfway. Let me know if you need more info!!

What voltage will be applied to pin A0 when sensor F1 is at its minimum resistance value ?

Is there any possibility that the resistance of F1 will be less than 40K ?

I don't see anything that would harm a 3.3V AVR but I know nought about the Nano33.

Oof its hard to answer that question, I don't believe I know off the top of my head. If I recall, I think it was about 2.20V when at its minimum resistance. Same with all of the other sensors, they were all around that range +/- 0.10. That was when I was using the +3.3V output to power the sensors. However, when I used the +5V output to power the sensors, I believe they were at about 3.8 - 3.9V, maybe even more, I don't quite remember. I believe that I got these values by measuring with my multimeter the voltage from ground to the end of the resistor that is directly connected to the sensor.

I think that that was just a rough estimate. In other words, yes they can be less than 40k. I think the lowest sensor resistance reading that I got was about 25k when completely straight.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.