Connecting 2 to 9 switches on one ADC pin

Tips, Tricks and methods for programming, learn ways of making your programming life easier, and share your knowledge with others.

Moderators: Benj, Mods

Post Reply
medelec35
Valued Contributor
Valued Contributor
Posts: 9341
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2562 times
Been thanked: 3773 times
Contact:

Connecting 2 to 9 switches on one ADC pin

Post by medelec35 »

As we know, Flowcode has made programming microcontrollers so much easier than with C code for example.
This tip is keeping with the theme of making things easier.
To do that, I have created a spreadsheet that will allow you to easily connect from 2 to 9 single pole switches, e.g tactile to a single analogue pin.
The spreadsheet should work with any free or paid for software.
For those who have not got such software here is a screen shot:
Required resistors for 2 to 9 Switches.png
Required resistors for 2 to 9 Switches.png (37.54 KiB) Viewed 4614 times
All the if ADC values are dynamically changed, depending on resistor selection.
My hardware consists of 12F1840 + I2C 4x20 LCD.
Of course any microcontroller and peripherals an be used.
The attached Flowchart is an example only.
Circuit:
Schematic for  2 to 9 Switches.png
Schematic for 2 to 9 Switches.png (58.96 KiB) Viewed 4629 times
Note: Can only operate one switch at a time.
New ADC values are required for multiple switch detection.
Attachments
12F1840 9 Switches & LCD v2.fcfx
(16.48 KiB) Downloaded 115 times
Resistance-calculator-Up-to-9-Switches-V2.xls
(14 KiB) Downloaded 122 times
Martin

mnf
Valued Contributor
Valued Contributor
Posts: 1105
Joined: Wed May 31, 2017 11:57 am
Has thanked: 66 times
Been thanked: 423 times
Contact:

Re: Connecting 2 to 9 switches on one ADC pin

Post by mnf »

Thanks Martin,

An excellent resource - much neater than multiplexing the switches...

Martin

medelec35
Valued Contributor
Valued Contributor
Posts: 9341
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2562 times
Been thanked: 3773 times
Contact:

Re: Connecting 2 to 9 switches on one ADC pin

Post by medelec35 »

Thanks Martin.
mnf wrote:
Sat Jun 20, 2020 6:41 am
much neater than multiplexing the switches
Agreed.
The other alternative is to use all the same resistor values e.g 1K2 in series, connected between +VDD & GND.
A high value resistor e.g 820K is connected between ADC pin and GND.
The ADC pin can then be connected to each series resistor junction via momentarily switches.
The disadvantage being very susceptible to electrical noise.
Therefore would strongly recommend using a decoupling capacitor across the high value resistor.
Martin

mnf
Valued Contributor
Valued Contributor
Posts: 1105
Joined: Wed May 31, 2017 11:57 am
Has thanked: 66 times
Been thanked: 423 times
Contact:

Re: Connecting 2 to 9 switches on one ADC pin

Post by mnf »

Hi Martin,

One query - there is some overlap in the checked values (<208 sw 1 >200 sw 2 for example)... This shouldn't pose a problem - button registered should map to first conditional statement...

Unless you just use consecutive if's (and nested is going to get a bit messy for 9 switches) in which case two switches could register?

Martin

medelec35
Valued Contributor
Valued Contributor
Posts: 9341
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2562 times
Been thanked: 3773 times
Contact:

Re: Connecting 2 to 9 switches on one ADC pin

Post by medelec35 »

Hi Martin,
Thank you for your query.
You are spot on!
I just did a calculation so the ADC was in the middle of two ranges which indeed could cause issues if not nested.
I have change the excel sheet so the start in the next value is 1 greater then the end of the previous value.
It does shift from the centre point but should cause less of an issue in the long run.
Updated both files
Martin

User avatar
Steve001
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 460 times
Been thanked: 523 times
Contact:

Re: Connecting 2 to 9 switches on one ADC pin

Post by Steve001 »

Hi Martin,

Very useful thank you

Steve
Success always occurs in private and failure in full view.

medelec35
Valued Contributor
Valued Contributor
Posts: 9341
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2562 times
Been thanked: 3773 times
Contact:

Re: Connecting 2 to 9 switches on one ADC pin

Post by medelec35 »

Your welcome Steve.
Thank you.
Martin

User avatar
Benj
Matrix Staff
Posts: 15257
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4774 times
Been thanked: 4300 times
Contact:

Re: Connecting 2 to 9 switches on one ADC pin

Post by Benj »

Brilliant and easy to follow, thanks for sharing :D

medelec35
Valued Contributor
Valued Contributor
Posts: 9341
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2562 times
Been thanked: 3773 times
Contact:

Re: Connecting 2 to 9 switches on one ADC pin

Post by medelec35 »

Thanks Ben,
You're welcome.
Saves changing microcontroller if not got enough pins.
Martin

jgu1
Posts: 1331
Joined: Tue Oct 06, 2009 9:39 am
Has thanked: 1132 times
Been thanked: 297 times
Contact:

Re: Connecting 2 to 9 switches on one ADC pin

Post by jgu1 »

Wow Martin, perfect and s always useful from you. :wink:

Br jorgen

NB. Why is it not possible to give thump up anymore? :lol:

medelec35
Valued Contributor
Valued Contributor
Posts: 9341
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2562 times
Been thanked: 3773 times
Contact:

Re: Connecting 2 to 9 switches on one ADC pin

Post by medelec35 »

Hi jorgen,
Thank you, its appreciated. :)
jgu1 wrote:
Thu Jun 25, 2020 1:11 pm
Why is it not possible to give thump up anymore?
Because of the the new server & forums update.
DavidA mentioned it here.
Martin

jgu1
Posts: 1331
Joined: Tue Oct 06, 2009 9:39 am
Has thanked: 1132 times
Been thanked: 297 times
Contact:

Re: Connecting 2 to 9 switches on one ADC pin

Post by jgu1 »

Thank´s Martin. I have not seen it. You get "thump up". :lol:

Br Jorgen

User avatar
AbhijitR
Posts: 253
Joined: Fri Nov 07, 2014 12:48 pm
Location: Pune, India
Has thanked: 228 times
Been thanked: 61 times
Contact:

Re: Connecting 2 to 9 switches on one ADC pin

Post by AbhijitR »

Hello!

I do agree with Jorgen, rather it was really very easy to register the appreciation, is it not possible to add that button once again.

By the way Martin let me take this opportunity, a big Thumbs Up, cheers....

Abhi

Frank607
Posts: 172
Joined: Mon Mar 04, 2013 8:07 pm
Has thanked: 28 times
Been thanked: 15 times
Contact:

Re: Connecting 2 to 9 switches on one ADC pin

Post by Frank607 »

Hi Martin,

I have read this topic because it is interesting for me.

I have 8 switches in a remote control box and a cable with 3 wires. 5V, GND and a “signal” wire. This cable is some 7 meters long. The cable is in the open air. (outside) The cable has 3 x 1.5mm wires.

Wil this solution work with this cable length? I have the feeling that this solution is only working for short cables because of the voltage per switch.

Or can I better go for a digital solution such as a DS2408 from MAXIM ?

Thanks,

Frank

medelec35
Valued Contributor
Valued Contributor
Posts: 9341
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2562 times
Been thanked: 3773 times
Contact:

Re: Connecting 2 to 9 switches on one ADC pin

Post by medelec35 »

Hi Frank,
Sorry not tested the resistor version over any lengths of wires.
The idea of the resistors was to make the coding as simple as possible.
The DS2408 sounds like it could work but one wire timing is critical, so how well I don't know.
The other alternative is UART.
That would be easier to implement, but will work over shorter distances of up to 50ft.
If it was my project I would go with UART if I only had three wires, one being for data.
Martin

Post Reply