analog input = digital output

Hi Guys,

I want to program this:
when analog input has (around) 5V then digital output = HIGH , and this is connected to 330R + LED
when analog input is 0V then digital output = LOW, so no led burning.

I typed this:

int myPin=A0;
int redPin=7;

// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
pinMode (redPin,OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
// print out the value you read:
Serial.println(voltage);
delay(250);
if (myPin>5./1023.){
digitalWrite (redPin,HIGH);
}
else if (myPin<1./1023.){
digitalWrite (redPin,LOW);
}
delay(250);
}

This doesnt work, the LED keeps on shining.
Anyone idea ?
im totally noob with this, but atleast i've tried

Thanks already!

if (myPin>5./1023.){ Almost sure Dead cert to be true.

Please remember to use code tags when posting code

You don't need to convert the reading to a voltage, just decide what value of the A2D represents the boundary between LED off and LED on and test for that.

Thanks for answer,
Could you like type it in code language? ^^

Try comparing the voltage, not the pin number you read it from.

in code language ?

IGYIB DRGQW PKDYU NCHKT AXRHL QIUTI PGUIK SETGC KTREW MGGLY

1. When you say input voltage is around 5V, then let us take it as 3.0 V (as per ATMega328P data sheets, it is the minimum value that UNO/MCU will accept as Logic High).

2. When you say input voltage is 0V, then let us take it as 1.5 V (as per ATmega328P data sheets, it is the maximum value that UNO/MCU will accept as Logic Low).

3. Other values in-between 1.5 V and 3.0 V would be considered lying in the forbidden zone.

4. Now, you should have the following comparison codes in your sketch at the appropriate place.

if(voltage >= 3.0)
{
    digitalWrite(redPin, HIGH);
}  

if(voltage <= 1.5)
{
    digitalWrite(redPin, LOW);
}
if (myPin>5./1023.){

myPin equals A0 which, if you print it, equals 14 on an Uno. So that will never always evaluate to true.

Maybe you want to compare the voltage with a certain value that is around 5V (not 5V).

sterretje:

if (myPin>5./1023.){

myPin equals A0 which, if you print it, equals 14 on an Uno. So that will never ALWAYS evaluate to true.

OOPS, thanks for the correction. Will correct the reply.

joepl10:
in code language ?

Here is the Arduino reference to give you the "code language"

Arduino Reference

joepl10:
Thanks for answer,
Could you like type it in code language? ^^

Hi joepl10,
Sorry, no, I can't type it in 'code language'. What you want to do is simple and there are plenty of tutorials on this web site and in the IDE to help you. Please study the available teaching material and try to do this yourself.

If you are struggling with this then you need to go back a few steps and learn the basics, then try again.

Thank you.

I want to program this:
when analog input has (around) 5V then digital output = HIGH , and this is connected to 330R + LED
when analog input is 0V then digital output = LOW, so no led burning.

That's exactly how a digital input works. :wink:

See GolamMostafa's post above and see the [u]Digital Read Serial Example[/u].

But you could do something like this:

if (analogRead(A0) > 512)

Or you can convert that to an actual voltage, but the processor doesn't care if you convert it to volts or if you just use the raw number that's proportional to voltage.

You can let the compiler do the math:

const int THRESHOLD = 1024 * 1 / 2; // set threshold to half scale
if (analogRead(A0) > THRESHOLD)...

aarg:
You can let the compiler do the math:

const int THRESHOLD = 1024 * 1 / 2; // set threshold to half scale

if (analogRead(A0) > THRESHOLD)...

THRESHOLD turns to be 512, which when converted to voltage (with FS = 5 V) appears to be 2.5 V. Do you want to turn ON the LED when the input voltage is > 2.5 V? But, OP wants to turn ON the LED when the input voltage is around 5V.

Is it 2.5 V or 3.0 V that is close to 5V (Logic High) in ATmega328P/UNO context?

Well, we can only hope that the OP isn't applying more than 5V to the input. They asked for, "close to" 5V... to do that, you must pick a threshold value "close" to 1023. Which value to pick, is application dependant.

Any doubts about why, are a result of the OP getting a free pass on explaining what they are doing and why they need to perform this particular test. The forum guidelines strongly suggest it, so that speculation and guesswork that is not helpful to the OP's actual problem doesn't take hold.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.