I'm working on a project in which I use a lot of the pins on the Arduino; I have run out of most of the pins and and I need to read the value of a push button.

Is it possible to read the value of the switch in the analog in pins on the board??

If so how can I implement this?

Is it possible to read the value of the switch in the analog in pins on the board??

Yes. Analog input pins are also digital IO pins. You can use them exactly the same way as all the other pins on the board.

• e.g. `digitalRead(A1)` Commented Feb 13, 2017 at 11:34

On ATmega640/ATmega1280/ATmega1281/ATmega2560/ATmega2561 devices (with Arduino Mega using an ATmega640 or 2560), the analog input pins have their analog capabilities in addition to standard digital capabilities. They can be used to read or write digital values, like other digital pins on the device.

On ATmega48A/PA/88A/PA/168A/PA/328/P devices (with Arduino Uno using an ATmega168- or 328-family device) analog inputs A0 ... A5 have standard digital capabilities, but A6 and A7 do not. A6 and A7 on an Uno or Nano is limited to analog input. However, you can use a circuit like the following to multiplex several buttons. With this circuit attached to A6, you would get 1 V at A6 when you press S1; 2 V when you press S2; 3 V when you press S3; and so forth. You get 0 V at A6 when all the switches are open.

One would first use a fast analog read (about 13 μs) to decide if any switch is pressed; for example, if the first reading is above 1/2 V. In some systems, a fast reading may be good enough by itself to decode switch number; if not, take a second reading via ordinary analog read. In any case, given an analog reading `ar`, and letting `CPS` be the expected step in counts from one switch to the next (in this case either 256/5 or 1024/5 depending upon use of 8 or 10 bits of readings), compute switch number via something like `s = (ar+CPS/2)/CPS;`.

Same things happens with me.

Here, it's my code. It's works for me.

``````const int kPinBtn = A0;  // Push-Button connected to Analog pin A0
const int kPinLED = 13;  // LED connected to to Digital Pin 13

void setup()
{
pinMode(kPinBtn, OUTPUT); // Generally, in push-button we take INPUT as a parameter but here we take OUTPUT because ANALOG PIN

digitalWrite(kPinBtn, HIGH); // Make button condition HIGH

pinMode(kPinLED, OUPUT);
}

void loop()
{
// Now, normal push-button operation, LED blink when we press push button

if(digitalRead(kPinBtn) == LOW)  // If button pressed
{
}
else digitalWrite(kPinLED, LOW); // Else LED is off
}
``````

Is it possible to read the value of the switch in the analog in pins on the board??

As other answers say, you can use digitalRead on most Analogue pins. But to answer the question, yes you can read analogue input, which is useful on pins like A6/A7 which cannot be used as digital pins.

You should connect the pin with a pull up resistor - for arduino pins A0-A5 (depending on board, i'm referring to a Nano) this can be done internally using:

``````pinMode(5, INPUT_PULLUP); // Enable internal pull-up resistor on pin 5
``````

If you are working on pins A6/A7 you cannot access the internal pull-up resistor, so you need to add your own to prevent a floating input. A 10K resistor should go between the input pin and VCC.

Lastly, an example of reading the input:

``````const int buttonPin = A7;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

void loop() {
// read the state of the pushbutton value: