Looking for ideas for hardware means of configuring a simple numeric value - Page 1
Forum section: electronics / projects

author: HwAoRrDk
author_rank: Super Contributor
timestamp: January 18, 2025, 07:26:34 am
content: I'm looking for some ideas. I have a microcontroller running some code that will need to reference an externally-configured numeric value that is set by the user of the device. The device has no user interface per se - no buttons, display, or any other way of communicating with it. I need to find some mechanism for the user to be able to set that value and for it to be readable by the microcontroller.The value is simply an integer value between 1 and 200, so can be represented by 8-bits. It also doesn't need to accept being changed at run-time, just captured once at start-up. It's mostly a set-and-forget value that will be changed very infrequently.My first thought was an 8-way DIP switch array, with the value being set in binary format. But this isn't particularly user-friendly, and it means dedicating 8 IO pins of the microcontroller to the switches, which may be a squeeze.Another thought I had was three numeric thumbwheel switches, which is great from an ease-of-use point of view (just enter the decimal digits - e.g. "1", "5", "9" for 159), but takes even more microcontroller pins (3×4=12), and they're quite expensive.In a similar vein, there's also rotary code switches. Similar things apply: three switches, one per decimal digit, 12 microcontroller pins required, not particularly cheap.I have a 10-bit ADC available on the microcontroller, so the thought of using a simple potentiometer occurred, but that seems flawed because setting a precise value would be hard (because no immediate feedback), and the usual analog vagaries apply (temperature, drift, noise, etc.).Anybody got any other ideas?

author: Smokey
author_rank: Super Contributor
timestamp: January 18, 2025, 07:33:18 am
content: Is there any display or visual feedback to the user to show what the number is set to?If not, I'd stick with something with physical digit like the thumb wheels.If there is a display, just have two buttons. An "up" button, and a "down" button and make them hold them button until the number they want is shown in the display.

author: Simmed
author_rank: Frequent Contributor
timestamp: January 18, 2025, 07:47:23 am
content: how many io pin do you have to do this ?

author: kripton2035
author_rank: Super Contributor
timestamp: January 18, 2025, 08:06:45 am
content: I would try a rotary encoder 3 pins and a led i2c display 2 pins (if there is no sda/scl already)

author: nfmax
author_rank: Super Contributor
timestamp: January 18, 2025, 08:18:53 am
content: Possibly a phone DTMF keypad connected to the ADC, or even a rotary dial connected to a single input bit could work. But your users will hate you 😉

author: ledtester
author_rank: Super Contributor
timestamp: January 18, 2025, 08:19:33 am
content: Quote from: HwAoRrDk on January 18, 2025, 07:26:34 amYou can use a shift register to capture all of the thumbwheel switch settings and then only use just a few GPIO lines to read them into the MCU.A thumbwheel switch can create a voltage which can be read by the ADC - probably one analog input per digit would be a good idea to achieve decent discrimination.Update: Found this old thread...https://www.eevblog.com/forum/projects/how-to-read-multiple-thumbwheel-switches/

author: ledtester
author_rank: Super Contributor
timestamp: January 18, 2025, 08:23:52 am
content: Quote from: nfmax on January 18, 2025, 08:18:53 amNot as much as requiring them to use Morse code on a capacitive touch switchhttps://www.instructables.com/Morse-Code-with-Capacitive-Touch/

author: HwAoRrDk
author_rank: Super Contributor
timestamp: January 18, 2025, 08:34:23 am
content: Quote from: Smokey on January 18, 2025, 07:33:18 amAs I said, no. The user would be making the setting while the device is powered off, and wouldn't otherwise be able to see the result until they power it on.Quote from: Simmed on January 18, 2025, 07:47:23 amI think Imightbe able to make 12 available, but more likely to be 10.Quote from: kripton2035 on January 18, 2025, 08:06:45 amI can't add any kind of display to this device.Quote from: ledtester on January 18, 2025, 08:19:33 amIndeed, I could use a shift register or some other kind of IO expander (e.g. I2C), but that's one extra chip that if I could get away with doing without, all the better.How would a thumbwheel or rotary coded switch create a voltage output? Some kind of resistor ladder arrangement?

author: ledtester
author_rank: Super Contributor
timestamp: January 18, 2025, 08:43:35 am
content: Quote from: HwAoRrDk on January 18, 2025, 08:34:23 amIf it is the "1 of 10" selector type, use a fixed value resistor between successive outputs and connect Vcc to output 0 and GND to output 9. Then use the ADC to read voltage at the common pin.Vcc --- 0 -/\/\/- 1 -/\/\/- 2 -/\/\/- 3 ... - 9 --- GNDFor a BCD switch I found this idea:https://electronics.stackexchange.com/a/98107but another approach is to use capacitors instead of resistors since parallel capacitance is additive and then use the timing idea from the older thread I mentioned:https://www.eevblog.com/forum/projects/how-to-read-multiple-thumbwheel-switches/msg4412254/#msg4412254The set up is:Vcc --/\/\/-- GPIO -- (parallel caps) -- GNDFirst set the GPIO pin to an output and make is LOW to discharge the caps. Then turn it into an input and time how long it takes to go HIGH.

author: brucehoult
author_rank: Super Contributor
timestamp: January 18, 2025, 08:46:58 am
content: Quote from: HwAoRrDk on January 18, 2025, 07:26:34 amConnect the DIP switch to a shift register and you need three pins: latch, dataIn, clock.In fact for any number of DIP switches, using chained shift registers.But it might be cheaper to just get the same microcontroller in a bigger package with 5 more pins.It might be possible to arrange to have the DIP switch data latched at power on and save a pin, if you only read it once -- or if you connect the output back to the input, but that would make any (highly unlikely, but who knows?) corruption permanent for that run.

author: Benta
author_rank: Super Contributor
timestamp: January 18, 2025, 11:18:13 am
content: If you can live with just 00...99, two BCD-coded switches could do it. If you really need 00...199, add an extra SPST switch for the 100s selection. In total 9 I/Os.https://eu.mouser.com/c/electromechanical/switches/coded-rotary-switches/?output%20code=BCD

author: HwAoRrDk
author_rank: Super Contributor
timestamp: January 18, 2025, 01:39:14 pm
content: Quote from: ledtester on January 18, 2025, 08:43:35 amI'm not seeing any 1-of-10 types; all the 10-position rotary coded switches I'm seeing are BCD, in either binary or Gray code.Looks like I can get a fairly decent voltage delta between each switch position by using approximate halving of the resistor value for each bit position - something like 10k, 4.7k, 2.2k, 1k, and a 1k lower resistor.Position10k4.7k2.2k1kVoutADCval00.0001x0.45922x0.881803xx1.192434x1.563195xx1.783646xx2.004097xxx2.174448x2.505129xx2.62536I think this will only work for binary-coded switches, not Gray-coded, because I don't imagine those could be made to give a progressive set of voltage steps.So far I think three rotary coded switches outputting to 3 pins read by the ADC to get the three decimal digits of the numeric setting are looking like a winner.I can probably just have three holes in the enclosure through to the PCB-mounted switches that the user can poke a screwdriver into to set them. Don't think I would want to use ones with a shaft poking through, as that risks accidental changes during use.

author: ledtester
author_rank: Super Contributor
timestamp: January 18, 2025, 05:52:28 pm
content: I'm glad you have a solution!Just a couple of notes...- you could use a Gray code thumb switch by storing a translation table between the Gray code and its position in the Gray code sequence- since microcontroller ADCs are usually only 8-bit, distinguishing between 256 different voltage values gets pretty iffy. However, the timing method can easily do this by simply extending the timing period. For instance, if the RC constant is 100 microseconds (which for a 10 MHz MCU is a very easy period of time to detect), reading an 8-bit value can be done in 25.6 milliseconds and requires only one GPIO.

author: Nominal Animal
author_rank: Super Contributor
timestamp: January 18, 2025, 06:40:12 pm
content: A separate device with a small display that connects to the target using four pins, programming a tiny EEPROM containing the configuration value?No? Okay, then two/threeAlps Alpine SRBD110401(2.25€ in singles at Mouser) and three 74HC165/74HC166 8-bit parallel-to-serial shift registers would do the job. The shift registers parallel inputs are connected to the rotary switch pins, with pull-down/up resistors on each pin, and the switch wiper connected to VCC/GND. The shift registers' latch pin is connected in parallel to the MCU, with input and clock pins chained (in serial). At startup, the MCU latches the rotary switch state, and clocks in the state bits, say repeated, until two successive reads get the exact same bit sequence, then it parses the bit sequence to the 8-bit number (which is trivial but depends on how you wire the switches; each 10-bit group will have exactly one bit set. You only need three or four input pins: latch/shift, clock, data, and optionally a pin controlling VCC to the shift register and switches, to save power.The SRBD110401 are 10mm × 10mm × 1.7mm SMD rotary switches with detents, wiper connecting to exactly one of the ten output pins. I suggest using a current-limiting resistor on the wiper, to limit the current to 0.25mA – 0.50mA. You'll also need resistor arrays for the pull-downs/ups.

author: tooki
author_rank: Super Contributor
timestamp: January 18, 2025, 07:24:01 pm
content: Quote from: ledtester on January 18, 2025, 08:23:52 amI can’t really criticize a post about a Morse code keyer written by an excited 12-year-old kidwhose last name is Morse.

author: PCB.Wiz
author_rank: Super Contributor
timestamp: January 20, 2025, 01:01:17 am
content: Quote from: HwAoRrDk on January 18, 2025, 01:39:14 pmGray code are not progressive, but that is not a problem to a MCU.I can see uses for Gray coded rotary knobs, but for code switch screw driver set, it matters less.For screwdriver use, binary is actually slightly better, and there is a better choice of load resistor, for 0-9, than what you would choose for 0-15Spice is useful here, you look tomaximize the smallest voltage change.That gives :1K2 2k2 4k7 10k and RL = 642 gives 15-16 step of =161.26108mV/10V and 8-9 step = 253.92818mVHowever, if you change RL, and tune Ratios, the 0-9 ADC margins can be improved to1K15 2k2 4k7 10k RL = 1100R gives 8-9 step of = 272.05515mV/10V ( and 7-8 = 343.99509mV)ie you can improve a BCD ADC from smallest 2.54% to 2.72% step.The simple resistive load does have a cost, of reducing the percentage change because of the voltage divider effect.A current mirror or current monitor can avoid the 1/X and load divider effects, to give larger % steps.More important for > 4 bits of readout.

author: Simmed
author_rank: Frequent Contributor
timestamp: January 20, 2025, 01:06:24 am
content: its just weirdyou cannot have a displayyou dont like DIP arraybinary is inconvenient inputis any of the io an ADC input ?

author: PCB.Wiz
author_rank: Super Contributor
timestamp: January 20, 2025, 01:24:48 am
content: Quote from: ledtester on January 18, 2025, 08:43:35 amThe weakness in that scheme, is the threshold of MCU pins is not well defined.You might get away with calibrating for a specific MCU, and hoping their process is consistent across batches.

author: ledtester
author_rank: Super Contributor
timestamp: January 20, 2025, 01:47:20 am
content: Quote from: PCB.Wiz on January 20, 2025, 01:24:48 amTrue - a lot of MCUs have an internal comparator which also could be used for the timing measurement.

author: kripton2035
author_rank: Super Contributor
timestamp: January 20, 2025, 07:18:00 am
content: Quote from: Simmed on January 20, 2025, 01:06:24 ambcd switches with a shift register seems the best idea here, as mentionned in previous posts