HeavensBlade
Posts: 26
Joined: Fri Aug 03, 2012 10:45 am

Switch array layout.

Sun Jan 13, 2013 9:03 pm

Hey,
I want to create a array with a few switches but I don't want to use one GPIO per switch.
I have created a layout:Image
The question I have now is what diode should I use?
I thought about some 1N4001 but even after a few articles I don't seem to know how a diode works. And with those resistors the current is to about 0.03 mA. Can such a diode forward such a little voltage?

User avatar
Burngate
Posts: 6347
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: Switch array layout.

Mon Jan 14, 2013 10:24 am

How a diode works: I think it would be best not to go into that here - we'd be here for hours ;)

1N4001 is a bit overkill for this. I'd use 1N4148
http://cpc.farnell.com/_/1n4148/diode-h ... is%20Range
As you can see it's quite cheap - less than a penny each, and would handle up to 150mA, which is far more than you need.
It's of a type known as a small signal diode, whereas the 1N4001 is more for power

Another point: I don't think you need all those resistors, just the four pull-down ones. And the four diodes you've put in series with those four aren't needed

Consider what happens when you close one switch. Without those resistors, one or more GPIOs is pulled up to 3v3 (less one diode voltage, about 0v6), leaving the others to be pulled down to 0v. If you put in the resistors, then you've got a potential divider, so the GPIOs will only reach 1v7, maybe not enough. If you wish to protect the GPIOs from errors such as making one an output and driving it low, you could consider using eg. 330Ω - enough to keep the current down to 10mA whilst not reducing the maximum volage by much.

Also, I would consider reducing the pull-down resistors to 10k. The higher the value you choose, the more prone the circuit will be to picking up mains hum and the like. 1N4148 diodes will work with 0.3mA or 0.03mA in either case

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4257
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: Switch array layout.

Mon Jan 14, 2013 10:35 am

It's probably a good idea to put resistors between the GPIO and 3.3V, in case you make one of the GPIOs an output and drive it low by accident. But that doesn't have to be where you have them. Four 1k resistors on the left of the diagram where you have "a b c d" would be fine.

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: Switch array layout.

Mon Jan 14, 2013 10:40 am

I dont know what you are trying to achieve here.

It looks like you have a set of switches that produce a different code when the switch is pressed.

However it will NOT work if more than one switch is pressed at a time. So with two switches at the same time you will get a MIX of the codes.

Normally people use switches with coding inside the switch to do this like Then put 4 pull up resistors around 10k on each input line and connect the switch common to ground

A lot less wiring and chance of mistakes, saves time and money.

These are known as BCD and Hex encoded switches (usually rotary)
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: Switch array layout.

Mon Jan 14, 2013 10:44 am

Also the large size of the pull DOWN resistors and the diodes could cause the GPIO pins not to go low enough to register as 0.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

HeavensBlade
Posts: 26
Joined: Fri Aug 03, 2012 10:45 am

Re: Switch array layout.

Mon Jan 14, 2013 12:06 pm

@Burngate
Now thats what the layout looks now,
Image
would that be ok?

When looking at the diode specs it says:
Forward Voltage VF Max: 1V
What does that mean? Wont the 3V3 blow the diode?

@techpaul
I want to make a panel with a few push-buttons. I don't know if I need 15, but with four channels I have the possibility to use them later.
I'm using WebIOPi with my own python script to interpret the code transmitted from the GPIO`s and execute a command.
I know that this wont work when more than one buttons are pressed but this thing is not going to be used for anyone other than me.

P.S.: I'm sorry for writing switch in the headline instead of push-button. I guess thats where the confusion came from.

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4257
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: Switch array layout.

Mon Jan 14, 2013 12:39 pm

Those 330 ohm resistors worry me. Check the GPIO specs to see if a low output can sink 10mA, and four of them can sink a total of 40mA. I think the total figure is too much, which is why I said 1k.

There will not be 3.3V across the diodes. When they are conducting (ie the voltage is "forward") they will have about 0.6V to 1V across them. The rest of the voltage is across the 10k resistors and the GPIO input, which is where you want it.

Looking at the data sheet of the 1N148, the VF figure is in "Electrical characteristics", which is a description of how the device behaves, ie it's the manufacturer's promise to you. The limits of what you can do to the device, your promise to the manufacturer, are in the previous table, "Limiting Values", and include such things as the maximum current you can ask it to conduct and the maximum Reverse voltage you can put across it.

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: Switch array layout.

Mon Jan 14, 2013 1:00 pm

Why not just use an I2C GPIO expander like PCF8575 which 16 pins on it then you need 4 wires to the front panel
  • +5V
  • GND
  • SCL
  • SDA
One chip, with 16 switches to ground and 16 pullup resistors. Simpler to wire, you can read 16 switches at once which can cope with any number of switches pressed at once

If you must have the value as 4 bits the classic circuit is 2 x 74*148 and 1 x 74*00 see the datasheet http://www.ti.com/lit/gpn/sn74ls148 page 13
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

User avatar
gordon@drogon.net
Posts: 2022
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website Twitter

Re: Switch array layout.

Mon Jan 14, 2013 1:24 pm

HeavensBlade wrote:Hey,
I want to create a array with a few switches but I don't want to use one GPIO per switch.
I have created a layout:Image
The question I have now is what diode should I use?
I thought about some 1N4001 but even after a few articles I don't seem to know how a diode works. And with those resistors the current is to about 0.03 mA. Can such a diode forward such a little voltage?
You can do the whole thing with no resistors and no diodes - if you want to. (Why do you think you need the diodes in this instance?)

The down-side of this way (and yours) is that you can only detect one button pushed at a time. If you push 2 (or more) buttons then you'll get a false reading. (Predictable, but false).

Another down-side is that if you're clumsy enough to make one of the input pins an output and drive it to +3.3v then push a button then it can potentially burn-out the pin and/or the Pi. You can protect yourself with anything from 64Ω to 1000Ω on an input pin. (but no more than 1000Ω as you'll need to use the internal pull-up resistors here which are ~10KΩ)

But if you can live with the limitation of only pushing one button at a time, then it's OK. set the 4 pins as inputs, enable the 4 internal pull-up resistors, common the switches to 0v and read the 4 bits - combine them together and you'll get a binary number corresponding to the switch that was pressed. (0-15, with 0 being nothing pushed.) The input here is inverted though, but that's easy to fix in software - ie. it will actually read 15 with no buttons pushed.

And if you trust yourself to never set the 4 GPIOs you use as outputs *and* push a button at the same time, then you can do away with the 4 "I don't trust myself" protection resistors too.

This is a very simple project - don't make it overly complicated. You don't need the diodes, you don't need extra resistors. Keep it simple and there is less to go wrong. (Google "KISS principle")

Image

If you do need to detect 2 switches pushed at once, (or >15 switches) then you will need more GPIO pins (another 4) and diodes - look up matrix keyboards for examples.

-Gordon
--
Gordons projects: https://projects.drogon.net/

Hoagie
Posts: 119
Joined: Sun Sep 23, 2012 7:19 pm

Re: Switch array layout.

Mon Jan 14, 2013 1:35 pm

gordon@drogon.net wrote:
HeavensBlade wrote:Hey,
I want to create a array with a few switches but I don't want to use one GPIO per switch.
I have created a layout:Image
The question I have now is what diode should I use?
I thought about some 1N4001 but even after a few articles I don't seem to know how a diode works. And with those resistors the current is to about 0.03 mA. Can such a diode forward such a little voltage?
You can do the whole thing with no resistors and no diodes - if you want to. (Why do you think you need the diodes in this instance?)

The down-side of this way (and yours) is that you can only detect one button pushed at a time. If you push 2 (or more) buttons then you'll get a false reading. (Predictable, but false).

Another down-side is that if you're clumsy enough to make one of the input pins an output and drive it to +3.3v then push a button then it can potentially burn-out the pin and/or the Pi. You can protect yourself with anything from 64Ω to 1000Ω on an input pin. (but no more than 1000Ω as you'll need to use the internal pull-up resistors here which are ~10KΩ)

But if you can live with the limitation of only pushing one button at a time, then it's OK. set the 4 pins as inputs, enable the 4 internal pull-up resistors, common the switches to 0v and read the 4 bits - combine them together and you'll get a binary number corresponding to the switch that was pressed. (0-15, with 0 being nothing pushed.) The input here is inverted though, but that's easy to fix in software - ie. it will actually read 15 with no buttons pushed.

And if you trust yourself to never set the 4 GPIOs you use as outputs *and* push a button at the same time, then you can do away with the 4 "I don't trust myself" protection resistors too.

This is a very simple project - don't make it overly complicated. You don't need the diodes, you don't need extra resistors. Keep it simple and there is less to go wrong. (Google "KISS principle")

Image

If you do need to detect 2 switches pushed at once, (or >15 switches) then you will need more GPIO pins (another 4) and diodes - look up matrix keyboards for examples.

-Gordon

That doesn't look right. The bottom switch connects all 4 input lines together, so any switch will activate all 4 lines going through this link.

User avatar
Mortimer
Posts: 926
Joined: Sun Jun 10, 2012 3:57 pm

Re: Switch array layout.

Mon Jan 14, 2013 1:38 pm

Sorry but the diodes are essential, otherwise any key pressed will result in a key 15 pressed result, as the 15th key connections short all the GPIO sense lines together.

-- Hoagie beat me to it.!
--------------
The purpose of a little toe is to ensure you keep your furniture in the right place.

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4257
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: Switch array layout.

Mon Jan 14, 2013 1:40 pm

That does not work. There are sneak paths that allow the current to flow to all lines. If you press any button, all of the inputs will be zero.

Say you press "2". That connects 0V to input 2. Input 2 is also connected to switches 3, 6, 7, 10, 11, 14 and 15. The wiring for switch 15 is connected to all input lines. So all the input lines are connected to 0V. So you take out switch 15. But still the wiring for switch 3 connects input 2 to input 1, and the wiring for switch 6 connects input 2 to input 3, and the wiring for switch 10 connects input 2 to input 4, so all lines are still connected to 0V.

And that is why the diodes are there.
-- Hoagie beat me to it.!
And me.

User avatar
gordon@drogon.net
Posts: 2022
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website Twitter

Re: Switch array layout.

Mon Jan 14, 2013 1:43 pm

Gah. I'm obviously having a bad day - ignore my ramblings!!!

I've just been (re) making some matrix keyboards where I don't need the diodes and obviously have my head screwed on the wrong way round today!

-Gordon
--
Gordons projects: https://projects.drogon.net/

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4257
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: Switch array layout.

Mon Jan 14, 2013 1:50 pm

Diodes are useful in keypad matrices too. Consider a simple Hex keypad, for want of something else to use:

Code: Select all

0 1 2 3
4 5 6 7
8 9 A B
C D E F
If you press 2, 0 and 8 simultaneously, the software will see a 2, a 0, an 8... and an A. If you put diodes in series with each switch, you can push any combination of keys and read them accurately.

User avatar
gordon@drogon.net
Posts: 2022
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website Twitter

Re: Switch array layout.

Mon Jan 14, 2013 2:11 pm

rurwin wrote:Diodes are useful in keypad matrices too. Consider a simple Hex keypad, for want of something else to use:

Code: Select all

0 1 2 3
4 5 6 7
8 9 A B
C D E F
If you press 2, 0 and 8 simultaneously, the software will see a 2, a 0, an 8... and an A. If you put diodes in series with each switch, you can push any combination of keys and read them accurately.
I know that too, and did allude to it in my goofy posting, but the project I'm doing right now only needs one key at a time, so that's fine for me. I've just built up 3 little keypads for a little project, so head is slightly in the wrong place.. Or I've just been sniffing at the solder flux again ;-)

-Gordon
--
Gordons projects: https://projects.drogon.net/

HeavensBlade
Posts: 26
Joined: Fri Aug 03, 2012 10:45 am

Re: Switch array layout.

Mon Jan 14, 2013 2:22 pm

Ok, I think I should try it with the I2C chip. I thought this would be the easiest thing to do for me because I have not that much experience in programming and electronics. I think I will buy the 74*148 and try out a few tutorials for learning the handling of a bus system.

Edit:
sorry, I meant I will buy the PCF8575 not the 74*148

HeavensBlade
Posts: 26
Joined: Fri Aug 03, 2012 10:45 am

Re: Switch array layout.

Mon Jan 14, 2013 3:00 pm

Hmm, since there is no THT version of the chip available, I think about ordering something like
http://www.abelectronics.co.uk/products ... ter-boards

Hoagie
Posts: 119
Joined: Sun Sep 23, 2012 7:19 pm

Re: Switch array layout.

Mon Jan 14, 2013 8:36 pm

Matrix keypad is another idea. 4x4 keys would use 8 gpio lines an no extra components. 3x4 phone style would need 7 gpio pins.

Imagine the switches arranged in a grid. Connect each row to an output and each column to an input using the internal pulldown resistors. The switch connects the row to the column when pressed. Set one row to 1 and the others to 0. Read the inputs. Now set the next row to 1 and the others to 0. Read again. After all rows have been checked, you know the row and column of the key.

You can also set all rows initially high and wait fir an interrupt. Then scan for the key.

Return to “Automation, sensing and robotics”