Simple pull-up confirmation

Before I get too deeply buried in my master plan, can someone confirm that I've understood the built-in pull-up resistors correctly?

I have a simple sensing setup for my bot. It consists of two push-switches that get closed when something hits the front of the bot. Like a wall.

These are feeding into two pins on the uC, so that I can tell which side was touched.

The attached diagram is a bit oddly laid out because it's part of other stuff, but this is just the sensing input.

So P1 and P2 attach to Arduino pins, set to INPUT, and I have a resistor on the pin-wire (R1 and R4) and a resistor to ground (R2 and R3).

What is the favoured configuration for this sort of thing? If there even is a 'preferred' way? I've seen some people always put the switches on the GND side of the layout, others on VCC side. Does it really matter?

antenna.png

If you put the switches to the Vcc side you have to use an external pull-down resistor. If you put the switches to ground you can use the internal pull-up resistor. Your R1 and R4 are not needed. If you use ground instead of +5 your R2 and R3 are also not needed.

pinMode(pin, INPUT);
digitalWrite(pin, HIGH); // Enable internal pull-up

not a guru on these things, but I think it would be better to have the Ground to the top of the switches.
Set the 2 pins as Input and set them High to enable the built in pull up resistors.
Connect the 'bottom' of the switches to the input pins.

This way you're only working with a Ground signal, the pins sit High ( using the internal pull-ups ) and your code looks for a Low on each pin.

Not sure if you should then have a resistor in series between the switch and the input pin. Personally I would pass the signal via an opto-coupler so if anything went wrong ( like your antenna getting zapped by static, etc ) your input pin is protected.

Attached is a drawing I did for the opto-coupler.

you would simply change the 'passive detector' with your switch, and you could do away with the 10K pull-up resistor.

DRAWING10.jpg

Thank you both. Less bits is good - I'll turn it upside-down and put the resistors back in the box :slight_smile:

johnwasser:
If you put the switches to the Vcc side you have to use an external pull-down resistor. If you put the switches to ground you can use the internal pull-up resistor. Your R1 and R4 are not needed. If you use ground instead of +5 your R2 and R3 are also not needed.

So just to check, you basically mean connect PinA to the switch, and the switch to GND, and not need any external pull resistors?

Cylindric:

johnwasser:
If you put the switches to the Vcc side you have to use an external pull-down resistor. If you put the switches to ground you can use the internal pull-up resistor. Your R1 and R4 are not needed. If you use ground instead of +5 your R2 and R3 are also not needed.

So just to check, you basically mean connect PinA to the switch, and the switch to GND, and not need any external pull resistors?

Correct. Enable the internal pull-up and the switch will read HIGH when open (because of the internal pull-up) and LOW when closed (because of the connection to ground through the switch).

Many people seem like the HIGH means CLOSED, LOW means OPEN they get when they use an external pull-down and s switch to +5 but the software can be written either way and the hardware is less complicated and expensive when you use the internal pull-up.

Great, thanks for the clarification.

Not too worried about the HIGH/LOW thing, as you say it's easily coded for, and anyway it would be a single line in a function along the lines of

bool Antenna::isTouched()
{
  return (digitalRead(ANT_PIN) == HIGH);
  // or
  return (digitalRead(ANT_PIN) == LOW);
}

(obviously not both of those)

I've seen some people always put the switches on the GND side of the layout, others on VCC side. Does it really matter?

I would say yes. Always connect a switch between input and ground if you have any choice about it.

Switches are often on the end of long wires and if one is +5V then there is a danger of shorting things out and causing excessive current flow. Having long runs of ground has no danger.
Also in many types of logic (TTL for example) You need a much lower value resistor to pull down than to pull up. This leads to noise immunity problems because you want to pull down with as low a value as possible but this increases the current flow through the switch to get to the logic one level. So the compromise is against you. Some logic gates will "float high" that is will look like a logic one when nothing is connected, while this is bad practice to have on permanently (noise immunity problems) it is OK for a quick test.