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!
