Problem reading two sensors with analogRead()

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Re: Problem reading two sensors with analogRead()

Postby anders_malmberg » Sun Feb 12, 2012 10:04 am

adafruit_support wrote:Without changing any wiring, if you just read sensor 0 or sensor 2, do they give you 17/18 like sensor 1?


Hmm that was strange, I was positive that it should have, but its doesnt!
With only one sensor (tried sensor0) it still gives me rubbish. That worked fine before, but that was before I changed the code and rewired the other sensors though.

This is a picture taken with only 2 sensors connected.

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

Re: Problem reading two sensors with analogRead()

Postby anders_malmberg » Sun Feb 12, 2012 10:22 am

do I need some external powersupply?
Im using only the USB port atm. I reconnected the first sensor and thats working like it should, but as soon as I connect another it starts to play up.

this is a readout with 2 sensors and the voltage display

A18;0.68 V B12;0.62 V
A18;0.68 V B13;0.63 V
A18;0.68 V B12;0.62 V
A18;0.68 V B11;0.61 V
A18;0.68 V B12;0.62 V
A18;0.68 V B11;0.61 V
A18;0.68 V B12;0.62 V
A18;0.68 V B12;0.62 V
A18;0.68 V B12;0.62 V
anders_malmberg
 
Posts: 7
Joined: Sun Feb 12, 2012 8:43 am

Re: Problem reading two sensors with analogRead()

Postby adafruit_support_bill » Sun Feb 12, 2012 12:34 pm

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.
User avatar
adafruit_support_bill
 
Posts: 16083
Joined: Sat Feb 07, 2009 9:11 am

Re: Problem reading two sensors with analogRead()

Postby anders_malmberg » 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 cheap 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()

Postby adafruit_support_bill » 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: 16083
Joined: Sat Feb 07, 2009 9:11 am

Re: Problem reading two sensors with analogRead()

Postby anders_malmberg » 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()

Postby adafruit_support_bill » 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: 16083
Joined: Sat Feb 07, 2009 9:11 am

Re: Problem reading two sensors with analogRead()

Postby anders_malmberg » 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 banned-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()

Postby anders_malmberg » 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()

Postby adafruit_support_bill » 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 cheap 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: 16083
Joined: Sat Feb 07, 2009 9:11 am

Re: Problem reading two sensors with analogRead()

Postby navinl16 » 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()

Postby adafruit_support_bill » 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: 16083
Joined: Sat Feb 07, 2009 9:11 am

Re: Problem reading two sensors with analogRead()

Postby john444 » 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: 411
Joined: Sun Mar 04, 2012 1:42 pm
Location: Claremore, Ok

Re: Problem reading two sensors with analogRead()

Postby navinl16 » 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()

Postby adafruit_support_bill » 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: 16083
Joined: Sat Feb 07, 2009 9:11 am

PreviousNext

Return to Arduino

Who is online

Users browsing this forum: No registered users and 7 guests

Stuff to buy from the Adafruit store and links to product documentation!


New Products [108]

Raspberry Pi[80]
 
FLORA[23]
 
Bunnie Studios[9]
 
FPGA[1]
 
mbed[11]
Arduino[60]
 
NETduino[14]
 
BeagleBone[24]
 
Android[6]
 
XBee[10]
More Dev Boards[31]


 
BoArduino[8]
 
SpokePOV[4]
 
TV-B-Gone[4]
 
MiniPOV[3]
 
SIM reader[3]
 
Microtouch[5]
 
Clocks & Watches[18]
 
Drawdio[4]
 
Brain Machine[1]
 
Game of Life[2]
 
MintyBoost[2]
More DIY Kits[16]


 
MaKey MaKey[3]
 
Tweet-a-Watt[5]
 
Young Engineers[33]
 
Discover Electronics[2]
 
Snap Circuits[4]
 
littleBits[3]
 
Project packs[8]


 
Breakout Boards[34]
LCDs & Displays[48]
Components & Parts[70]
Batteries & Power[49]
EL Wire/Tape/Panel[52]
LEDs[111]
 
Wireless[14]
Cables[62]
 
Lasers[6]
Sensors/Parts[145]
 
Enclosures/Cases[11]
 
Solar[11]
 
RFID / NFC[13]
Prototyping[70]
 
iDevices[13]
Tools[71]
 
Wearables[39]
 
CNC[37]
 
Robotics[29]
 
3D printing[1]
 
Materials[24]


 
Stickers[41]
 
Skill badges[55]
 
Books[25]
 
Circuit Playground[7]
 
Gift Certificates[4]