2
\$\begingroup\$

I am trying to "shrink" my arduino project to an Attiny85. But for some reason I cannot get an input button to work on the Attiny85? To simply the circuit and to make sure I didnt make a mistake somewhere I took everything off except for a button and an LED. First I tried it on the Arduino and used the example from http://arduino.cc/en/tutorial/button to try a simple example. I changed the ledPin to 10 which I plugged into my breadboard, through a resistor, to the LED and to ground. I plugged the button into pin 2 on the arduino and connected the ground wire of the button to a 10k resistor, then to ground. All of that works.

So then before I upload it to the tiny I changed the ledPin to 0 or 1 (it works on either one - I uploaded a simple blink sketch without the button code to test the LED) and then left the button pin to 2 as is shown by the diagram here http://www.pighixxx.com/pgdev/Temp/attiny.pdf or http://hlt.media.mit.edu/?p=1695 but nothing happens when the button is pressed. If I reverse the logic so the LED is on unless the button is pressed, then the LED is always on.

I cannot figure out what I am doing wrong. The same wiring works with the arduino, but I cannot get the attiny to register the button press. The final goal with the button would be to use the attachInterrupt(0, increment, CHANGE); for the button, but I cannot get even a simple sketch to work.

Any help is much appreciated! Thanks in advance!

Edit1:

// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  1;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);  
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin, HIGH);  
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW); 
  }
}
\$\endgroup\$
10
  • \$\begingroup\$ Show the code. I've managed to get both the button and debounce sketches working on an '85. \$\endgroup\$ Commented Sep 21, 2013 at 4:20
  • \$\begingroup\$ That code works perfectly here. Verify operation and pinout of the switch, as well as continuity of the wires. \$\endgroup\$ Commented Sep 21, 2013 at 4:38
  • \$\begingroup\$ But if it works as expected with the Arduino and I am not changing anything then all that should be fine? I will look into that though now that I know that the code is correct and I didnt miss an important step. \$\endgroup\$ Commented Sep 21, 2013 at 4:43
  • \$\begingroup\$ your description of the button connection sounds wrong. You should connect one terminal of the button to ground, and the other to the input pin, then connect a resistor from the input pin to Vcc (or switch to Vcc and resistor to ground, if you prefer). Your description says you have the switch and resistor in series between the input pin and ground, which is unlikely to work. \$\endgroup\$ Commented Sep 21, 2013 at 5:11
  • 1
    \$\begingroup\$ @Elmer: I prefer to connect the switch between the input pin and ground, with the resistor between the input pin and the positive supply. With connection as shown, if there is an internal pull-up resistor on that input pin, the external resistor may not pull the input low enough to be recognized as a low, where a switch to ground will definitely pull the input pin low. \$\endgroup\$ Commented Sep 21, 2013 at 15:54

1 Answer 1

1
\$\begingroup\$

Its simply your button setup that is the problem. Tie your button pin to ground with the resistor and tie the button from the button pin to vcc.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Generally speaking it is preferred to do it the other way around, with a pull-up resistor and a switch contacting to ground, both because this means that part of the button can then be at ground potential which may be useful for safety or shielding purposes and more available on a PCB layout, and because for many chips it will then be possible to leverage an internal pull-up resistor. \$\endgroup\$ Commented May 28, 2015 at 19:19
  • \$\begingroup\$ And to make use of this internal pull-up you just replace INPUT with INPUT_PULLUP in pinMode. \$\endgroup\$
    – hlovdal
    Commented Apr 3, 2016 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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