AnalogIn

02 Jul 2011

Hello,

Hardware:

0 to 10K Poti

1 LED

I want to write one program that at a voltage above 2V LED getting on. Is it higher 3V, the LED agetting off.

How i get it?

02 Jul 2011

Based on http://mbed.org/handbook/AnalogIn

#include "mbed.h"

AnalogIn ain(p20);
DigitalOut led(LED1);

int main() {
    while (1){
        if((ain > 2.0) && (ain < 3.0)) {
            led = 1;
        } else {
            led = 0;
        }
    }
}
02 Jul 2011

sorry my failur.

the Voltage is equal by 10 or 30 Ohm.

my goal is to read a resistance via AnalogIN.

if the Resistor 2Ohm or (example) 200 Ohm. LED On or LED off

02 Jul 2011

no problem, you can use ohms law here:

This is the complex but most accurate way:

If you send a fixed current through a resistor the voltage over the resistor will be proportional to the resistance. So if you send 10 mA through a resistor of 300 ohm you will measure a voltage of 0.01 * 300 = 3 Volts With a resistor of 200 ohms it will be 2 Volts etc. etc. All you need is a constant current source.. :-) http://en.wikipedia.org/wiki/Current_source You can make one of a low voltage stabilizer IC, connect a good quality (1%) resistor between the output and the common or adj. pin and put it in series with a power supply. Something like this:

/media/uploads/gertk/350px-lm317_1a_constcurrent.jpg

Replace the 1.25 ohm resistor for a 120 ohm (or even better: a 125 ohm) and the current will be a constant 0.01A

The resistor you want to read out is connected to the output and ground, the voltage can then be measured with the mbed.

A simpler solution is to connect a known resistor to the +3.3 Volt output of the mbed module and connect your unknown resistor in series to ground. The junction in between is connected to an analog input of the mbed. But then you need to create a table or calculation for the resistance to voltage ratio.

http://en.wikipedia.org/wiki/Voltage_divider /media/uploads/gertk/resistive_divider.png

02 Jul 2011

the code in your post is it correct?

or have i a failur?

my voltmeter say 2,5 v

led doesen`t going on damm´t

02 Jul 2011

but THANK YOU FOR YOUR HELP :-)

02 Jul 2011

I think the constants should be 2/3.3 ~ 0.6 and 3/3.3 ~ 0.9

03 Jul 2011

code is based on the handbook page, see the link above

03 Jul 2011

I think Ad van der Weiden is correct. You need to normalize the constants. The AnalogIn method being used here returns a float value between 0.0f and 1.0f: where 0.0f represents 0V and 1.0f represents 3.3V.

#include "mbed.h"

AnalogIn ain(p20);
DigitalOut led(LED1);

int main() 
{
    while (1)
    {
        if((ain > (2.0f / 3.3f)) && (ain < (3.0f / 3.3f))) 
        {
            led = 1;
        } else 
        {
            led = 0;
        }
    }
}
03 Jul 2011

Yep, seems correct:

The 0.0v to 3.3v range of the AnalogIn is represented in software as a normalised floating point number from 0.0 to 1.0