# Analog pin A7 for push button - Any way possible?

Hi,

I have made a circuit board for controling LEDs using push button. The circuit I have designed uses analog pin A7 for button input and the PCB are allready produced an shiped to me. Yes, I know now that this pin is analog only and cannot be used as digital input, but is there any way possible for this pin to be used as a button input? My code is using a button to control a switch case function and by testing with a other analog pin, the button/code works fine.

Thanks

32teeth:
You can actually use A6 and A7 as 'digital' pins with a little bit of logic in your code
below is my craptastic ] example

``````int pinStateA6 = 0;
``````

int pinStateA7 = 0;

void setup()
{
}

void loop()
{
pinStateA6 = analogRead(A6) > 100 ? 0 : 1;
pinStateA7 = analogRead(A7) > 100 ? 0 : 1;
}

Is it possible to do this?
(Thanks for the example 32teeth)

but is there any way possible for this pin to be used as a button input?

Have the pin pulled to GND by a resistor and pull it to 5V when the button is pressed

What values are returned by analogRead(A7); ?
A small amount of code will allow you to determine whether the button was pressed or not. As you have the PCB already made the resistor and associated wiring will need to be bodged into the circuit

Why would you tell someone to install a pulldown and then a button to connect to+5?
A miswire ends up shorting +5 to Gnd, that's something to be avoided.

Do it the other way around. Pullup to 5V, button press to connect to Gnd, just like using INPUT_PULLUP on the other pins.
Then:

``````if (analogRead (A7) <= 185){
// button pressed . Valid low is less than 0.9V with VCC = 5V, analog reading of 185 = 0.9028V
}
``````

A miswire ends up shorting +5 to Gnd, that's something to be avoided.

That is true however you wire up the circuit

Why would you tell someone to install a pulldown and then a button to connect to+5?
A miswire ends up shorting +5 to Gnd, that's something to be avoided.

Do it the other way around. Pullup to 5V, button press to connect to Gnd, just like using INPUT_PULLUP on the other pins.
Then:

``````if (analogRead (A7) <= 185){
``````

// button pressed . Valid low is less than 0.9V with VCC = 5V, analog reading of 185 = 0.9028V
}

Thanks guys. Could you please guide me on how I should change my code for this?
This is parts of the code:

``````void loop() {

if(buttonPoll == 1){
delay(50);
if(buttonPoll == 0){
state = old + 1;
}}

else{
delay(100);
}

switch (state) {

case 1:    //NL

digitalWrite(UV,LOW);
digitalWrite(LED13,LOW);
digitalWrite(X1A,LOW);
digitalWrite(X1B,LOW);
digitalWrite(X1C,LOW);
digitalWrite(B20,LOW);
digitalWrite(B40,LOW);
digitalWrite(B60,LOW);
digitalWrite(B80,LOW);
digitalWrite(B99,LOW);
digitalWrite(RED,LOW);
digitalWrite(GREEN,LOW);
digitalWrite(BLUE,LOW);

digitalWrite(NL, HIGH);
old = state;
break;

case 2:    //25%

digitalWrite(UV,LOW);
digitalWrite(NL,LOW);
digitalWrite(LED13,LOW);
digitalWrite(X1B,LOW);
digitalWrite(X1C,LOW);
digitalWrite(B20,LOW);
digitalWrite(B40,LOW);
digitalWrite(B60,LOW);
digitalWrite(B80,LOW);
digitalWrite(B99,LOW);
digitalWrite(RED,LOW);
digitalWrite(GREEN,LOW);
digitalWrite(BLUE,LOW);

digitalWrite(X1A, HIGH);
old = state;
break;
``````

``````  buttonPoll = analogRead(A7) < 500 ? 1 : 0;
``````

Exchange the positions of the 0 and 1 if you want to reverse the logic

I'm curious. Which Arduino board do you have that has analog only pins? I thought all Arduino analog pins could also be configured as digital pins.

Which Arduino board do you have that has analog only pins?

It will be a Nano I expect

Pins A6 and A7 are analogue in only

Interesting. I learned something new today. I always assumed, because it was a 328, it followed the same pin configuration.

Never make assumptions!

Im using a Atmega328-AU on my own circuit bord. Im using all but one analog/digital pin for my project. The Atmega328 is to be used at 8MHz witouth a crystal, but thats a problem for another post.

Have you tried the suggested methods of detecting a digital input on A7 ?

UKHeliBob:
Have you tried the suggested methods of detecting a digital input on A7 ?

Like this?

``````  buttonPoll = analogRead(button) < 500 ? 1 : 0;
if(buttonPoll == 1){
delay(50);
buttonPoll = analogRead(button) < 500 ? 1 : 0;
if(buttonPoll == 0){
state = old + 1;
}}
``````

Like this?

Yes, as long as what you want to do is to check whether the pin went from HIGH to LOW within 50 milliseconds.

UKHeliBob:
Yes, as long as what you want to do is to check whether the pin went from HIGH to LOW within 50 milliseconds.

Okey, thanks!
On my PCB I have a resistor (10K) from the A7 pin to GND and a wire from A7 to the push button, then to VCC, so when I push the button, the A7 pin should be "HIGH" and when the button is not pushed, the pin is connected to ground via the 10K resistor.
(If I add use serial monitor on the A7 pin, I get 1023 when button is pushed and 0 when its not pushed)

If I add use serial monitor on the A7 pin, I get 1023 when button is pushed and 0 when its not pushed

So you are getting HIGH and LOW. As I said in post #6, if you want to invert the logic change the positions of the 0 and 1 in the test expression. Alternatively change the test from < 500 to > 500

UKHeliBob:
So you are getting HIGH and LOW. As I said in post #6, if you want to invert the logic change the positions of the 0 and 1 in the test expression. Alternatively change the test from < 500 to > 500

The code worked! Thank you so much!