Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

AnalogInput and Netduino Plus 2


  • Please log in to reply
9 replies to this topic

#1 mrDraft

mrDraft

    New Member

  • Members
  • Pip
  • 2 posts

Posted 20 December 2012 - 12:24 PM

Hi All,

I have a Netduino Plus 2 board and I would like to use one of its analog input to read value from a temperature sensor.
For my tests, I connect the pin A0 to ground then to +3.3V and I read the value, it's always something between 0.2xxV and 0.7xxV.

The code U use is:
Microsoft.SPOT.Hardware.AnalogInput pinA0 = new Microsoft.SPOT.Hardware.AnalogInput(Cpu.AnalogChannel.ANALOG_0);
double temperature = pinA0.Read();
I guess the pin referenced by
Cpu.AnalogChannel.ANALOG_0
is not the good one (meaning wiring).

I also tried this to define the pin as:
SecretLabs.NETMF.Hardware.AnalogInput pinA0 = new SecretLabs.NETMF.Hardware.AnalogInput(Pins.GPIO_PIN_A0);
int temperature = pinA0.Read();
But then I get a runtime exception:
An unhandled exception of type 'System.ArgumentException' occurred in Microsoft.SPOT.Hardware.dll
on the pin's definition line...

If I continue executing the code untill I read the pin, I see that pinA0 is "null".

Any idea on how to correctly define the A0 pin please?

Thanks!

#2 NooM

NooM

    Advanced Member

  • Members
  • PipPipPip
  • 490 posts
  • LocationAustria

Posted 20 December 2012 - 01:47 PM

read doesent return the actual voltage, it returns the voltage in percent, where 0.5 = 50% = 1.65v, 1.0f = 100% = 3.3v when you connect it it should read 1.0f (or something like 0.999f). when you multiply the read value with 3.3 you get the voltage. (microsoft class, id never used the sl one)

#3 JerseyTechGuy

JerseyTechGuy

    Advanced Member

  • Members
  • PipPipPip
  • 870 posts

Posted 20 December 2012 - 02:19 PM

On the plus 2 it is: (AnalogChannels not AnalogChannel)

AnalogInput pinA0 = new AnalogInput(AnalogChannels.ANALOG_PIN_A0);

Noom is correct that it returns a percentage of the input signal. So you can do something like this below to get the raw value, the floating percentage and a double range fro 0-1023.

double maxVoltage = 3.3;
int maxAdcValue = 4096; // ADC is 12-bit Resolution

int rawValue = pinA0.Read();
float aValue (raw * maxVoltage);
double value = aValue / maxAdcValue;
Debug.Print(rawValue + " " + value.ToString("f"));

Edited by Dave VanderWekke, 20 December 2012 - 06:28 PM.


#4 mrDraft

mrDraft

    New Member

  • Members
  • Pip
  • 2 posts

Posted 20 December 2012 - 02:48 PM

Hi Guys,

Thank you both for your advices, now it's working like it should!

I was not reading the analog value correctly...

To summarize:

1. I defined my pin as Dave mentionned:

AnalogInput pinA0 = new AnalogInput(AnalogChannels.ANALOG_PIN_A0);

2. I multiplied the value read by 3.3 as NooM mentionned

when you multiply the read value with 3.3 you get the voltage.


For my temperature sensor (LM35), I need to get a voltage representing the temperature (1°C=10mV), so I just take the output of pinA0, multiply it by 330 and I get a double that represents my temperature:
_temperature = pinA0.Read();
_temperature = _temperature * 330F;

I was thinking it was a pin mapping issue at first because I already had problems with that, but it ain't...

Thanks again for your help!

Cheers

#5 JerseyTechGuy

JerseyTechGuy

    Advanced Member

  • Members
  • PipPipPip
  • 870 posts

Posted 20 December 2012 - 02:56 PM

Glad it is working. Happy Programming!

#6 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 20 December 2012 - 03:05 PM

Perhaps you could also use AnalogInput.Scale property (or the constructor parameter).

#7 carb

carb

    Advanced Member

  • Members
  • PipPipPip
  • 352 posts
  • LocationCrystal River, Florida

Posted 20 December 2012 - 04:20 PM

On the plus 2 it is: (AnalogChannels not AnalogChannel)
int maxAdcValue = 1023; // ADC is 10-bit Resolution

Dave,
On the NP2 it has a 12 bit ADC, ie. 4095 instead for the ReadRaw.

#8 JerseyTechGuy

JerseyTechGuy

    Advanced Member

  • Members
  • PipPipPip
  • 870 posts

Posted 20 December 2012 - 06:25 PM

Dave,
On the NP2 it has a 12 bit ADC, ie. 4095 instead for the ReadRaw.


Yep, you are right. Guess I'm still stuck in NDP1 mode. :huh:

#9 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 20 December 2012 - 07:31 PM

Oh noes! It's more accurate! :D
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#10 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 20 December 2012 - 07:51 PM

Oh noes! It's more accurate! :D

Nitpicking corner: The ADCs have almost the same accuracy, but different resolution Posted Image Sorry, could not resist.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.