I am trying to get a simple button (GPIO shortcircuit) to work. I have chosen Pin D0 as input and Pin D7 as output. It works fine in an Arduino sketch but I can't get it to run in pure C. What am I doing wrong? I bet I got the bits all wrong...

My code:

DDRD = 0b11111110; // set all port D pins to Output besides pin 0, which is set to input
PORTD = 0b11111111; // set all D pins to WRITE / PULL-UP
int a = PIND & 1; // a is 0 or 1, depending on whether pins D0 and D7 are physically connected

It doesn't work though, a is never zero, whether the pins are connected or not.

Here is my complete code sample, where the on board LED depends on my pins connection state:

#include <avr/io.h>
#include <util/delay.h>

int main(void)
 DDRB |= _BV(DDB5);
 DDRD = 0b11111110;
 PORTD = 0b11111111;

     while(1) {
        int a =  PIND & 1;
        if(a != 0)
             PORTB |= _BV(PORTB5); // blink on
             PORTB &= ~_BV(PORTB5); // blink off
  • You wired a pushbutton between digital 0 (PD0) and GND, right? – Edgar Bonet Apr 12 '16 at 15:23
  • GND is not involved. I put a direct wire between PD0 and PD7. Is that wrong? It works in Sketch. – Traubenfuchs Apr 12 '16 at 15:30
  • 1
    Yes, it's wrong, you will always read HIGH. – Edgar Bonet Apr 12 '16 at 15:53
  • Pin D7 is HIGH, so if the pins are connected you read HIGH. If they are disconnected you will still read HIGH because you enabled the pull-up resistor. Set D7 to LOW instead. That way you can differentiate between connected and disconnected. – Gerben Apr 12 '16 at 15:56
  • Setting pin D7 to 0 makes my contraption work. I don't understand it though, I thought I could imagine it like this: Setting D7 to High/1 would make it pour out power and setting D0 to read and reading it's value would tell me whether power is pouring in... – Traubenfuchs Apr 12 '16 at 16:14

The problem here is you set pull-up on D0. So even if button is not pressed it will be at high state because of pull-up. You would need pull-down for this to work (Arduino does not have it).

The pullups are useful if you want to connect pin to GND when button is pressed. When button is not pressed it will have defined state (HIGH because of pull-up) and when pressed it will be pulled LOW by button. You can imagine pull-up as a resistor to Vcc.

So for this to work I would put the other end of button to GND and it will work (One end to D0, second on GND). Condition in code should be altered (a == 0) but otherwise all can be the same.

Other option is to connect external resistor in range 1K-100K Ohm between pin D7 and GND and turn off pull-up in code. That resistor would serve as pull down.

Just one more note. If you are not using other pins it is the best to leave them as inputs. Setting an "unused" pin as output and setting it to HIGH can damage the pin if you accidentally connected it to GND (because in previous sketch you experimented something).

| 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.