Reading multiple analogue inputs and printing values

Hello,

i have been trying to read two sensor values and printing them to the serial monitor.

they are A0 and A1, an LM35 on pin 1 and a MQ7 gas sensor on pin 0.
I wnt to serial monitor to read /Pin1= Temperature / Pin0 = Carbon Monoxide.

Could anyone help me with the code? i can rad one at a time but I'm not any closer to reading two inputs as of yet.

i also wish to integrate Blynk into this so I can print the blynk virtual write on these pins so I can get them in the app.

Hi, @anon69033363
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".

We need to see your circuit diagram and your code so we can give you an answer.

What model Arduino are you using?

Thanks. .. Tom.. :smiley: :+1: :coffee: :australia:

Please post your best efforts at reading the 2 inputs

Please follow the advice given in the link below when posting code , use code tags when posting code here to make it easier to read and copy for examination

The line of code that you posted below the line where you do the thing, is wrong.

What code would you like help with?

The best would have been to post the code you have tried, with your original post, in code tags. Then write about what your code is supposed to do and what you code is doing.

Hi my code is not good so i was hoping for a fresh code.
i have inserted the code below:

void setup()

{

Serial.begin(9600);//Set Baud Rate to 9600 bps

}

void loop()

{

uint16_t val;

double dat;

val=analogRead(0);//Connect LM35 on Analog 0

dat = (double) val * (5/10.24);

Serial.print("Tep:"); //Display the temperature on Serial monitor

Serial.print(dat);

Serial.println("C");

delay(500);

int val;

val=analogRead(1);//Read Gas value from analog 0

Serial.println(val,DEC);//Print the value to serial port

delay(100);

}

What do you see on the Serial monitor when you run your sketch ?

Hi,
To add code please click this link;

Does your code actually compile.

You have ;

uint16_t val;

and

int val;

It won't compile.
What model Arduino are you using?

Thanks.. Tom.... :smiley: :+1: :coffee: :australia:

no it wont compile.
i can get one sensor working but not the second one.
i know there is a confusion on the val

im using a dfrobot version of the uno with an IO shield on it.

this is the serial monitors error:
C:\Users\Stuart\Documents\Arduino\sketch_aug06b\sketch_aug06b.ino: In function 'void loop()':
sketch_aug06b:17:7: error: conflicting declaration 'int val'
int val;
^~~
C:\Users\Stuart\Documents\Arduino\sketch_aug06b\sketch_aug06b.ino:8:14: note: previous declaration as 'uint16_t val'
uint16_t val;
^~~
exit status 1
conflicting declaration 'int val'

Hi,
Try this edited code.

uint16_t val;
double dat;
byte TempPin = A0;
byte GasPin = A1;



void setup()
{
  Serial.begin(9600);//Set Baud Rate to 9600 bps
  Serial.println("Setup complete");
}

void loop()
{

  val = analogRead(TempPin); //Connect LM35 on Analog 0
  val = analogRead(TempPin); // Read twice to let ADC read selected channel
  dat = (double) val * (5 / 10.24);
  Serial.print("Temp:"); //Display the temperature on Serial monitor
  Serial.print(dat);
  Serial.println("C");

  delay(500);

  val = analogRead(GasPin); //Read Gas value from analog 0
  val = analogRead(GasPin); // Read twice to let ADC read selected channel
  Serial.println(val, DEC); //Print the value to serial port

  delay(100);
}

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

As @TomGeorge has pointed out, you have val declared twice. Remove the declaration of one of them

Hi,
The analog pin is read twice because there is only ONE ADC and it is switched from channel to channel.
The ADC circuit has an input capacitor that has to charge to the input voltage, this takes a little time, reading twice makes sure that the second read has allowed the capacitor and hence the ADC to acquire the new input voltage.

Tom... :smiley: :+1: :coffee: :australia:

Wow its reading the two inputs thank you!

i really want to learn this coding... i can create crazy excel formulas but then looking at this code im gobsmacked...

Thank you again for the help...

1 Like

Do you understand what the problem was ?

i think so, it was a duplicated "Function or reference" i dont know what to call them but it was the val i had duplicated on each pin.

where do you get the handbook listing all available functions? or rather a dummies guide to setting up code?

You can find details of all of the Arduino specific functions in Arduino Reference - Arduino Reference but as the Arduino runs C++ then any C++ reference will give you details of more general functionality

OH MY WORD!!!

THANK YOU

ive been looking for something like this seriously thank you

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