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
User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8884
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
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 737 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 752 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 10 times
Resistance-calculator-Up-to-9-Switches-V2.xls
(14 KiB) Downloaded 17 times
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

mnf
Valued Contributor
Valued Contributor
Posts: 738
Joined: Wed May 31, 2017 11:57 am
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

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8884
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
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

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

mnf
Valued Contributor
Valued Contributor
Posts: 738
Joined: Wed May 31, 2017 11:57 am
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

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8884
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
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

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

User avatar
Steve001
Valued Contributor
Valued Contributor
Posts: 1145
Joined: Wed Dec 31, 2008 3:37 pm
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.

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8884
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Contact:

Re: Connecting 2 to 9 switches on one ADC pin

Post by medelec35 »

Your welcome Steve.
Thank you.
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

User avatar
Benj
Matrix Staff
Posts: 14848
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Contact:

Re: Connecting 2 to 9 switches on one ADC pin

Post by Benj »

Brilliant and easy to follow, thanks for sharing :D

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8884
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
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

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

jgu1
Posts: 1189
Joined: Tue Oct 06, 2009 9:39 am
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:

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8884
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
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

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

jgu1
Posts: 1189
Joined: Tue Oct 06, 2009 9:39 am
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: 134
Joined: Fri Nov 07, 2014 12:48 pm
Location: Pune, India
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

Post Reply