Using arduino mega to read analog inputs from pressure sensors and mapping them to a load value

Hi all,
As the title of my post suggests I have connected 6 flexible resistive pressure sensors (in a voltage divider configuration 10kOhm resistor) to my arduino mega analog channels A0-A5. My goal is to have a calibration routine in the setup where readings are taken with no load on the sensors and with a known load (ex: 0.5kg) applied and then relating adc minimum and maximum values to no load and max load respectively.

I am trying to do it by declaring a 3row, 6 column array that takes in sensor numbers, their min adc values, their max adc values in row1, row2, row3, respectively. Then calling a calibrate function that counts through the array and populates min adc and max adc values so that I can use them later in the code to map to min and max load for each sensor. However, that's not happening; my calibrate function is not populating the allocated array elements with those values. What am I doing wrong?

//set pressure sensor in voltage divider config; Vcc-Psensor-Vout_to_arduino-10k res-ground
//relate Vout_to_arduino to mass on pressure sensor and display the mass on LCD
//problem: resistive pressure sensors have an unpredictable ohmic resistance response (and by extension adc value range)
//to make consistent readings of mass from pressure sensors need to get adc min and max values of each (which differ from one another) 
//and map them to 0 g and max grams (eg 1kg) 
//write calibration loop that looks at individual sensors no load adc value and known 
//load adc value, then map those values to 0 grams and max grams for each


#include<Math.h>
#include<LiquidCrystal.h>

const int row_num = 3;
const int column_num = 6;

int p_sensor1 = A0;
int p_sensor2 = A1;
int p_sensor3 = A2;
int p_sensor4 = A3;
int p_sensor5 = A4;
int p_sensor6 = A5;

int min_adc1 = 0;
int min_adc2 = 0;
int min_adc3 = 0;
int min_adc4 = 0;
int min_adc5 = 0;
int min_adc6 = 0;

int max_adc1 = 0;
int max_adc2 = 0;
int max_adc3 = 0;
int max_adc4 = 0;
int max_adc5 = 0;
int max_adc6 = 0;


// declare an array of 3 rows and 6 columns
//3 row, 6 column array that stores indexed numbers of sensors on first, min_adc_readings on second, and max_adc_readings on third rows
int p_sensor[row_num][column_num] = {{p_sensor1, p_sensor2, p_sensor3, p_sensor4, p_sensor5, p_sensor6},
                      {min_adc1, min_adc2, min_adc3, min_adc4, min_adc5, min_adc6},
                      {max_adc1, max_adc2, max_adc3, max_adc4, max_adc5, max_adc6}
                     };                                                                        // can do 2D array int p_sensor[3] [6]




void setup(){
  pinMode(p_sensor1, INPUT);
  pinMode(p_sensor2, INPUT);
  pinMode(p_sensor3, INPUT);
  pinMode(p_sensor4, INPUT);
  pinMode(p_sensor5, INPUT);
  pinMode(p_sensor6, INPUT);


  Serial.begin(9600);
  calibrate_sensors();
  
  //delay(1000);
  //prompt user to remove any load on sensors if any and press button
  //initiate calibration for no load
  //prompt user to apply known load to each sensor and press button
  //initiate calibration for known load

  Serial.println("values in array: first row");
  Serial.println(p_sensor[0][0]);
  Serial.println(p_sensor[0][1]);
  Serial.println(p_sensor[0][2]);
  Serial.println(p_sensor[0][3]);
  Serial.println(p_sensor[0][4]);
  Serial.println(p_sensor[0][5]);

  Serial.println("values in array: second row");
  Serial.println(p_sensor[1][0]);
  Serial.println(p_sensor[1][1]);
  Serial.println(p_sensor[1][2]);
  Serial.println(p_sensor[1][3]);
  Serial.println(p_sensor[1][4]);
  Serial.println(p_sensor[1][5]);

  Serial.println("values in array: third row");
  Serial.println(p_sensor[2][0]);
  Serial.println(p_sensor[2][1]);
  Serial.println(p_sensor[2][2]);
  Serial.println(p_sensor[2][3]);
  Serial.println(p_sensor[2][4]);
  Serial.println(p_sensor[2][5]);

  }

void loop(){
  
  Serial.println("values in array: first row");
  Serial.println(p_sensor[0][0]);
  Serial.println(p_sensor[0][1]);
  Serial.println(p_sensor[0][2]);
  Serial.println(p_sensor[0][3]);
  Serial.println(p_sensor[0][4]);
  Serial.println(p_sensor[0][5]);

  Serial.println("values in array: second row");
  Serial.println(p_sensor[1][0]);
  Serial.println(p_sensor[1][1]);
  Serial.println(p_sensor[1][2]);
  Serial.println(p_sensor[1][3]);
  Serial.println(p_sensor[1][4]);
  Serial.println(p_sensor[1][5]);

  Serial.println("values in array: third row");
  Serial.println(p_sensor[2][0]);
  Serial.println(p_sensor[2][1]);
  Serial.println(p_sensor[2][2]);
  Serial.println(p_sensor[2][3]);
  Serial.println(p_sensor[2][4]);
  Serial.println(p_sensor[2][5]);
  //Serial.println(p_sensor[0][0]);
  
  }
  

void calibrate_sensors() {
  for (int i = 0; i <= 5; i++) {
    Serial.println("I'm in for loop");
    Serial.println("i is: ");
    Serial.println(i);
    Serial.println("Value at index 0, i of array is: ");
    Serial.println(analogRead(p_sensor[0][i]));
    if (analogRead(p_sensor[0][i]) <= 0) {
      Serial.println("I'm in less than zero if...");
      p_sensor[1][i] == analogRead(p_sensor[0][i]);
      Serial.println(p_sensor[1][i]);
      if (analogRead(p_sensor[0][i]) > 0) {
        Serial.println("I'm in more than zero if..");
        p_sensor[2][i] == analogRead(p_sensor[0][i]);
        }
        }
    }
  }

My serial monitor output

I'm in for loop
i is: 
0
Value at index 0, i of array is: 
0
I'm in less than zero if...
0
I'm in for loop
i is: 
1
Value at index 0, i of array is: 
141
I'm in for loop
i is: 
2
Value at index 0, i of array is: 
220
I'm in for loop
i is: 
3
Value at index 0, i of array is: 
262
I'm in for loop
i is: 
4
Value at index 0, i of array is: 
291
I'm in for loop
i is: 
5
Value at index 0, i of array is: 
303
values in array: first row
54
55
56
57
58
59
values in array: second row
0
0
0
0
0
0
values in array: third row
0
0
0
0
0
0
![pressure sensor voltage divider arduino|666x362](upload://1NMJclmGUh2Czzmu13uVg8O4MUm.png)

here's the
pressure sensor voltage divider arduino
voltage divider scheme

Perhaps I'm misreading the code, but you don't have a 3x6 array of sensors, only sensor reading values.
And you are trying to analogRead the whole 3x6 array. It works for the first row, because these are all the sensors.
But the second and third rows can't be analogRead-ed :slight_smile:

I'm actually trying to read the first row and then depending on the value I get I use it as a condition to place that value in either the 2nd or 3rd row; for instance if I read a value of 0 I want to place that into an element on the 2nd row (min_adc) and if the value I read is greater than 0 I want to place it in the 3rd row (max_adc)

Did you mean == or = here:

p_sensor[1][i] == analogRead(p_sensor[0][i]);

Would you be able to simplify the code and simply read the sensors into a 1x6 array and print them, to see if the electrical part of your circuit work?

I also see why your print output is this:

values in array: first row
54
55
56
57
58
59

This is the printing out literally the A0, A1 pins etc on the Mega, not their values. I didn't catch it initially. This sequence starts with 14 on the Uno.

You will need to change

  Serial.println("values in array: first row");
  Serial.println(p_sensor[0][0]);

to

  Serial.println("values in array: first row");
  Serial.println(analogRead((p_sensor[0][0]));

It looks like the actual reading of the sensors is working fine.

Don't you need to read the first row with the sensors unloaded, stop the program and put the known load on each sensor, read the second row, calculate the offset and max reading, write that into the third row , then continue?

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