First AnalogInput Read not Reliable
#1
Posted 03 September 2012 - 06:04 PM
I am working in a very simple temperature reading project and I am experiencing some strange behavior during the first Analog input read.
To reproduce what I think it's strange, I created a simple code using Netduino Plus running firmware 4.2.0 (the code snippet on this forum editor does not work for me):
public static void Main()
{
AnalogInput adc = new AnalogInput(Cpu.AnalogChannel.ANALOG_0);
System.Threading.Thread.Sleep(5000);
double firstRead = adc.Read();
double secondRead = adc.Read();
while (true)
{
Debug.Print("First Read: " + firstRead.ToString());
Debug.Print("Second Read: " + secondRead.ToString());
System.Threading.Thread.Sleep(10000);
}
}
The while is only used to make sure I won't loose the messages when connecting to MFDeploy.
When I run this code using the Visual Studio debugger, first and second reads are very similar. No problem there. However, when I:
1 - Stop Debugging
2 - Unplug Netduino Plus
3 - Open MFDeploy
4 - Plug Netduino ON again (using the usb cable)
I have the following results:
First Read: 0.10166177908113393
Second Read: 0.078201368523949169
As you can see the first read always have a higher value than the second one. In my real project, this means the first temperature reading is always much higher then reality.
I included a 5 second delay before reading, since I was expecting there was some "port initialization" but this seems not to be the case, so you can get rid of it that the results are the same.
The most annoying thing is that when debugging in Visual Studio, I cannot see the behavior (It took me some time to figure out this).
Any thoughts about why am I seeing this? Any tests I could make?
Thank you,
Igor.
Igor Kondrasovas
www.inovativatec.com
#2
Posted 03 September 2012 - 06:33 PM
#3
Posted 03 September 2012 - 08:36 PM
Hi Igor,
For best results with an on-chip ADC, you will generally want to take a few readings and average them together.
This is particularly important on Netduino Plus, where the Ethernet can draw sudden current draws fluctuating the voltage on the 3V3 line a bit.
Also...what happens if you put a 100uF cap between the 3V3 and GND header pins?
Chris
Hi Chris,
I am aware that Analog inputs are tricky and we should use either hardware and/or software filtering. However, the most intriguing question is why do I have such erroneous value when the debugger is not attached?
In both cases I am powering up the device using the USB cable and no other power supplies.
I was suspecting that maybe when I do not have debugger attached, the boot could be quicker and I could potentially be reading a peak or something like this. That is why I put the 5 second delay before the first read. But this did not produce any difference.
So, filtering or ignoring the first Read call seems to solve the problem (so we have a workaround), however I would like to investigate what is happening, so we wouldn't have a "cargo cult" about this issue!
Any thoughts?
Igor.
Igor Kondrasovas
www.inovativatec.com
#4
Posted 03 September 2012 - 08:51 PM
We're pulling the ADC values directly from the micro.Any thoughts?
How long are you delaying between creating the AnalogInput object and taking the first reading?
Chris
#5
Posted 03 September 2012 - 08:55 PM
Hi Igor,
We're pulling the ADC values directly from the micro.
How long are you delaying between creating the AnalogInput object and taking the first reading?
Chris
Hi Chris,
As you can see in my code in the beginning of the code, I wait 5 seconds.
Please note I can reproduce this consistently, if there is anything I can test, please let m know!
Thanks
Igor Kondrasovas
www.inovativatec.com
#6
Posted 03 September 2012 - 08:58 PM
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users