Re: Problem reading two sensors with analogRead()

by anders_malmberg on Sun Feb 12, 2012 2:21 pm

adafruit_support wrote:
Hmm that was strange, I was positive that it should have, but its doesnt!

Is it stable at least? (i.e. consistently the same error offset?)

Double check all your wiring & keep all your jumpers short.



Ok sitrep.
One idea I had was that the banned lab cables I used to connect the sensors had a weak connection since the values changed when i touched the cables. To get rid of this problem I soldered the sensors to 0,75mm2 cables and connected them to a screwshield. However now I get even stranger results. With only one sensor connected its alright, a bit low on output voltage but ok. With two sensors its banned, 0.00 V and 0.52 V output. I think it might be the resistance in the cables. The voltage to the sensors is like it should be, but result-voltage (yellow wire) is way to low.

I will need to use long (about 4meters) cables, can I add some extra juice with external power to solve this problem?

ImageImageImage
Image
anders_malmberg
 
Posts: 7
Joined: Sun Feb 12, 2012 8:43 am

Re: Problem reading two sensors with analogRead()

by adafruit_support_bill on Sun Feb 12, 2012 3:04 pm

The resistance of the lead wires is not going to be a major issue since the currents involved are so low. However, noise can be a problem - especially with longer wires. Values changing when you touch the wires is consistent with noise sensitivity.

It is difficult to know the nature of the noise without looking at it with a scope. But there are a couple simple things you can do to minimize it:

1. If you have to have long wires, use twisted and/or shielded wires for your connections.
2. Implement a low-pass filter in software. This can be as easy as taking the average of some number of samples. (on the Arduino, it is best to throw away the first sample in the series.)
User avatar
adafruit_support_bill
 
Posts: 25412
Joined: Sat Feb 07, 2009 9:11 am

Re: Problem reading two sensors with analogRead()

by anders_malmberg on Sun Feb 12, 2012 3:45 pm

adafruit_support wrote:The resistance of the lead wires is not going to be a major issue since the currents involved are so low. However, noise can be a problem - especially with longer wires. Values changing when you touch the wires is consistent with noise sensitivity.

It is difficult to know the nature of the noise without looking at it with a scope. But there are a couple simple things you can do to minimize it:

1. If you have to have long wires, use twisted and/or shielded wires for your connections.
2. Implement a low-pass filter in software. This can be as easy as taking the average of some number of samples. (on the Arduino, it is best to throw away the first sample in the series.)


Yea the current is quite low so I guess your right. I still get zero to nothing in return-voltage from the sensors though. What could it be? Im using an old Dell laptop to run the arduino, maybe the power from that usb port is too weak. Since im using Visual Basic to display the temps I need to have a usb connected, can I use external power at the same time?
Last edited by anders_malmberg on Sun Feb 12, 2012 4:55 pm, edited 1 time in total.
anders_malmberg
 
Posts: 7
Joined: Sun Feb 12, 2012 8:43 am

Re: Problem reading two sensors with analogRead()

by adafruit_support_bill on Sun Feb 12, 2012 3:59 pm

A separate analog reference voltage is a good idea. Battery power is typically very clean. You would need to re-scale your readings to the battery voltage:http://arduino.cc/it/Reference/AnalogReference
User avatar
adafruit_support_bill
 
Posts: 25412
Joined: Sat Feb 07, 2009 9:11 am

Re: Problem reading two sensors with analogRead()

by anders_malmberg on Mon Feb 13, 2012 3:41 pm

adafruit_support wrote:A separate analog reference voltage is a good idea. Battery power is typically very clean. You would need to re-scale your readings to the battery voltage:http://arduino.cc/it/Reference/AnalogReference


Shield? Check!

I crafted some shielding for one of the sensors and twisted the wires to see what happend, and it made wonders. I also disconnected the plint I used to connect multible sensors (the screwshield can only fit one cable in the ground/5V connection). It seems that the cables are extremely sensitive, is there any other sensors that I can use that isnt that sensitive for noise? Im gonna get a shielded cable (that isnt made from kitchen-supplies) and see what happens, I hope that it will put a end on this problem.

Do I have to shield the 3 wires one by one (3x 1-wire cable), or can I get a 3-wire cable (with the 3 wires inside the same shield). Or is it even possible to use only a shielded wire for the sensor (yellow) bit? Shielded cables is quite costly :(

Image

Thanks for all your help! :D
anders_malmberg
 
Posts: 7
Joined: Sun Feb 12, 2012 8:43 am

Re: Problem reading two sensors with analogRead()

by anders_malmberg on Mon Feb 13, 2012 4:12 pm

How about these? Are these better in terms of noise-sensetivity?

http://www.adafruit.com/products/372
anders_malmberg
 
Posts: 7
Joined: Sun Feb 12, 2012 8:43 am

Re: Problem reading two sensors with analogRead()

by adafruit_support_bill on Mon Feb 13, 2012 5:33 pm

Nice looking shield :D

You can get bulk shielded microphone cable for < $0.25/ft. Cat-5 Ethernet cable has 4 twisted pairs in it and is pretty banned also.

Digital sensors, such as the DS18B20 are less susceptible to noise - but a little trickier to work with. http://www.adafruit.com/products/374
User avatar
adafruit_support_bill
 
Posts: 25412
Joined: Sat Feb 07, 2009 9:11 am

Re: Problem reading two sensors with analogRead()

by navinl16 on Thu Aug 09, 2012 1:12 pm

I seem to be having the same problem. I have however used the delays as specified. But my temperature readings are erratic and not accurate and my outputs aren't correct unless the temperature sensor is disconnected or comment out the analog read. I am using an arduino mega 2560 with 4 led's as outputs, and as inputs i have 2 rain sensors, 1 light sensor and a temperature sensor. Any help would be greatly appreciated. The code used is shown below.

[Edit - moderator - use "Code" button when submitting code]

Code: Select all
//Program used for arduino in the automated Cocoa House


/*

   Declaring of Variables
*/
int rainsensor1 = A2;    //rain sensor1 input
int rainsensor2 = A3;    //rain sensor2 input
int lightsensor = A0;    //light sensor input
int tempsensor = A11;     //temperature sensor input
int closingroof = 22;    //Output to close roof
int openingroof = 24;    //Output to open roof
int turningbeans = 26;   //Output to turn beans
int heater = 28;         //Output to turn on heater   
int rainsensor1val = 0;  //storing the value of rainsensor1
int rainsensor2val = 0;  //storing the value of rainsensor2
int lightsensorval = 0;   //storing the value of the light sensor
int tempsensorval = 0;    //storing the value of the temperature sensor
float voltage = 0;        //volate calculated from output of temperature sensor
float temperature = 0;    //temperature calculated with voltage converted from temperature sensor 



void setup()
{
Serial.begin(9600);
pinMode (rainsensor1,INPUT);          //setting rain sensor 1 as an input
pinMode (rainsensor2,INPUT);          //setting rainsensor 2 as an input 
pinMode (lightsensor,INPUT);          //setting the light sensor as an input
pinMode (tempsensor,INPUT);           //setting the temperature sensor as an input
pinMode (closingroof,OUTPUT);         //setting an output used to close roof
pinMode (openingroof,OUTPUT);         //setting an output used to open roof
pinMode (turningbeans,OUTPUT);        //setting an output to turn beans for 5 minutes
pinMode (heater, OUTPUT);             //setting an output to turn the heater on
}


void loop()
{
rainsensor1val=analogRead(rainsensor1);       //storing of rainsensors, light sensor and temperature sensor inputs
rainsensor2val=analogRead(rainsensor2);
analogRead(lightsensor);
delay(10);
lightsensorval=analogRead(lightsensor);
analogRead(tempsensor);
delay(10);
tempsensorval=analogRead(tempsensor);
voltage = tempsensorval * (5000/1024);        //calculating volate from input of temperature sensor
temperature = (voltage / 10);          //calculating temperature from the voltage calculated

Serial.println("First Rainsensor value = ");
Serial.println(rainsensor1val);
Serial.println("Second Rainsensor value = ");
Serial.println(rainsensor2val);                         //Output to screen values of sensors
Serial.println("Light Sensor value = ");
Serial.println(lightsensorval);
Serial.println("Temperature in Celcius = ");
Serial.println(temperature);
//delay(3000);

if (rainsensor1val <= 200 || rainsensor2val <= 200 || lightsensorval <= 300)
{
digitalWrite (closingroof,HIGH);
digitalWrite (openingroof,LOW);
digitalWrite (heater,HIGH);
digitalWrite (turningbeans,HIGH);               //causes roof to close, heater to come on and turning of beans for 5 minutes if conditions met
}
else if (rainsensor1val > 200 && rainsensor2val > 200 && lightsensorval > 300)
{
digitalWrite (openingroof,HIGH);
digitalWrite (closingroof,LOW);
digitalWrite (heater,LOW);                      //causes roof to open, heater to come off and turning of beans for 5 minutes if conditions met
digitalWrite (turningbeans,HIGH);
                   
}

}
navinl16
 
Posts: 2
Joined: Thu Aug 09, 2012 1:00 pm

Re: Problem reading two sensors with analogRead()

by adafruit_support_bill on Thu Aug 09, 2012 2:02 pm

Could be noise picked up in the wiring. How do you have the sensor connected? You could also have problems with the stability of your voltage reference. Try using 3.3v instead of 5v as suggested in the TMP-36 tutorial.
User avatar
adafruit_support_bill
 
Posts: 25412
Joined: Sat Feb 07, 2009 9:11 am

Re: Problem reading two sensors with analogRead()

by john444 on Thu Aug 09, 2012 10:32 pm

Hi Anders & Navin,

The TMP36 data-sheet indicates it supplies only 50-uA.
In addition, it is unstable with a capacitive loads over .01-uF.
It is not surprising that long leads introduce noise causing unstable readings.

Here are some ways to mitigate noise and stabilize the output of the TMP36:
Twisted pair wiring (you need to use 1-pair for power another pair for signal & gnd)
You can also place a 0.1-uF ceramic cap across the power leads on the TMP36.
The data sheet indicates a 2.2-uF tantalum would also not hurt.
At the TMP36's output terminal - place a 100-ohm series resistor & a 0.1-uF cap to gnd.
Using a 0.1-uF (or larger) cap at the Arduino's analog input will also help.
Although, it will slow the rate of change of the indicated temp.

Without the series resistor on the output, shielded wire is likely to make the TMP36 oscillate.

I would suggest you try some or all of the above until you get good results.
PM me if you would like additional recommendations.

Good Luck, John
john444
 
Posts: 443
Joined: Sun Mar 04, 2012 1:42 pm
Location: Claremore, Ok

Re: Problem reading two sensors with analogRead()

by navinl16 on Sun Aug 12, 2012 11:14 am

The temperature readings are way off but my main concern at the moment is that my ckt won't work once i use both sensors together(photocell and temp sensor). I tried multiple reads and delays and it they still won't work together. If they're used alone, the results are stable and work each work normal, but together nothing works. Fatlibb suggested that the temp sensors impedence is too high. Is there any way i can still use this temp sensor and add something in my coding for the both to work simultaneously or would it be best to get me another temp sensor.?
navinl16
 
Posts: 2
Joined: Thu Aug 09, 2012 1:00 pm

Re: Problem reading two sensors with analogRead()

by adafruit_support_bill on Sun Aug 12, 2012 12:02 pm

You haven't told us how you have this connected. You could very well be picking up noise in the wiring. And as John444 says, excess capacitance in the wiring can make it unstable too.
User avatar
adafruit_support_bill
 
Posts: 25412
Joined: Sat Feb 07, 2009 9:11 am

Re: Problem reading two sensors with analogRead()

by physicsonion on Wed Nov 14, 2012 9:43 pm

My $0.05 solution to this problem is as following. As people earlier in the thread have indicated the arduino itself has a multiplexer and only one ADC. Problem occurs when flipping between different voltage sources that they introduce transient which takes a while to disappear. Source of the transient is the input capacitance of the ADC and its huge input resistance (hence the large time constant).

This said, of 6 input channels of arduino, I take one - say A0 - and through a 5k resistor I ground it. Then, before I try to read any other analog channels, I first connect the ADC to the ground by reading from A0.

!!Code snippet
analogRead(A0); // dummy read to discharge ADC
delay (20); // wait a little
analogRead(A1); // now hook up ADC to the channel we really want to read
delay (20); // doze a little
temp = 100*(analogRead(A1)*5/1025 -0.5); // now read once more
!!End of code snippet

With this approach I was able to read 4 additional analog inputs - in my case these were 4 x TMP36 temperature sensors at rate 50, or so, milliseconds per sensor.

Hope this helps,
PhysicsOnion
physicsonion
 
Posts: 2
Joined: Wed Jan 04, 2012 10:10 am

Re: Problem reading two sensors with analogRead()

by gregario on Fri Nov 16, 2012 6:53 am

Hi All,

I'm having a similar problem. I'm running a photoresistor and a TMP36 and logging the data on an arduino.
The temperature sensor is fine but the photoresistor is very noisy. Any tips?

Details, the photoresistor is in a voltage divider with a 1K resistor (the lower resistor is there to increase the range).
I've tried the tricks suggested and then some, here's an excerpt of code for example:

Code: Select all
  analogRead(gndPin);
  delay(100);
  analogRead(photocellPin);
  delay(100);
  int photocellReading = analogRead(photocellPin); 
  delay(100);
  analogRead(gndPin);
  delay(100);
  analogRead(tempPin);
  delay(100);
  int tempReading = analogRead(tempPin);   
  delay(100);
  // converting that reading to voltage, for 3.3v arduino use 3.3, for 5.0, use 5.0
  float voltage = tempReading * aref_voltage / 1024;
  float temperatureC = (voltage - 0.5) * 100;
  float voltage1 = photocellReading * aref_voltage / 1024;
  // float temperatureF = (temperatureC * 9 / 5) + 32;
  //Converts Photocell Reading to Lux Reading, This will be unique for each Sensor and will change between diodes/Photoresistors
  // Sensor 12 (C) = y = 626.06x6 - 3395.4x5 + 7395.5x4 - 7264.1x3 + 3743.7x2 - 148.26x
  double luxReading = ((626.06*(pow(voltage1,6)) -(3395.4*(pow(voltage1,5))) + (7195.5*(pow(voltage1,4))) - (7264.1*(pow(voltage1,3))) + (3743.7*(pow(voltage1,2))) - (148.26*voltage1)));


The ground pin is a 5K resistor tied to ground to try and flush the ADC cap between measurements. The 6th order polynomial is my attempt to get an accurate lux measurement from one of these sensors. (I calibrated using an OLED taped to the top of a dremel tube and a lux meter)

Here's an output under a controlled light source
Code: Select all
Seconds (S)   Light (10 bit)   Light (lux)
1   244   544.59
2   265   607.21
3   275   638.49
4   251   565.03
5   245   547.49
6   276   641.67
7   256   579.89
8   247   553.3
9   276   641.67
10   272   629
11   254   573.92
12   253   570.95
13   276   641.67
14   254   573.92
15   245   547.49
16   271   625.86
17   276   641.67
18   248   556.22
19   250   562.09
20   275   638.49


The measurements quoted above under fluorescent, I must dig out the response times of these sensors in case its light flicker I'm seeing...

Any help would be greatly appreciated guys, micro's are not my strong point!
gregario
 
Posts: 3
Joined: Fri Jun 11, 2010 8:18 am

Re: Problem reading two sensors with analogRead()

by adafruit_support_bill on Fri Nov 16, 2012 10:34 am

CdS cells are fairly slow, but the fluorescent flicker could be part of the problem. You can minimize the 60Hz flicker and other noise as well by taking the average of multiple samples over some multiple of 16ms. If you are only logging once per second, you have plenty of time to take multiple samples.
User avatar
adafruit_support_bill
 
Posts: 25412
Joined: Sat Feb 07, 2009 9:11 am