How-to
How to Wire Eight Stepper Motor Buttons Through a Multiplexer
Short answer
Eight direction and mode buttons for two stepper motors fit on 4 PIC pins: an 8-to-1 multiplexer with three select outputs and one data input. Wire each switch to ground behind a pull-up, read logic 0 as pressed, and scan one channel per timer tick, accepting a change only after several identical samples.

On this page
- How many PIC pins do eight stepper buttons take?
- Which expansion part should you pick for eight buttons?
- How do you wire the multiplexer to the PIC?
- How does the firmware scan and debounce the buttons?
- How long does a press take to register?
- How do you change stepping mode or direction without stalling?
- How do you confirm each button maps to one action?
- Questions asked
Eight operator buttons for two stepper motors — jog left, jog right, half/full-step mode and a spare per motor — fit on four PIC pins with an 8-to-1 multiplexer. Three output pins carry the binary channel address and one input pin reads whichever switch that address selects. Wire every pushbutton between its multiplexer input and ground, fit a pull-up per input so an open switch reads logic 1, and treat logic 0 as pressed. Scan one channel per timer tick and accept a state change only after several identical samples. The steps below give the pin map, the settle and debounce arithmetic, the mode-change rule that avoids a stall, and the bench checks that prove each button maps to exactly one action.
How many PIC pins do eight stepper buttons take? #
Eight buttons through an 8-to-1 multiplexer cost four PIC pins: three outputs drive the select address and one input reads the selected switch, against eight pins if each button gets its own. The PIC never drives the motor coils itself — a driver stage does, for the reasons in why a stepper motor will not turn from microcontroller pins alone — so step and direction outputs, the drive connections and the supply pins consume most of a small package before the first button is connected. The direct-wiring workarounds each fail for a specific reason:
- Paralleling several buttons on one pin: the pin sees the same level whichever switch closes, so firmware cannot tell them apart.
- A resistor ladder into a digital input: a digital pin has one threshold and resolves two states; eight levels need an ADC channel and resistor tolerances tight enough to separate them, which motor-driver supply noise then erodes.
- Sharing a coil-drive line with a button input: driver current and switching edges land on the input pin and can damage it.
- Polling faster: the problem is pin count and contact bounce, not scan rate.
Which expansion part should you pick for eight buttons? #
Pick the 8-to-1 multiplexer: it needs four pins and no serial protocol, while a 74HC165 or CD4021 shift register also works with three pins — shift clock, serial data and a load line — and an I2C expander needs bus code and addressing. The shift register samples all eight switches at once, which pays off only when the count grows well past sixteen; the 3-to-8 decoder with one read-back pin scans exactly like the multiplexer but saves nothing. For eight switches the multiplexer is the least code and the least wiring. Be clear about what it does: it reads operator commands. Producing the step pulses those commands gate is a separate job, covered in running a stepper motor from discrete logic gates.
How do you wire the multiplexer to the PIC? #
Connect S0, S1 and S2 to three PIC pins configured as digital outputs, route the multiplexer output Y to one pin configured as a digital input, tie the enable pin to its active level, and run the part from the PIC's own supply rail. The full hookup, in order:
- Set three port pins as outputs for the select lines S0 to S2 and one as the input for Y — on a PIC that means the TRIS direction registers — keeping them clear of the step and direction outputs.
- Tie the enable pin to the level its datasheet pin table calls active, so the output is always driven.
- Power the multiplexer from the same rail as the PIC and place a 100 nF decoupling capacitor at its supply pins; a shared rail guarantees the output high and low levels satisfy the PIC's VIH and VIL rows with no level shifter.
- Wire each pushbutton between its data input — I0 through I7 — and ground.
- Fit a pull-up resistor from every data input to the supply; 10 kohm is a typical starting value, a rule of thumb, sized properly against input leakage and cable length with the method in sizing pull-up resistors for PIC keypad inputs. A digital multiplexer has no internal bias, so without the resistor an open switch leaves its input floating and reading random levels.
- Freeze the channel assignment before writing any code.
| Channel | Switch | Firmware action |
|---|---|---|
| I0 | Motor 1 left | Direction flag while held |
| I1 | Motor 1 right | Direction flag while held |
| I2 | Motor 1 mode | Toggle half/full stepping on press |
| I3 | Motor 1 spare | Unassigned |
| I4 | Motor 2 left | Direction flag while held |
| I5 | Motor 2 right | Direction flag while held |
| I6 | Motor 2 mode | Toggle half/full stepping on press |
| I7 | Motor 2 spare | Unassigned |
The grouping is an arrangement for this project, not a requirement; firmware only needs the table to match the wiring. With the pull-ups in place an open switch reads logic 1 and a pressed switch reads logic 0, so the code treats 0 as pressed.
How does the firmware scan and debounce the buttons? #
Advance one channel per timer tick: write the next address on S0 to S2 at the end of one tick and read Y on the following tick, so the multiplexer settles for a whole timer period and the main loop never busy-waits. Drive the scan from a periodic timer interrupt, not a delay loop, so step pulse generation is never stalled. Each channel keeps a short history of raw samples and is called pressed only after several consecutive identical samples, because mechanical contacts bounce for a few milliseconds after closing; the window that sample count creates must exceed the bounce you measure on your own switches.
Illustrative pseudo-code, run once per tick. ch is a 3-bit channel index starting at 0, hist an 8-bit sample history for each channel initialised to all released, raw the bit read from the Y pin, and pressed the debounced state for each channel, cleared to released at start-up:
raw = read Y pin // 0 = pressed, 1 = released
hist[ch] = (hist[ch] << 1) | raw // newest sample enters bit 0
if (hist[ch] & 7) == 0: pressed[ch] = 1 // three lows in a row
if (hist[ch] & 7) == 7: pressed[ch] = 0 // three highs in a row
ch = (ch + 1) & 7
write S2, S1, S0 = ch // address for the next tick's read
Handle the mode button as an edge: toggle half or full stepping only on the released-to-pressed transition of pressed[ch], so holding the button cannot retoggle it every frame. Direction buttons are levels: the direction flag follows pressed[ch], and the step generator keeps running at its own timer rate.
How long does a press take to register? #
With a 1 ms tick, one channel per tick and three matching samples, the eight-channel frame takes 8 ms and a press is accepted within about 32 ms of contact — a delay no operator can distinguish from immediate. The arithmetic, a calculated example for those inputs rather than a measurement:
- Frame time: 8 channels × 1 ms per channel = 8 ms per full scan.
- First sample of a new press: at worst one full frame after contact.
- Acceptance: 3 consecutive low samples, one per frame = 24 ms.
- Worst-case total: one frame plus 24 ms = 32 ms from contact to a registered press.
Change the tick or the sample count and every figure scales in proportion. The same arithmetic sets the debounce window at 24 ms per channel, comfortably longer than the few-millisecond bounce of a panel pushbutton — confirm yours with a scope. The 1 ms gap between writing an address and reading it also exceeds any propagation delay a multiplexer timing table lists by a wide margin; take the exact figure from the datasheet of the part you buy.
How do you change stepping mode or direction without stalling? #
Toggle half and full stepping only on the press edge, only at a sequence index that exists in both step tables, and only after a ramp or a brief stop when the load carries inertia; reverse direction the same way. A full-step sequence advances two positions for each half-step table index, so a change made mid-sequence can land on an index that is not valid for the new mode: the coil pattern jumps, and the motor skips a step or stalls. Apply the new mode at the next index the two tables share, the full-step positions. Reversal obeys the same physics — the rotor cannot change direction instantly at speed — so stop pulses briefly or ramp down before flipping the direction flag; from standstill the reverse is immediate.
Keep driver noise out of the scan: route switch wires short, or twisted with a ground return, and on a panel cable add a small RC network per line sized for the debounce window, letting the firmware reject what remains. Share one common ground between driver supply and PIC, but run motor return current on its own path, never through the logic ground.
How do you confirm each button maps to one action? #
Press each button in turn with the drives disabled and log the channel index: exactly one index must change per press, the output must read logic 1 on every channel when nothing is held, and a held pair on different motors must register both. Disabling the drives first is the one rule of this bench session — a jog button pressed during a test can move an axis without warning. Then work through the checks:
- Press each switch and log the channel that fires. One channel per press; two firing means a shorted input or a swapped select line.
- Scope the Y line while the firmware cycles the addresses with nothing pressed. It must sit at logic 1 on every channel; a channel that floats or oscillates has a missing pull-up.
- Press and release one switch while watching that line, and measure how long the contact bounces. The debounce window must be longer than the measured bounce.
- Hold one button from each motor at the same time. Both must register, because the channels are scanned independently.
- Run a motor slowly and toggle the mode at the transition. Any skipped step or stall means the change is landing on an index outside the shared full-step positions.
- Command a reversal at speed, then again after a ramp. Only the ramped path is allowed to stay clean; the at-speed result confirms why the firmware must ramp first.
When all six pass, the design is finished: eight buttons on three select lines and one data input, one action each, and a mode change that cannot stall the motor.
Questions asked
Can one pull-up on the multiplexer output replace the eight input pull-ups?
Yes, when every switch goes to ground: the selected input is pulled up through the multiplexer's own signal path, so the common output reads 1 for an open switch and 0 for a closed one. The eight separate resistors remain the safer default, because they also hold the unselected inputs at a defined level instead of leaving them floating.
How do you read sixteen buttons on the same PIC?
Use two 8-to-1 multiplexers with their select lines S0, S1 and S2 wired in parallel, and give each part its own data input pin. Sixteen switches then cost five PIC pins, the scan advances the shared address once per tick, and each tick reads both data pins, so the frame time stays what it was with one part.
Does the 10 kohm pull-up starting value change on a 3.3 V PIC?
Keep it as the starting value: at 3.3 V a closed switch sinks about 0.33 mA through the resistor, which any CMOS input stage tolerates. Go lower only if long panel cables or the multiplexer's input leakage pull the low level up, and check the result against the VIH and VIL rows of both datasheets.
What happens if the firmware reads the output right after changing the address?
Until the multiplexer's propagation delay has passed, the output can still show the previous channel, so a press is credited to the neighbouring switch. Writing the address on one tick and reading on the next removes the hazard, because a whole timer period separates the two events.

