Attiny85 Pin2 (SCK) detects (non existing) input

Hi,

I'm not really experienced when it comes to electronics, but I've used the Attiny in some projects. For this project, I wanted to read a buttonstate via Pin 2, but that doesn't work. The pin always detects an input even if the button is not pressed. It's quite an easy setup (one button, one led) and it works if I use other Pins. I tried two Attinys, both show the same effect. So I guess it has something to do with this pin being the serial clock pin?

Here the corresponding code (not the most complex one)

const int LED_PIN = 4; const int BUTTON_PIN = 2;

void setup() { pinMode(BUTTON_PIN, INPUT); pinMode(LED_PIN, OUTPUT); }

void loop() { if(digitalRead(BUTTON_PIN) == HIGH ){ digitalWrite(LED_PIN, HIGH); }else{ digitalWrite(LED_PIN, LOW); } }

Why does it behave that way?

Also if I touch a pin that reads digital input (doesn't matter if Pin 2 or 3 for example) with some object (doesn't matter if metal, paper or plastics), it detects an input

With just eyeballs and a brain I can tell your code will not compile. Code that compiles would be a good starting point.

Sorry, simpliefied the code and forgot to change a variable name

If nothing is connected to BUTTON_PIN, then the pin is floating, and its behavior is unpredictable. Try using INPUT_PULLUP instead of INPUT.

AljoSt: Hi,

I'm not really experienced when it comes to electronics, but I've used the Attiny in some projects. For this project, I wanted to read a buttonstate via Pin 2, but that doesn't work. The pin always detects an input even if the button is not pressed. It's quite an easy setup (one button, one led) and it works if I use other Pins. I tried two Attinys, both show the same effect. So I guess it has something to do with this pin being the serial clock pin?

Here the corresponding code (not the most complex one)

const int LED_PIN = 4; const int BUTTON_PIN = 2;

void setup() { pinMode(BUTTON_PIN, INPUT); pinMode(LED_PIN, OUTPUT); }

void loop() { if(digitalRead(BUTTON_PIN) == HIGH ){ digitalWrite(LED_PIN, HIGH); }else{ digitalWrite(LED_PIN, LOW); } }

Why does it behave that way?

Also if I touch a pin that reads digital input (doesn't matter if Pin 2 or 3 for example) with some object (doesn't matter if metal, paper or plastics), it detects an input

Hi! A typical newbe catch. If You don't have a external "PULL_DOWN" resistor for them INPUT's, they'll be hanging in the free air, thus catching "what so ewer" signals from the close by area. Rather make them inputs to be with internal PULL_UP resistor = pinMode (button,INPUT_PULL_UP) ; Thus this makes Your button to be "Active" with "LOW" = 0V, mind that in your programming. I have made a simple routine to convert that "LOW" to be "HIGH" =>e.g. BUTTON_CONV = digitalRead (BUTTON_1);// BUTTON_1 is defined as input with pull_up BUTTON_CONV =! BUTTON_CONV;//The statement " =! " says it NOT to be what it is, so, it becomes the complement of itself :confused:

kiselin: Hi! A typical newbe catch. If You don't have a external "PULL_DOWN" resistor for them INPUT's, they'll be hanging in the free air, thus catching "what so ewer" signals from the close by area. Rather make them inputs to be with internal PULL_UP resistor = pinMode (button,INPUT_PULL_UP) ; Thus this makes Your button to be "Active" with "LOW" = 0V, mind that in your programming. I have made a simple routine to convert that "LOW" to be "HIGH" =>e.g. BUTTON_CONV = digitalRead (BUTTON_1);// BUTTON_1 is defined as input with pull_up BUTTON_CONV =! BUTTON_CONV;//The statement " =! " says it NOT to be what it is, so, it becomes the complement of itself :confused:

Or you could just do this:

const int LED_PIN = 4;
const int BUTTON_PIN = 2;  // pin -- button -- gnd

void setup() {
 pinMode(BUTTON_PIN, INPUT_PULLUP);
 pinMode(LED_PIN, OUTPUT);
}

void loop() {
 if(!digitalRead(BUTTON_PIN) == HIGH ){
   digitalWrite(LED_PIN, HIGH);
 }else{
   digitalWrite(LED_PIN, LOW);
 }
}

ie put the ! in front of digitalRead().

ChrisTenone: Or you could just do this:

const int LED_PIN = 4;
const int BUTTON_PIN = 2;  // pin -- button -- gnd

void setup() { pinMode(BUTTON_PIN, INPUT_PULLUP); pinMode(LED_PIN, OUTPUT); }

void loop() { if(!digitalRead(BUTTON_PIN) == HIGH ){   digitalWrite(LED_PIN, HIGH); }else{   digitalWrite(LED_PIN, LOW); } }




*ie* put the ! in front of digitalRead().

So right you are, 1. " ! " comes before the "=", howewer it doesn't matter if it comes before or after, it works both ways. 2. It's wa...y better to read the input to a variable and "!" convert it right on. Because when you use the input in many places in your program would you allways use the: if(!digitalRead(xxxxxxxx))............. in say 12 places? Instead of using the converted variable!