Shiftin - 8 pulldowns?

Is it really neccessary to have 8 pulldowns in the circuit, or could you get by with just one 10K (or would that be 80K?) resistor wired in parallel to all the switches?

The reason I ask is I was considering adding a dip switch with 8 switches to my project, and I'd rather not have to add 8 resistors as well.

So is there a way to read the dip switch without the use of so many additional resistors? If there's some other IC I could use to accomplish the same thing... read 8 switches without using 8 pins... without needing so many extra components, that would be helpful.

Also, if it helps, I don't need to read these switches fast or anything. Once when the arduino boots up, that's all. Maybe there's some way to use something like a decade counter to step through each switch to read its value using just one resistor, one ic, one dip switch and two pins on the Arduino? That would be great if I could do that.

http://www.arduino.cc/en/Tutorial/ShiftIn

Is it really neccessary to have 8 pulldowns in the circuit, or could you get by with just one 10K (or would that be 80K?) resistor wired in parallel to all the switches?

Yes, it is necessary with 8 pull-down/-ups in that circuit. You cannot just add the resistance together like that and have one pulldown resistor. Also, how would you (or the circuit) know the difference between the switches if they are in parallel?

The reason I ask is I was considering adding a dip switch with 8 switches to my project, and I'd rather not have to add 8 resistors as well.

So is there a way to read the dip switch without the use of so many additional resistors?

You could use the internal pull-up resistors of the arduino inputs. But then you would use one input pr. switch.
Also, resistors, especially the 1/4W carbon type, aren't exactly expensive or very big anyway.

Also, if it helps, I don't need to read these switches fast or anything. Once when the arduino boots up, that's all. Maybe there's some way to use something like a decade counter to step through each switch to read its value using just one resistor, one ic, one dip switch and two pins on the Arduino? That would be great if I could do that.

That is exactly what the ShiftIn tutorial is all about, just using three pins. And a shiftregister (PISO - Parallel In, Serial Out type). Not impossible with two or even one pin I would think, but then again it would need more external component and more complex interface with software.

Yes, you need all 8 resistors.
Same reason as when you are using a shift register to drive leds. You need equal current to all the buttons, and one resistor cannot do this. But I would think there would be PISO shift registers that have built in pullups, so you might look into that.
I believe there also are resistors in 16 pin DIP packages, so that could simplify things, having one piece instead of 8 separate resistors.

Edit: whoops raron beat me!

There are bussed resistor arrays available in SIP packages - check Mouser, Digikey, etc. Though the 9-pin versions with 8 resistors can be annoyingly hard to find.

Try here

http://au.farnell.com/jsp/search/browse.jsp?N=0+5122258+326304&Ntk=gensearch_001&Ntt=sil+resistor&Ntx=mode+matchallpartial&suggestions=false&ref=globalsearch&_requestid=1855307&No=0&getResults=true&appliedparametrics=true&locale=en_AU&catalogId=&prevNValues=0&filtersHidden=false&appliedHidden=false&originalQueryURL=%2Fjsp%2Fsearch%2Fbrowse.jsp%3FN%3D0%26Ntk%3Dgensearch_001%26Ntt%3Dsil%2Bresistor%26Ntx%3Dmode%2Bmatchallpartial%26suggestions%3Dfalse%26ref%3Dglobalsearch%26_requestid%3D1855307%26No%3D0%26getResults%3Dtrue%26appliedparametrics%3Dtrue%26locale%3Den_AU%26catalogId%3D%26prevNValues%3D0

Sorry about the huge URL. If you have trouble just go to Farnell and search for "sil resistor".

Also, how would you (or the circuit) know the difference between the switches if they are in parallel?

Hm... yes, I see now. The pulldowns are connected to the pins on the Arduino.

So the pins in the schematic are connected like so:
Vcc-Switch-Pin-10K-GND
Vcc-Switch-Pin-10K-GND
Vcc-Switch-Pin-10K-GND
Vcc-Switch-Pin-10K-GND

With all the grounds tied together obviously.

But if I were to try to ground all the pins via a single resistor it would be like this:
Vcc-Switch-Pin
Vcc-Switch-Pin
Vcc-Switch-Pin
Vcc-Switch-Pin-10K-GND

With all the pins tied together. And in that setup, if any switch were to be turned on, all of them would be connected directly to +5v.

Yep, that was dumb of me!

Yes, you need pull-up (or -down) resistors if you are using a chip with "logic" inputs like the 4021.
If you want to get away with only one resistor, use a multiplexer chip like 4051.

Oh that's funny. :slight_smile:

I actually drew up something that would work exactly like that chip this morning and was wondering if something like that existed. It didn't occur to me that you might be able to chain them or set them up in an array though. Neat!

So how would I go about wiring that up? One pulldown on the input pin of the arduino I assume? But what about the three output pins? Will I need to pull the inputs on the IC low?

Guess I'll see what the datasheet says once I find one on Mouser. definitely gonna pick one of those up. Not sure if I'll use it or not though... it does cost one more pin than the shift register. On the other hand I won't have to solder as many resistors. Decisions!

Graynomad, Professor Chaos:
Thanks for those, I'll pick some of them up as well! Those would not only save me a bunch of work but also would allow me to reduce the size of the board. I've only got around 3"x4" of PCB to work with.

Yep, that was dumb of me!

No worries. Short-circuits like that (uhm, yeah) happens in my brain all the time too :smiley:

If you want to get away with only one resistor, use a multiplexer chip like 4051.

I don't think that would work, you are just moving the problem. The inputs of the multiplexer would be floating instead, giving random signals on its output, regardless of a pullup/down resistor on that output. Well unless the multiplexer has some kind of internal pullup like the arduino.

The inputs of the multiplexer would be floating instead,

True

giving random signals on its output

False, because the selected signal will have the pull-up/down resistor, the others can do what they like.

However there may be glitches to account for when the MUX input is changed.

I always thing it is very bad in the tutorials having pull down resistors with switches connected to +5v.
Yes I know that is the way beginners like to think of things with a switch on giving a one but that is not the way it is normally done in electronics. Having the power rail appere on the output of a switch is more potentally damaging when errors in wiring are made and with TTL logic pulling down takes more current and has less noise imiunity that pulling up.
In fact with TTL inputs will float high by themselves although it is not good practice to let them.

However if you used a multiplexer it would work with a pull up on the input of the multiplexer and a connection to ground through the switch. That way the output would be high unless a closed switch was selected and switch through to the output.

However there may be glitches to account for when the MUX input is changed.

No because you change the input selection bits and then look at the input. Any false selections will have settled down by then.

Ok. So a mux and one pull-up would work. I'm guessing a bi-directional analog one then.

However if you used a multiplexer it would work with a pull up on the input of the multiplexer

Don't you mean on the output of the multiplexer? (or, the side side used as an output).

Don't you mean on the output of the multiplexer

Well as it is analogue the terms input and output are somewhat interchangable.
To clear things up, pull up the common connection and switch to ground the secected pins.

I always thing it is very bad in the tutorials having pull down resistors with switches connected to +5v.
Yes I know that is the way beginners like to think of things with a switch on giving a one but that is not the way it is normally done in electronics. Having the power rail appere on the output of a switch is more potentally damaging when errors in wiring are made and with TTL logic pulling down takes more current and has less noise imiunity that pulling up.

Do you have a link to a tutorial or schematic showing the right way to do it?

Any of my circuits.
switch between input and ground. resistor from input to +5.

Sounds easy enough...

But what about the shift register example? Swap +5v and Gnd in the schematic I presume?

And what of the Arduino's internal 20K pullup resistors? Can I then make use of those and gret rid of the connections to +5v through the 10K resistors?

Oh no... wait. I was looking at the shift register thinking that was the Arduino. Damn. :confused:

No because you change the input selection bits and then look at the input. Any false selections will have settled down by then.

Right, I forgot I wasn't dealing with clocked logic.

However if you used a multiplexer it would work with a pull up on the input of the multiplexer and a connection to ground through the switch. That way the output would be high unless a closed switch was selected and switch through to the output.

Hm.

So, if I have 8 dip switches...

...and I use a shift register to read them, I will need three pins on the Arduino, and I'll need one pull up resistor for each switch, for a total of eight resistors.
(I can however use a 9 pin resistor array to save myself a whole lot of soldering and a whole lot of space on the board.)

...but, if I use a multiplexer, I'll need four pins on the Arduino. However, I'll need only one pullup resistor connected to the input pin.

But wait. What about those internal pullup resistors? I see why they won't work for the shift register, but what about the multiplexer? Do I really need an external pullup on that common input/output?

Hm...

It occurs to me that I should be able to read up to 10 switches using a 4017 decade counter and just two pins on the Arduino.

Here's what I'm thinking:

Connect one pin of the arduino to the 4017's clock line, and to a 10K pulldown. Connect the 10 outputs of the 4017 to your switches. Connect the 10 switches to a single input pin on the Arduino.

Set the 4017 clock pin high. Read the input pin. The input pin will be high if the first switch is on. Toggle the clock pin. Read the second switch. Continue until all switches are read.

Something I am uncertain about is whether or not it is bad that the pin on the 4017 which has gone high will be connected to another pin on it which is low if any two switches are on at once. I suspect that this is in fact, a no-no, and that simply adding a resistor there won't solve the issue. However, adding diodes to each pin should fix that.

I'm also not sure if the reset or enable pins of the 4017 would need to be pulled low.

I know grumpy said it was bad practice to connec tthe switches to +5v, so this is just a though exercise. Especially since it would require all those diodes. But if you can get 10 switches down to two pins, that ain't bad.

I wonder though... speaking of two pins... I think the I2C standard uses two pins. I wonder if there is a shift register that uses that standard that would work in this instance.

Still got all those darn resistors to deal with if I go the shift register route though. :slight_smile:

Connect one pin of the arduino to the 4017's clock line, and to a 10K pulldown

No need for a pull down res here as the Arduino pin is an OP.

I'm also not sure if the reset or enable pins of the 4017 would need to be pulled low.

While not strictly necessary if you can ensure the state of the 4017 at power up (would need two more components to reset it or run its RST pin to the AVR's) I think it's bad practice to do this, if somehow you get out of sync you'll get all sorts of wrong readings. You really should reset the chip before each cycle, which means another pin.

it is bad that the pin on the 4017 which has gone high will be connected to another pin on it which is low if any two switches are on at once.

Yep, not good. You would have to be sure two switches never get pressed at the same time.

However, adding diodes to each pin should fix that.

Right, but now you have 10 components.

I wonder if there is a shift register that uses that standard that would work in this instance.

There are IO expanders that can do all this, see the MCP23017, 16 IO, pullups included, i2c or spi interface.

There are IO expanders that can do all this, see the MCP23017, 16 IO, pullups included, i2c or spi interface.

Interesting. Looking at the datasheet it's a bit more than I need though. 16 input/outpts... quite a few pins.

Looks like it'd be hard to get too; only got one solid hit on Mouser. And $1.23 a chip. Guess that's not too much more than the shift register plus a resistor array though. Multiplexer at only $0.52 plus one maybe 10K resistor beats them both handily however. Plus I like the simplicity. Guess I'll have to see if I can spare that extra pin.

Oh and speaking of that 10K resistor on the Multiplexer... I'm still wondering if I can't simply turn on the internal pulldown for that pin.