Page 1 of 1

Connecting 2 to 9 switches on one ADC pin

Posted: Sat Jun 20, 2020 1:12 am
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 22751 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 22766 times
Note: Can only operate one switch at a time.
New ADC values are required for multiple switch detection.

Re: Connecting 2 to 9 switches on one ADC pin

Posted: Sat Jun 20, 2020 6:41 am
by mnf
Thanks Martin,

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

Martin

Re: Connecting 2 to 9 switches on one ADC pin

Posted: Sat Jun 20, 2020 9:55 am
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.

Re: Connecting 2 to 9 switches on one ADC pin

Posted: Sat Jun 20, 2020 1:12 pm
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

Re: Connecting 2 to 9 switches on one ADC pin

Posted: Sat Jun 20, 2020 2:44 pm
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

Re: Connecting 2 to 9 switches on one ADC pin

Posted: Sat Jun 20, 2020 7:35 pm
by Steve001
Hi Martin,

Very useful thank you

Steve

Re: Connecting 2 to 9 switches on one ADC pin

Posted: Sat Jun 20, 2020 9:37 pm
by medelec35
Your welcome Steve.
Thank you.

Re: Connecting 2 to 9 switches on one ADC pin

Posted: Mon Jun 22, 2020 2:54 pm
by Benj
Brilliant and easy to follow, thanks for sharing :D

Re: Connecting 2 to 9 switches on one ADC pin

Posted: Mon Jun 22, 2020 5:17 pm
by medelec35
Thanks Ben,
You're welcome.
Saves changing microcontroller if not got enough pins.

Re: Connecting 2 to 9 switches on one ADC pin

Posted: Thu Jun 25, 2020 1:11 pm
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:

Re: Connecting 2 to 9 switches on one ADC pin

Posted: Thu Jun 25, 2020 3:41 pm
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.

Re: Connecting 2 to 9 switches on one ADC pin

Posted: Thu Jun 25, 2020 3:58 pm
by jgu1
Thank´s Martin. I have not seen it. You get "thump up". :lol:

Br Jorgen

Re: Connecting 2 to 9 switches on one ADC pin

Posted: Thu Jun 25, 2020 6:47 pm
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

Re: Connecting 2 to 9 switches on one ADC pin

Posted: Sat Jul 17, 2021 4:09 pm
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

Re: Connecting 2 to 9 switches on one ADC pin

Posted: Tue Jul 20, 2021 9:23 am
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.