Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

switch as input into AVR microcontroller

Status
Not open for further replies.
Hi
I'm having a problem with my avr not detecting a button press it seems. The chip is running fine and switch is connected to ground. I'm wondering if the type of switch makes a difference since the one in the image isn't an SPDT.
 

Attachments

  • vusb_breadboard.jpg
    vusb_breadboard.jpg
    152 KB · Views: 298
Hi
I'm having a problem with my avr not detecting a button press it seems. The chip is running fine and switch is connected to ground. I'm wondering if the type of switch makes a difference since the one in the image isn't an SPDT.
hi,
Do you have a pull up resistor from the pin to +V, say 4k7?
 
I believe the avr has a pull ups internally since in the firmware code I borrowed it mentioned it will activate the pull ups.

If I've understood the manual correctly writting a logical 1 to PORTD =1 << 7 turns on the pull ups
 
Dunno about the AVR, but on a PIC, the port has to be set up for input, and independently, the Pull-Ups have to be enabled by writing a bit to the appropriate register (two independent settings).

The switch then is wired between the port pin and Vss (GND).
 
PORTD = 1<<7 will affect PORTD pin 7. You have the button on PIN 5

DDRD &= ~(1<<5); // This sets Pin 5 of port D to an input
PORTD |= (1<<5); // This enables the pullup on port D pin 5
button_state = (PIND & (1<<5)); // Read from PIND, not from PORTD... this is a classic AVR mistake

if (button_state == 0)
button is pressed

Also, take a look here: AVR Tutorial: Switch Input and Debounce
If you don't handle bounce, you could easily read many switch presses when you only want to read 1
 
Last edited:
If you measure the voltage at the port, it should be high (V+) when the button is not being pressed, if the pull-up is working properly.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top