Pages: [1]   Go Down
Author Topic: Simple pull-up confirmation  (Read 640 times)
0 Members and 1 Guest are viewing this topic.
London
Offline Offline
Full Member
***
Karma: 0
Posts: 168
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 (8.3 KB, 335x383 - viewed 15 times.)
Logged

Current Projects: Rodentometer - Hamster Endurance Moni

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 202
Posts: 8711
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

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

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

South Africa
Offline Offline
Sr. Member
****
Karma: 2
Posts: 403
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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_final_passive detector.jpg (35.26 KB, 383x293 - viewed 12 times.)
« Last Edit: July 06, 2011, 11:38:44 am by DaveO » Logged

London
Offline Offline
Full Member
***
Karma: 0
Posts: 168
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Current Projects: Rodentometer - Hamster Endurance Moni

London
Offline Offline
Full Member
***
Karma: 0
Posts: 168
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Current Projects: Rodentometer - Hamster Endurance Moni

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 202
Posts: 8711
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

London
Offline Offline
Full Member
***
Karma: 0
Posts: 168
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

(obviously not both of those)
Logged

Current Projects: Rodentometer - Hamster Endurance Moni

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 603
Posts: 33408
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: