lazymans switch

ok, so lets say for an application i'm using, i need to add a quick switch, or i'm short certain resistors, or some other excuse, Can i simply hook up a switch to a resistor from 5v to an analog input, and just use an if situation to see weather the switch is open or closed? just always woundered for quick debuging, and becuase i suck at the whole pull up, down resistors...


The mega168's digital I/Os have internal pull-up resistors that you can use for this exact purpose. Connect one side of the switch to ground and the other side to your I/O pin. If you enable that pin's internal pull-up, it will read high with the switch open and low with the switch closed.

  • Ben

Wait, so you're saying you don't actually need resistors on buttons, as long as you enable the internal ones?

How would one do that?

When you're using AVR pins as inputs, they have very high input impedance and draw almost no current, so no, you don't need current-limiting resistors. The only thing you have to be careful of is that you actually use the pin as an input in your code and not an output, because if you try to drive the pin high while it's connected directly to ground (or low while it's connected directly to Vcc) you risk damaging the pin.

AVR digital I/O pins can be in one of input two states:

  1. High-Z (the pin is essentially floating and is not at any predetermined voltage). Use this input mode if you are connected to an input that is never floating, or if you want to use an external pull-down.

  2. Pulled-high internally. Use this input mode if your input source floats in one of its states, like a switch.

The PORTx register determines the input state when the pin is being used as an input. If the PORTx bit for the pin is high, the pull-up is enabled, else the pin is high-Z. I assume you can get the same result (i.e. enable the internal pull-up) with the following Arduino functions:

pinMode(3, INPUT); // make the pin an input
digitalWrite(3, HIGH); // enable internal pull-up

Now if the pin reads high, the switch is open, else if the pin reads low, the switch is closed and shorting it to ground. Very little current will flow through the internal pull-up resistor to ground.

  • Ben

Well that's convenient.

So, if I get this right(11:30PM, end of tough week)

I can wire one end of a "rail" on a micro switch to pin3, then the other to ground.

And if I write the code properly it'll be fine?

thus, the aditional resistors are there just as a safety buffer, just in case?

Yeah, you never need current-limiting resistors on digital inputs as they have plenty of impedance already and will never sink more than a few uA. The internal pull-up also has very high impedance. The only time you need to use a resistor on an input is if you want an external pull-down or if it's part of some circuit meant to modify the input signal (e.g. a voltage divider or RC filter).

Basically, when you're hooking things up, ask yourself "is it possible that I'm shorting anything bad here". When it comes to a digital input, you can put any voltage you want directly on it (within the spec of the pin, of course) and the pin will be fine. Once again, just make sure your program doesn't then go and use that pin as a digital output.

  • Ben


Well that's good, I was panicking when I read the button guides, because being a beginner at electronics(soldered couple dozen joints total) I don't exactly have a massive variety of resistors, and i don't have much of a stable income to get em.

Eventually I'm probably gonna get a proper storage solution and a set of 100 assorted resisters.

thats cool, i never knew that! just connecting a switch from ground to my pin and typing it as an input is MUCH MUCH easier then finding reight resistor values and such!

Hmm, can't seem to get this to work.

My code

int ledPin = 13;

void setup()
pinMode(ledPin, INPUT); // make the pin an input
digitalWrite(ledPin, HIGH); // enable internal pull-up

void loop()
if (ledPin = HIGH){
Serial.println("Pin is High");
Serial.println("Pin is LOW");

And here's a pic on my breadboard.(It's too big for the forum)
The ground is jumped to the bottom rail on the far end of the board.

Button pushed or not, the serial returned is always High

This is because ledPin is a variable that has nothing to do with the input state of the pin. You need to do a digitalRead() on ledPin to get the input state.

Also, you are using the wrong operator in your equality check:

if (ledPin = HIGH)

this sets the variable ledPin equal to HIGH (1) and returns the value HIGH.

The "correct" check would be

if (ledPin == HIGH)

if this is what you actually wanted to do (it isn't).

  • Ben

Bleh, in my hunt of how to use digitalWrite, I came across sample code that basically does this exact thing, and modified it slightly.

int ledPin = 13; // LED connected to digital pin 13
int inPin = 12; // pushbutton connected to digital pin 7
int val = 0; // variable to store the read value

void setup()
pinMode(ledPin, OUTPUT); // sets the digital pin 13 as output
pinMode(inPin, INPUT); // sets the digital pin 7 as input
digitalWrite(inPin, HIGH); // turn on pullup resistors


void loop()
val = digitalRead(inPin); // read the input pin
digitalWrite(ledPin, val); // sets the LED to the button's value

Now the pin 13 LED is on by default, and when pin 12 is grounded, the LED turns off.

BUT it doesn't seem to work with the switch, soon as I plug the switch in, the led goes off(so the switch grounds it) but pressing it does nothing.

I've tried 2-3 different ones and nothing :frowning:

If the LED is on with the input pin disconnected and off with the input pin grounded, you must be connecting your pushbutton wrong if the LED is always staying on. Instead of connecting the pin to ground through the button, it seems you are just shorting the pin permanently to ground.

Try rotating your pushbutton by 90 degrees (or, use a digital multimeter to figure out which pins are connected through the button and which pins are permanently shorted together).

  • Ben


I had the switches rotated wrong.

I was running them across the pins that "point" to each other, and I thought that that COULD be the problem, but the switches wouldn't fit across the breadboard the other way, so I assumed it was right, after bending the switch pins it all works.

Thanks a ton.

Now to come up with something to do with this newfound knowledge XD



Why do I get upgraded to "Full Member" at 103 posts?(I've been watching my post count)

I strictly remember being a Junior at 101.