void setup() { pinMode(A0, INPUT); Serial.begin(9600); }

void loop() { if (digitalRead(A0)== HIGH) { Serial.println("YES"); } }

Not giving any input when i am supplying input via push button. I have 5 v supply through 10 kohm resistor to push button and then have other side to A0 and a led to ground. It takes input when I take out wire connected to A0 and just leave it unconnected.


As an alternative to ChrisVolkoff's answer, you may not be getting a high enough voltage to reach the ON threshold.

If your circuit is setup:

[Vin]-----[10k Resistor]---[switch]---[pin]---[LED]---[Gnd]

The forward bias of the LED is generally about 1.8 volts so, when you push the switch, the voltage at the pin is only about 1.8 volts. Typically, with a boolean pin, "HIGH" is triggered for voltages above about 60% of Vin, "LOW" is triggered for voltages below about 30% of Vin, and anything in between is unreliable and, depending on the system, either ignored or may be counted as high or low.

My advice would be to put the 10k resistor between the input pin and the LED, so when you close the switch the input pin is reading 5V. If you don't want the full 5V on the pin, then you could set up a voltage divider by putting one smaller resistor between 5V and the pin, and another one (the remainder of the 10k of resistance you were originally using) between the pin and the LED.

I'm not familiar enough with the Arduino to know what your maximum allowable voltage is on the input pin (5V or 3.3V), so we'll consider a couple cases.

First, is that you're allowed to put 5V on the input pin. In that case, just move the full 10k between the input pin and the LED:

[Vin]----[switch]---[pin]---[10k Resistor]---[LED]---[Gnd]

Second is that you're only allowed 3.3V on the input pin. In this case, I would suggest you first measure voltage at the positive end of the LED when the circuit is completed. 1.8V is a general assumption; it might be different based on LED color.

You want 3.3V, but there is some voltage drop at the LED that fixes the "minimum" voltage you can get - let's call it V_LED.

What you want to do is to make up the difference between V_LED and 3.3V to get 3.3V on the input pin. Your input voltage is 5V, so you want a voltage divider that gives you:

(5V to 3.3V) on the high end (3.3V to V_LED) on the low end.

The total voltage drop should be (5V to V_LED), so you want one resistor that is

(5V-3.3V)/(5V-V_LED) * 10k

and one resistor that is:

(3.3V - V_LED)/(5V-V_LED) * 10k

Assuming again that V_LED is 1.8V, this means that you should get:

$$ R_1 = \frac{5-3.3}{5-1.8}*10k\Omega \\ R_1 = \frac{1.7}{3.2}*10k\Omega \\ R_1 = 5.3 k\Omega \\ $$ $$ R_2 = \frac{3.3-1.8}{5-1.8}*10k\Omega \\ R_2 = \frac{1.5}{3.2}*10k\Omega \\ R_2 = 4.7 k\Omega \\ $$

[Vin]-----[switch]-----[5.3k Resistor]----[pin]----[4.7k Resistor]---[LED]---[Gnd]

As you can see, $R_1 + R_2 = 10k\Omega$, so you still get the 10k resistance you need for the LED, but now the voltage at the input pin is 3.3V instead of the 1.8V (or V_LED, whatever it measures to be).

In general, when you are trying to measure a HIGH input value, you should attempt to send the nominal HIGH voltage that the input is expecting. Trying to send borderline values (minimum value for HIGH, etc.) is begging for trouble.

| improve this answer | |
  • $\begingroup$ Then why it works with any other analog input i.e. A1,A2,A3,A4,A5? $\endgroup$ – user16402 Mar 16 '17 at 4:42
  • 1
    $\begingroup$ @user16402 - It could be a number of different things, from noise to capacitive coupling to the pullup configuration for the pin. Like I mention in the post, the only time you're guaranteed to read a high or low correctly is when you exceed the threshold for that level. If you operate in the middle ground then you get arbitrary results. I see you accepted the answer - did moving the resistor work? $\endgroup$ – Chuck Mar 16 '17 at 13:28
  • $\begingroup$ yes now that is working perfectly $\endgroup$ – user16402 Mar 17 '17 at 12:45

The A0 pin is an analog pin, so you'd have to use analogRead(), which would give you a value. If you want to use digitalRead() and compare it to HIGH/LOW, use a digital pin.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.