how do you read analog signal to digital - Netduino Plus 2 (and Netduino Plus 1) - Netduino Forums
   
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

how do you read analog signal to digital


  • Please log in to reply
6 replies to this topic

#1 ofentse temo

ofentse temo

    Member

  • Members
  • PipPip
  • 11 posts

Posted 15 February 2013 - 12:18 PM

ive been trying it for days , i dont seem to be getting it right !!!! need help urgently please



#2 JerseyTechGuy

JerseyTechGuy

    Advanced Member

  • Members
  • PipPipPip
  • 870 posts

Posted 15 February 2013 - 03:14 PM

It's pretty simple... and varies slightly depending on which Netduino you are using due to the resolution.  The ND and ND+ are 10-bit ADC and the ND2 and ND+2 are 12-bit ADC.  Also the convention for creating the port differ.  I put both in the code, just comment and uncomment as necessary based on which Netduino you are using.

 

const double maxVoltage = 3.3;const int maxAdcValue = 1023; // 10-bit Resolution (ADC) change to 4096 for ND+2 as it is 12-bitAnalogInput voltagePort = new AnalogInput(Pins.GPIO_PIN_A1);  // For Standard Netduino//AnalogInput voltagePort = new AnalogInput(AnalogChannels.ANALOG_PIN_A0); // Use for Netduino Plus 2while (true){    int rawValue = voltagePort.Read();    double value = (rawValue * maxVoltage) / maxAdcValue;    Debug.Print(rawValue + " " + value.ToString("f"));    Thread.Sleep(300);}


#3 mohammad

mohammad

    Advanced Member

  • Members
  • PipPipPip
  • 79 posts

Posted 14 March 2013 - 06:02 PM

Hello Dave,

 

I really appreciate you. I read some threads, but none of them couldn't solve my problem.

 

Cheers,

Mohammad



#4 Verdris

Verdris

    Advanced Member

  • Members
  • PipPipPip
  • 128 posts
  • LocationReno, NV

Posted 15 March 2013 - 04:59 PM

Hello Dave,

 

I really appreciate you. I read some threads, but none of them couldn't solve my problem.

 

Cheers,

Mohammad

Could you show us your code and circuit so we can diagnose the issue?



#5 ncsteinb

ncsteinb

    Member

  • Members
  • PipPip
  • 27 posts

Posted 15 March 2013 - 08:45 PM

To really explain how the ADC works in hardware and software, we have to evaluate it in it's units. 

 

Say we have sensor S1, we may think of this as a simple potentiometer. We have GND on one side of the pot, 3.3 on the other side, and our center tap connected to one of our 'AnalogInputs' on our Netduino. Therefore, our analog input will only see between 0v and 3.3v. The ADC or Analog to Digital Converter will take this voltage it sees and scales it. With the Netduino, we have 10bit ADCs, and the Netduino 2 we have 12bit ADCs. This means a 10bit will yield 2^10 or 1024 quantizations, and the 12bit will yield 2^12 or 4096 quantizations. So, if we have a 3.3v vRef, the response for our 10bit ADC will be 3.3v / 1024 quantizations = 0.00293v/ quantization. Likewise, the 12bit ADC will be 3.3v / 4096 quantizations = 0.000805v / quantization. I will give an example using a 10bit ADC. You can extrapolate for use with the 12bit....

 

namespace SensorADC{    public class Program    {        public static void Main()        {            double eqVoltage;   //This will represent derived voltage on the center                                                          tap of the pot.                double transFunc;   //This will represent the real-world value of whatever the sensor is measuring, after you use the transfer function.            AnalogInput sensorInput = new AnalogInput(Pins.GPIO_PIN_A0);    //Declares the type of Input (AnalogInput), the Pin (Pins.GPIO_PIN_A0) the controller is                                                                             //looking at, and what variable (sensorInput) it should dump the value into.             while (true)            {                eqVoltage = sensorInput.Read() * (3.3 / 1023);      //We read the analog input and scale. eqVoltage has units of volts now.                transFunc = 1234 * (eqVoltage - 1.234);     //Most sensors will need a transfer function, this is given in the documentation of the sensor. With a pot, it could be angle...                Debug.Print("My real-world value of this sensor is " + transFunc);      //Display the real-world measured value to the user in the Debug window.                Thread.Sleep(1000);     //Nighty-night for 1 second..            }        }            }}

 

 

I hope this helps you. Here is a good article on the ADC and why we divide by 1023 instead of what you'd think, 1024. 

http://blog.codeblac...d-with-ADC.aspx

 

Sorry about the messy notes.....still not well versed in the art of uploading code to the website....oh well.

 

 

Cheers!

-Nevin



#6 mohammad

mohammad

    Advanced Member

  • Members
  • PipPipPip
  • 79 posts

Posted 23 April 2013 - 10:58 PM

Could you show us your code and circuit so we can diagnose the issue?

 

Hi Verdis,

 

Here is my code which is now working properly:

using System;using Microsoft.SPOT;using Microsoft.SPOT.Hardware;using SecretLabs.NETMF.Hardware;using SecretLabs.NETMF.Hardware.NetduinoPlus;namespace SWEIoT{    public class Sensor:Constants    {        AnalogInput voltagePort;        OutputPort lowPort, highPort;        public Sensor()        {            //Temperature senso           voltagePort = new AnalogInput(AnalogChannels.ANALOG_PIN_A1);           lowPort = new OutputPort(Pins.GPIO_PIN_A0, false);           highPort = new OutputPort(Pins.GPIO_PIN_A2, true);        }        public double read()        {            double rawValue = voltagePort.ReadRaw();            double value = (rawValue * maxVoltage) / maxAdcValue;            double result = (((value - 0.5) * 1000) / 10) - 4;            return result;        }    }}

 

Thanks for your concern,

Mohammad



#7 DaveKR

DaveKR

    New Member

  • Members
  • Pip
  • 4 posts

Posted 27 April 2013 - 03:21 PM

Hi, I just would like to mention that there is another method of reading anolog Inputs if you are using 4.2 and ND2 (+?). You can just set some Properties of the class of type AnologInput and you´re (almost) done with math. (See the ReadScaled method in code) Here´s an example of both methods (ReadingRaw and ReadingScaled):

static AnalogInput voltagePort = new AnalogInput(AnalogChannels.ANALOG_PIN_A0); // Use for Netduino Plus 2//AnalogInput voltagePort = new AnalogInput(Pins.GPIO_PIN_A1);  // For Standard Netduino       public static void Main()        {            while (true)            {                Debug.Print(ReadScaled().ToString("f") + "," + ReadRaw().ToString("f"));                Thread.Sleep(300);            }        }        public static double ReadScaled()        {            const double maxVoltage = 3.3;                          voltagePort.Scale = maxVoltage;            voltagePort.Offset = 0.0;            double voltagevalue = voltagePort.Read();            //double realworldvalue = (((voltagevalue - 0.5) * 1000) / 10) - 4;            return voltagevalue;        }        public static double ReadRaw()        {            const double maxVoltage = 3.3;            const int maxAdcValue = 4095; //12bit AD converter in ND2+            double rawValue = voltagePort.ReadRaw();            double voltagevalue = (rawValue * maxVoltage) / maxAdcValue;            //double realworldvalue = (((voltagevalue - 0.5) * 1000) / 10) - 4;            return voltagevalue;        }





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.