0

Problem reading two sensors with analogRead()
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Re: Problem reading two sensors with analogRead()

by adafruit_support_bill on Mon Jan 04, 2016 12:58 pm

Please post a photo of the one that doesn't work. And also post the code you are using to read the sensors.

adafruit_support_bill
 
Posts: 72764
Joined: Sat Feb 07, 2009 10:11 am

Re: Problem reading two sensors with analogRead()

by txbobs on Mon Jan 04, 2016 1:19 pm

adafruit_support_bill wrote:Please post a photo of the one that doesn't work. And also post the code you are using to read the sensors.


Sure thing.

Code: Select all | TOGGLE FULL SIZE
 
      analogRead(ADCpin);    // first ADC reading discarded
      delay(20);
      reading = 0;           // read ADC pin 0 10 times
      for (int loop = 0; loop < iterations; loop++)
      {
         reading += analogRead(ADCpin);
         delay(20);
      } 
      analogRead(ADCpin2);
      delay(20);
      reading1 = 0;           // read ADC pin 1 10 times
      for (int loop = 0; loop < iterations; loop++)
      {
         reading1 += analogRead(ADCpin2);
         delay(20);
      }



ADCpin is set to 0 and ADCpin2 is set to 1.
Attachments
metrotemp.jpg
metrotemp.jpg (105.13 KiB) Viewed 828 times

txbobs
 
Posts: 22
Joined: Wed Jan 30, 2013 3:11 pm

Re: Problem reading two sensors with analogRead()

by adafruit_support_bill on Mon Jan 04, 2016 1:27 pm

Add some serial output to your sampling loops and post the Serial Monitor output.

And please post the complete code so we can see what else may be going on there.

adafruit_support_bill
 
Posts: 72764
Joined: Sat Feb 07, 2009 10:11 am

Re: Problem reading two sensors with analogRead()

by txbobs on Mon Jan 04, 2016 1:31 pm

Output from the A0 and A1 readings.

I just added a false read to A2 which is connected to a 5k resistor to ground. Same behavior as when it was reading A0 and A1.

Code: Select all | TOGGLE FULL SIZE
/* LCD Thermometer Sketch for RasPiO Duino 20x4 i2c LCD
Library version:1.1 from robot-r-us.com
Compatible with the Arduino IDE 1.0 and above
*/

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
int ADCpin = 0;                    // declare variables
int ADCpin2 = 1;
int iterations = 10;
float voltage = 3.3;
float voltage1 = 3.3;
int reading = 0;
int reading1 = 0;

// set LCD i2c address to 0x27 and 20 char x 4 line display
LiquidCrystal_I2C lcd(0x27,20,4); 

void setup()
{
  lcd.init();                      // initialize lcd
  lcd.backlight();
  lcd.setCursor(0, 0);             // go to column 0 row 0
  lcd.print("    i2c 20x4 LCD");   // Print txt on LCD
  lcd.setCursor(0, 1);
  lcd.print("     powered by");
  lcd.setCursor(0, 3);
  lcd.print("        Bob");
  delay(5000);
  lcd.clear();
  Serial.begin(9600);
}

void loop()
{
      analogRead(2);    // first ADC reading discarded
      delay(20);
      reading = 0;           // read ADC pin 0 10 times
      for (int loop = 0; loop < iterations; loop++)
      {
         reading += analogRead(ADCpin);
         delay(20);
      } 
      analogRead(2);
      delay(20);
      reading1 = 0;           // read ADC pin 1 10 times
      for (int loop = 0; loop < iterations; loop++)
      {
         reading1 += analogRead(ADCpin2);
         delay(20);
      }

      lcd.setCursor(0, 0);
      lcd.print(reading / iterations);
      lcd.print("    ");     
      lcd.print(reading1 / iterations);     
      lcd.print("    AD raw");

      voltage = reading / 1023.0 / iterations * 3.3;
      voltage1 = reading1 / 1023.0 / iterations * 3.3;       

      lcd.setCursor(0, 1);
      lcd.print(voltage, 3);   // print voltage to 3dp
      lcd.print("  ");
      lcd.print(voltage1, 3);
      lcd.print("  Volts");

      lcd.setCursor(0, 2);
      int temperature = voltage * 100 - 50;
      lcd.print(temperature);
      lcd.print((char)223);    // print degree symbol
      lcd.print("C   ");
      int temperature1 = voltage1 * 100 - 50;     
      lcd.print(temperature1);
      lcd.print((char)223);
      lcd.print("C   ");
     
      Serial.print("A");
      Serial.print(ADCpin);
      Serial.print(" - ");
      Serial.print(analogRead(ADCpin));
      Serial.print("   A");
      Serial.print(ADCpin2);
      Serial.print(" - ");
      Serial.println(analogRead(ADCpin2));
     
      delay(500);
}

txbobs
 
Posts: 22
Joined: Wed Jan 30, 2013 3:11 pm

Re: Problem reading two sensors with analogRead()

by adafruit_support_bill on Mon Jan 04, 2016 1:35 pm

Output from the A0 and A1 readings.

? I don't see the output...

adafruit_support_bill
 
Posts: 72764
Joined: Sat Feb 07, 2009 10:11 am

Re: Problem reading two sensors with analogRead()

by txbobs on Mon Jan 04, 2016 1:46 pm

adafruit_support_bill wrote:
Output from the A0 and A1 readings.

? I don't see the output...


Helps if actually paste it. :)

A0 - 147 A1 - 147
A0 - 147 A1 - 147
A0 - 148 A1 - 147
A0 - 147 A1 - 147
A0 - 147 A1 - 147
A0 - 147 A1 - 147
A0 - 147 A1 - 147
A0 - 147 A1 - 147
A0 - 147 A1 - 147
A0 - 147 A1 - 147
A0 - 147 A1 - 147

I just checked and the voltage coming in from the sensors is around .67 which seems correct but I am calculating .47 for both of them. I am beginning to think it is just a code problem. Still interesting that changing Arduinos makes a difference.
Last edited by txbobs on Mon Jan 04, 2016 1:56 pm, edited 1 time in total.

txbobs
 
Posts: 22
Joined: Wed Jan 30, 2013 3:11 pm

Re: Problem reading two sensors with analogRead()

by adafruit_support_bill on Mon Jan 04, 2016 1:53 pm

Are those the raw readings or calculated results? Not sure about the accuracy, but they do seem pretty stable.,

adafruit_support_bill
 
Posts: 72764
Joined: Sat Feb 07, 2009 10:11 am

Re: Problem reading two sensors with analogRead()

by txbobs on Mon Jan 04, 2016 2:10 pm

adafruit_support_bill wrote:Are those the raw readings or calculated results? Not sure about the accuracy, but they do seem pretty stable.,


They are raw readings. I guess the instability comes only when I had only one sensor connected but was reading both inputs still. Since the open pin was floating, its readings varied greatly. That didn't happen on the raspio though which had me confused.

I guess now I am just left with the puzzle of the two platforms behaving/reporting differently. Thanks for your help, Bill.

txbobs
 
Posts: 22
Joined: Wed Jan 30, 2013 3:11 pm

Re: Problem reading two sensors with analogRead()

by adafruit_support_bill on Mon Jan 04, 2016 2:17 pm

A floating analog pin makes a pretty good random number generator on an Arduino. I'm not familiar with the internals of the RasPiO or what they may be doing differently. One possible difference might be the analog reference voltage. It is 5v by default on most Arduinos, but the Pi is a 3.3v device.

adafruit_support_bill
 
Posts: 72764
Joined: Sat Feb 07, 2009 10:11 am

Re: Problem reading two sensors with analogRead()

by adafruit_support_rick on Mon Jan 04, 2016 2:19 pm

Try the double-read thing. I don't know why the RasPiIO doesn't do it. This is an AVR thing.

adafruit_support_rick
 
Posts: 35095
Joined: Tue Mar 15, 2011 11:42 am
Location: Buffalo, NY

Re: Problem reading two sensors with analogRead()

by txbobs on Mon Jan 04, 2016 3:02 pm

adafruit_support_bill wrote:A floating analog pin makes a pretty good random number generator on an Arduino. I'm not familiar with the internals of the RasPiO or what they may be doing differently. One possible difference might be the analog reference voltage. It is 5v by default on most Arduinos, but the Pi is a 3.3v device.


I am feeding the sensors with the 3.3v line on the Metro. The .7v on the raspio is returning a raw 220 while the Metro only reports the 148.

I decided to see what the extremes are on the metro. Connecting A0 to ground and A1 to 3.3 I get the following.
A0 - 0 A1 - 692
A0 - 0 A1 - 694
A0 - 0 A1 - 693
A0 - 0 A1 - 693
A0 - 0 A1 - 693

Is the Metro ADC expecting 5v? Sure seems like that is the case.

txbobs
 
Posts: 22
Joined: Wed Jan 30, 2013 3:11 pm

Re: Problem reading two sensors with analogRead()

by adafruit_support_bill on Mon Jan 04, 2016 3:18 pm

I am feeding the sensors with the 3.3v line on the Metro.

But the default analog reference is 5v. This can be changed to 3.3v if desired.
https://www.arduino.cc/en/Reference/AnalogReference

adafruit_support_bill
 
Posts: 72764
Joined: Sat Feb 07, 2009 10:11 am

Re: Problem reading two sensors with analogRead()

by txbobs on Mon Jan 04, 2016 4:13 pm

adafruit_support_bill wrote:
I am feeding the sensors with the 3.3v line on the Metro.

But the default analog reference is 5v. This can be changed to 3.3v if desired.
https://www.arduino.cc/en/Reference/AnalogReference


Thanks, Bill. I added a 3.3v connection to the aref pin and added analogReference(EXTERNAL); to the code. Seems to be working now. Thanks for the newbie interruption. :)

txbobs
 
Posts: 22
Joined: Wed Jan 30, 2013 3:11 pm

Re: Problem reading two sensors with analogRead()

by WarthogARJ on Fri Feb 15, 2019 9:37 pm

Is this person still working at Adafruit?
If so, I really hope they were told that this specific response was a very poor one.

The guy you replied to was doing a good job of looking into a problem, but Adafruit Admin was pretty insulting and derogatory in their reply.

The issue is indeed a general one, and this sort of investigation needs to be encouraged.
I was thinking I should support Adafruit due to their general support, but this sort of reply doesn't dosn't do that.


adafruit wrote:
bswift5528 wrote:Yeah, I just finished that up now. Works great! Still don't know why the bandgap reference isn't stable, or why the 1.05 was in the original calculation,


not sure how else we can say that it wasnt stable because it wasnt stable. maybe they didnt want to spend more money on it? regardless, its not in the tutorial anymore because it didnt work well - which means you should probably stop thinking about it :mrgreen:

Still, Sensor 2 seems to be acting up...this time, reading a few deg higher than the other two, and not just being noisy. Has anyone had problems of this nature with a TMP36 before? Thanks again!


electronic temperature sensors are precise to only about 1 or 2 degrees.

WarthogARJ
 
Posts: 2
Joined: Sun Jan 27, 2019 11:51 pm

Please be positive and constructive with your questions and comments.