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

Basic Analog Input Circuit and Program


  • Please log in to reply
21 replies to this topic

#1 bill.french

bill.french

    Advanced Member

  • Members
  • PipPipPip
  • 260 posts
  • LocationPrinceton, NJ

Posted 09 November 2010 - 01:41 AM

This post covers:
  • a very basic circuit using a potentiometer
  • a program for reading an analog input port

First, the circuit. You'll want to read about voltage dividers to really understand what is happening. The potentiometer acts as one of the resistors in the voltage divider -- and since a potentiometer's resistance changes by turning it, it provides a good source for the analog input. If you are comfortable with Ohms Law (V=IR) the wikipedia article on voltage dividers is pretty approachable in my opinion. Something important in the circuit is the link from 3.3v to the aref pin. Aref is "analog reference" and sets the ceiling of the analog input. The max is 3.3v. I believe the revision B netduino boards have an internal tie in so that using aref is optional.

Posted Image

... and what it might look like on a breadboard:
Posted Image

... and the program:
using System.Text;
using Microsoft.SPOT;
using System.Threading;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;


namespace NDP_SocketSender1
{
    public class Program
    {
        public static void Main()
        {
       
            AnalogInput a5 = new AnalogInput(Pins.GPIO_PIN_A5);

            while (true)
            {
                string s = a5.Read().ToString();
                Debug.Print(s);
                Thread.Sleep(100);
            }
        }
    }
}

Basically, the program opens up an analog input port (a5 in this case), reads the value into a string, and prints the string to the output window in visual studio every 100ms. As you adjust the potentiometer, the values displayed in debug should change, varying from about 0 to about 1023, which are the bounds of the 10-bit ADC (analog-digital coverter) built in to the netduino.

Since I have a Netduino Plus, I have also set it up send the readings over the network and graph them in a Windows Forms Application, so I could see the results visually as I turned the potentiometer to different positions. It's important to realize that digital inputs read either 1 or 0; analog inputs have a range, and the graph from my netduino plus illustrates this:

Posted Image

If you don't have a netduino plus, you can cut and paste the data from the output window in Visual Studio into Excel or a Google spreadsheet and graph from there.

If this is interesting to you, I would encourage you to search the forums for "analog", "analoginput", and "setrange" for lots more useful information.

#2 Frank

Frank

    Advanced Member

  • Members
  • PipPipPip
  • 62 posts
  • LocationNorth Waterboro Maine

Posted 09 November 2010 - 02:58 PM

WOuld really appreciate you showing the Netduino to Windows Form code !
Retired -stone sculptor -macro photographer- novice but avid Go player - Geek

#3 bill.french

bill.french

    Advanced Member

  • Members
  • PipPipPip
  • 260 posts
  • LocationPrinceton, NJ

Posted 09 November 2010 - 06:31 PM

Sure! It's over in the netduino plus forum: http://forums.netdui...er-the-network/

#4 Frank

Frank

    Advanced Member

  • Members
  • PipPipPip
  • 62 posts
  • LocationNorth Waterboro Maine

Posted 09 November 2010 - 08:53 PM

Thank you. I should have looked better before asking !
Retired -stone sculptor -macro photographer- novice but avid Go player - Geek

#5 seano

seano

    New Member

  • Members
  • Pip
  • 1 posts

Posted 16 January 2011 - 12:54 AM

I just tried this today. It was my first non-blinking led project on the netduino. What are the units that are displayed in the output window? It seems like it's the resistance in the potentiometer plus the resistance of the resistor plus a bit of on board resistance. Am I right or is it a coincidence?

#6 VaticanUK

VaticanUK

    New Member

  • Members
  • Pip
  • 1 posts

Posted 25 January 2011 - 02:29 PM

Hi Bill. In your diagram, the input can never be 0 because R2 would always have a voltage applied to it meaning that the input at A5 would always be raised above 0. Wouldn't the attached be better since this would give you a full range of inputs from 0 to 3.3v. Unless I'm missing something? Maybe you can damage the netduino if you apply 0v to the inputs or something??

Attached Files



#7 Arron Chapman

Arron Chapman

    Advanced Member

  • Members
  • PipPipPip
  • 289 posts
  • LocationOregon, USA

Posted 26 January 2011 - 02:16 AM

@bill.french, this is an excellent post. I've been meaning to make some like this myself, but haven't gotten around to it just yet. I hope to see more.

When you talk EE use small words, I'm just a Software Developer :)
My Blog/Site and Everything Else

If my post helped you please consider pressing the "Like This" button in the bottom right-hand corner.

 

Oh my. So many things, so little money!!

 


#8 bill.french

bill.french

    Advanced Member

  • Members
  • PipPipPip
  • 260 posts
  • LocationPrinceton, NJ

Posted 26 January 2011 - 04:36 AM

How did I miss seano's question? Sorry. The units are from 0-1023, because it's a 10bit adc, and 2^10 (minus 1) is 1024. Search the forums for "setrange" for interesting info.

@vaticanuk: yes you are right, but i thought using a voltage divider circuit would be slightly more useful (in my opinion) for the example because you can replace the potentiometer with something else, like a thermistor. In your circuit the potentiometer is the voltage divider.

Either way works for a potentiometer.

#9 capn

capn

    Member

  • Members
  • PipPip
  • 14 posts
  • LocationNC

Posted 11 February 2011 - 12:04 AM

Does using an analog input require that you use the Aref in your circuit? Or could you use an external circuit with a voltage out to the analog input?

Be patient with me, I'm new to this.


#10 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 11 February 2011 - 12:26 AM

Does using an analog input require that you use the Aref in your circuit? Or could you use an external circuit with a voltage out to the analog input?


If you have a "Rev A" Netduino/Netduino Plus, you'll need to wire up the AREF to a 3.3V source. If you have a "Rev B" unit (or a Netduino Mini), 3.3V AREF is built-in and used by default.

Chris

#11 capn

capn

    Member

  • Members
  • PipPip
  • 14 posts
  • LocationNC

Posted 11 February 2011 - 12:40 AM

If you have a "Rev A" Netduino/Netduino Plus, you'll need to wire up the AREF to a 3.3V source. If you have a "Rev B" unit (or a Netduino Mini), 3.3V AREF is built-in and used by default.

Chris


Does AREF have to be 3.3V or could I change it? Also is the function of AREF to compare the voltage of the analog input so it knows how many bits to convert it to? Like 0V at the analog input is 0bits, and 3.3V at the analog input is 1023bits?

Be patient with me, I'm new to this.


#12 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 11 February 2011 - 12:47 AM

Does AREF have to be 3.3V or could I change it? Also is the function of AREF to compare the voltage of the analog input so it knows how many bits to convert it to? Like 0V at the analog input is 0bits, and 3.3V at the analog input is 1023bits?


Hi capn,

AREF can be between 2.7V and 3.3V (using the external AREF pin). It represents the high-end of the analog voltage range. So 0V will always be "0" and the voltage matching AREF will be "1023".

Voltages in between are represented linearly throughout the 0...1023 range (e.g. 1.65V = 511).

Chris

#13 capn

capn

    Member

  • Members
  • PipPip
  • 14 posts
  • LocationNC

Posted 11 February 2011 - 01:22 AM

Hi capn,

AREF can be between 2.7V and 3.3V (using the external AREF pin). It represents the high-end of the analog voltage range. So 0V will always be "0" and the voltage matching AREF will be "1023".

Voltages in between are represented linearly throughout the 0...1023 range (e.g. 1.65V = 511).

Chris



Ok, that's easy enough to understand. This might be for another topic, but if you have an analog voltage that doesn't vary from 0-3.3V, say 1.65-3.3V, can you shift the 0bit to 1.65V and still have the 1023 bit at 3.3V?

Be patient with me, I'm new to this.


#14 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 11 February 2011 - 01:24 AM

Ok, that's easy enough to understand. This might be for another topic, but if you have an analog voltage that doesn't vary from 0-3.3V, say 1.65-3.3V, can you shift the 0bit to 1.65V and still have the 1023 bit at 3.3V?


AnalogInput.SetRange lets you pick a custom range. So if you gave it a range of -1023 to 1023, then 1.65V would be 0.

Chris

#15 Ckiszka

Ckiszka

    Member

  • Members
  • PipPip
  • 14 posts

Posted 26 February 2011 - 07:17 AM

Do you have to use an AREF if your analog sensor is requiring a 5V and you are not using a 3.3V?

#16 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 26 February 2011 - 07:36 AM

Hi Ckiszka,

Do you have to use an AREF if your analog sensor is requiring a 5V and you are not using a 3.3V?


AREF is the measuring stick against which the analog signals are measured. If AREF is 3.3V and the signal is 1.65V, then the signal's value is "50%" of the range (value of ~511 on the default range of 0-1023).

AREF = analog reference voltage

The output voltage of the sensors usually depends on the input voltage given to the sensors. If your sensor inputs 5V and outputs up to 5V, then you will need to bring that voltage down (with a voltage divider, or with a sensor that can take a 3.3V input). Sending more than 3.3V to an analog input (or more than the AREF voltage) can damage your Netduino's microcontroller.

Does that help answer your question?

Chris

#17 Ckiszka

Ckiszka

    Member

  • Members
  • PipPip
  • 14 posts

Posted 26 February 2011 - 08:06 AM

Hi Ckiszka,



AREF is the measuring stick against which the analog signals are measured. If AREF is 3.3V and the signal is 1.65V, then the signal's value is "50%" of the range (value of ~511 on the default range of 0-1023).

AREF = analog reference voltage

The output voltage of the sensors usually depends on the input voltage given to the sensors. If your sensor inputs 5V and outputs up to 5V, then you will need to bring that voltage down (with a voltage divider, or with a sensor that can take a 3.3V input). Sending more than 3.3V to an analog input (or more than the AREF voltage) can damage your Netduino's microcontroller.

Does that help answer your question?

Chris


Thanks for the help. I am new and still fuzzy. I will study this. In the short term, I am using this http://www.phidgets....product_id=1120 . I have it powered by the 5V on my N+. I am using A01 as my input. What do you recommend?

#18 Ckiszka

Ckiszka

    Member

  • Members
  • PipPip
  • 14 posts

Posted 26 February 2011 - 08:27 AM

Thanks for the help. I am new and still fuzzy. I will study this. In the short term, I am using this http://www.phidgets....product_id=1120 . I have it powered by the 5V on my N+. I am using A01 as my input. What do you recommend?


I just put a MultiMeter on the analog line that would plug into A01, it is rading 4.XX V. This is what you were talking about when you said too much coming back and could damage my N+?

That is my only sensor, does this mean I need to implement a voltage divider?

Thanks in advance.

#19 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 26 February 2011 - 08:28 AM

Thanks for the help. I am new and still fuzzy. I will study this. In the short term, I am using this http://www.phidgets....product_id=1120 . I have it powered by the 5V on my N+. I am using A01 as my input. What do you recommend?


I would contact Phidgets and ask them if you can use a 3.3V input. The product specs say that it requires 4.5V to 5.25V input, but the FAQs for FlexForce seems to indicate otherwise.

Alternatively, you can use a voltage divider (using resistors) to convert the 0-5V output to 0-2.5V instead. If you then set your AnalogInput range to 0...1350 using AnalogInput.SetRange(0, 1350), you'll get a value of 0-1023 based on pressure.

Target range calculation (to recalibrate for 2.5V input instead of 3.3V input)
1350 = 1023 x (3.3 / 2.5)

Chris

#20 Ckiszka

Ckiszka

    Member

  • Members
  • PipPip
  • 14 posts

Posted 26 February 2011 - 08:48 AM

I would contact Phidgets and ask them if you can use a 3.3V input. The product specs say that it requires 4.5V to 5.25V input, but the FAQs for FlexForce seems to indicate otherwise.

Alternatively, you can use a voltage divider (using resistors) to convert the 0-5V output to 0-2.5V instead. If you then set your AnalogInput range to 0...1350 using AnalogInput.SetRange(0, 1350), you'll get a value of 0-1023 based on pressure.

Target range calculation (to recalibrate for 2.5V input instead of 3.3V input)
1550 = 1023 x (3.3 / 2.5)

Chris


Extremely helpful, thanks. Last questions… :). I have changed my voltage to 3.3 and added the AREF lead as well. I now measure my input lead at 2.9 to 3.4V. Is that too high for an input line?

If I wanted to use a voltage divider I understand implementing a resister but I do not follow the Max Range Value? Where did you get those numbers 1350, 1023 and 1550? I assume this adjustment is needed to regulate my sensor values because I dropped voltage?




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.