How to read inputs from multiple analog pins?

Hey guys,

I'm trying to read analog inputs from multiple analog pins. I understand how to read inputs from one analog pin but when I try to scale the code to read for more than one no matter what I do I just get errors.

I found a bunch of information online about reading multiple inputs from one analog pin but not any on reading information for multiple pins. I'm using an Arduino Mega with 15 analog inputs. Basically my project consists of reading multiple voltages below 5 volts on 8 analog input pins. And then sending that data to Adafruit IO or blynk. Below is the code that I'm using that is working to read one analog pin, could somebody please just show me how to scale the code to read from multiple pins and have the data sent to Adafruit IO which is the code I've provided below.

I've been pulling my hair out and I am at the absolute deadline of my project. I'm at Hardware guy, and I can barely modify a premade sketch to get it to work for my needs but anything other than that it's just a disaster. So if somebody could just show me what lines need to be added it would be greatly appreciated. I understand that most questions asked on the Forum and on all Arduino forums basically the people that are helping answer the questions normally like to give small hints on how to fix the problem which normally is the best way to go about it. Give a man a fish he eats for a day, teach a man to fish... but basically I am on my deathbed if I do not eat a fish right away... LOL

The sketch below works just fine and takes the analog input from A0 and sends it to my Adafruit IO dashboard. I just need to scale this somehow to send multiple analog inputs to my dashboard.

Thank you very much for your help it is very much appreciated!!!

The Code:

// set up the 'analog' feed
AdafruitIO_Feed *analog = io.feed("analog");

void setup() {

  // start the serial connection
  Serial.begin(115200);

  // wait for serial monitor to open
  while(! Serial);

  // connect to io.adafruit.com
  Serial.print("Connecting to Adafruit IO");
  io.connect();

  // wait for a connection
  while(io.status() < AIO_CONNECTED) {
    Serial.print(".");
    delay(500);
  }

  // we are connected
  Serial.println();
  Serial.println(io.statusText());

}

void loop() {

  // io.run(); is required for all sketches.
  // it should always be present at the top of your loop
  // function. it keeps the client connected to
  // io.adafruit.com, and processes any incoming data.
  io.run();

    // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (5.0 / 1023.0);
  
  // print out the value you read:
  Serial.println(voltage);
analog->save(voltage);
delay(5000);
}

Put the analogue pin numbers in an array and iterate through the array to use each pin number to read the value from that pin and do what you want with it before moving on to the next one. A for loop is a handy way to do this

Hardware guy, the answer is buried in the datasheet. Here is the short answer:

Manufacturer recommends that after switching analog pins you read twice and ignore the first read otherwise expect garbage.

The longer answer has to do with time for the pin just selected to settle.

In code, switch the pin and do a read before you get your sample in the next read.

Hey thanks for the info. I did read the other day about reading the analog pin twice. But other then that you lost me.

UKHeliBob:
Put the analogue pin numbers in an array and iterate through the array to use each pin number to read the value from that pin and do what you want with it before moving on to the next one. A for loop is a handy way to do this

The problem is that I have no idea how to do any of that. All I did to make my original code was take a example sketch from adafruit IO and a pre-made sketch to read analog pin voltage and write it to serial output and put the two together with lots of frustration in order for Arduino IDE not to give me errors.

Unfortunately I am at the limit of my knowledge on coding for the Arduino. I know it's not normal in any of these forums but I was hoping that somebody could take my sketch and had the necessary code to read from pin A1 and send it to Adafruit IO... the once I see how it works, I coukd then alter it to read from more analog pins etc... doesn't seem like I'm asking a lot but I know it's out of the norm....

So if anyone is willing to do that for me, I would be very Grateful!! Please know that this is a project that is going to make many Powerwall / Battery DIYers and there family's safer etc..

The only other real solution to see our huge diy batteries voltage and temperature in a easy to read format (GUI) costs hundreds of dollars +!!! I'm trying to create this for less than $20 give the project to DIY community.

So with that in mind if anyone could please take my sketch and add the necessary lines of code to read from analog pin A1 have it sent to Adafruit IO like it is in the Sketch I provided, I can take it from there and modify it to read from many analog pins. Again I've been at this for at least a month or more and it took me that long just to get what I have, working. It's not like I have not tried.

THANK YOU

Put the analogue pin numbers in an array and iterate through the array to use each pin number to read the value from that pin and do what you want with it before moving on to the next one. A for loop is a handy way to do this
The problem is that I have no idea how to do any of that.

const byte inputs[] = {A0, A1, A2};

void setup()
{
  Serial.begin(115200);
  while (!Serial);
}

void loop()
{
  for (int pin = 0; pin < 3; pin++)
  {
    int dummy = analogRead(inputs[pin]);  //read values twice as advised
    int value = analogRead(inputs[pin]);
    Serial.println(value);  //do whatever you want with the value
  }
}

@UKHeliBob

Thank you very much. Extremely appreciated!!!

I've taken the code and expanded it how I think it should be to read from A0 through A2... did I do it correctly? I'm at work at the moment so I'm not able to try it in IDE.

Thanks again

const byte inputs[] = {A0, A1, A2};

void setup()
{
  Serial.begin(115200);
  while (!Serial);
}

void loop()
{
  for (int pin = 0; pin < 3; pin++)
  {
    int dummy = analogRead(inputs A0);  //read values twice as advised
    int value = analogRead(inputs A0);
    Serial.println(value);  //do whatever you want with the value
  }

{
 
 for (int pin = 0; pin < 3; pin++)
  {
 
   int dummy = analogRead(inputs A1);  //read values twice as advised
    int value = analogRead(inputs A1);
    Serial.println(value);  //do whatever you want with the value
  }

{
  for (int pin = 0; pin < 3; pin++)
  {
    int dummy = analogRead(inputs A2);  //read values twice as advised
    int value = analogRead(inputs A2);
    Serial.println(value);  //do whatever you want with the value
  }

}

Also I still need to add the line

"float voltage = sensorValue * (5.0 / 1023.0);"

Correct? And do I need to just add it once at the bottom of the code or do I need to add it three times? once with each reading of the pins

bigjoncoop:
Also I still need to add the line

"float voltage = sensorValue * (5.0 / 1023.0);"

Correct?

No, it should be (5.0 / 1024.0)

did I do it correctly?

No

Look at my example program. It uses the pin variable to access successive elements of the inputs array so the analogRead() will successively read A0, A1 and A2

The whole point is to use the for loop to run the same code 3 times without the need to write the code multiple time. To add to the problem you have got the syntax of the analogRead()s wrong

"The problem is that I have no idea how to do any of that."

If you can read one analog port, then you probably can simply copy/paste more making adjustments to the pin numbers. You can just write your code inline instead of looping thru an array. For getting started, it may be easier to follow the logic flow.