74HC4051
The 74HC4051 is an 8-channel analog multiplexer (NXP 74HC4051, TI CD74HC4051). Three GPIO select lines connect one of eight analog inputs to one ADC channel. Boards use it to read many knobs or sensors with one ADC input.
Status at a glance
| Aspect | Status |
|---|---|
| Descriptor | configs/devices/74hc4051.yaml (primitive analog_mux, engine crates/core/src/peripherals/components/declarative_analog_mux.rs) |
| Aliases | cd74hc4051, cd4051 |
| Bus | Analog: Z drives one ADC channel; S0..S2 and E are MCU GPIO pads |
| Test fixture | crates/core/tests/fixtures/analog_mux/ (e2e: tests/analog_mux_e2e.rs) |
| Tier | modeled |
Pins
| Part pin | Connect to | Notes |
|---|---|---|
| Y0..Y7 | analog sources | a source names the mux as connection: and a Y input as channel |
| Z | ADC channel | connection: (the ADC id) and config.channel |
| S0, S1, S2 | GPIO outputs | s0_pin, s1_pin, s2_pin: chip pad labels |
| E | GPIO output or GND | e_pin, active LOW. Leave it out when E is tied to GND |
Behaviour
- Datasheet function table: Yn = S2·4 + S1·2 + S0 connects to Z.
- E HIGH opens all switches. Z is then open, and the model drives 0 mV onto the ADC channel (a pull-down on Z is assumed).
- The model reads the PAD level of each select line from the GPIO model. A store to a GPIO output register re-routes the mux inside that store, so a conversion that the next instruction starts converts the new channel.
- A stimulus on the routed source moves the ADC channel at once. A stimulus on a source that is not routed changes nothing until a select change routes it.
- A mux can feed another mux: set
connection:to the other mux id.
Example
external_devices:
- id: "knob_mux"
type: "74hc4051"
connection: "adc1"
config: { channel: 3, s0_pin: "PA4", s1_pin: "PA5", s2_pin: "PA6" }
# Declare each mux before the parts that name it.
- { id: "knob0", type: "potentiometer", connection: "knob_mux", config: { channel: 0, position: 25 } }
- { id: "knob1", type: "potentiometer", connection: "knob_mux", config: { channel: 1, position: 75 } }
Turn a knob in a test script (position is in %, 0..100):
stimuli:
- target: { component: "knob1", channel: "position" }
trigger: !after_cycles { cycles: 1000000 }
value: 10
Errors
The build of the system fails (no silent fallback) when:
- a select or enable pad is not a GPIO pad of the chip, or the GPIO model cannot report its level;
- a source uses a Y input above 7;
- a source names a mux that is declared after it.
Limitations
Not modelled: ON resistance, switch time (t_on is about 20 ns, much shorter than one ADC sample time), charge injection, and Z used as an input (demultiplexer direction).