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
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.
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.
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.
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.
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.
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.