Toggle-LEDs
This example demonstrates the use of push buttons and LEDs and how to use shift registers and analog multiplexers to save pins.
- Boards: 🛈
- AVR, AVR USB, Nano Every, Nano 33 IoT, Nano 33 BLE, UNO R4, Pi Pico, Due, Teensy 3.x, ESP8266, ESP32
Connections
- SS: 74HC595 ST_CP
- MOSI: 74HC595 DS
- SCK: 74HC595 SH_CP
- 2: 74HC4067 A (COM OUT/IN)
- 3: 74HC4067 S0
- 4: 74HC4067 S1
- 5: 74HC4067 S2
- 6: 74HC4067 S3
Connect 16 momentary push buttons between the input pins of the multiplexer and ground.
The internal pull-up resistor for the buttons will be enabled automatically, so no external resistors are necessary.
Connect 16 LEDs (and series resistors) between the eight outputs of the two shift registers and ground.
Remember to connect the enable pins of both the multiplexer and the shift registers to ground in order to enable them. Also connect the master reset pin of the shift registers to Vcc.
Connect the serial data output of the first shift register (QH' or Q7S) to the serial input of the second shift register.
Behavior
Pressing the first button will turn on the first LED. Pressing it again will turn it off again. Pressing the second button will turn on the second LED. Pressing it again will turn it off again, and so on.
Written by PieterP, 2018-08-28
https://github.com/tttapa/Arduino-Helpers
2,
{3, 4, 5, 6},
};
auto buttons =
void setup() {
sreg.begin();
for (
Button &button : buttons)
button.begin();
}
void loop() {
for (uint8_t i = 0; i < mux.length(); ++i)
if (buttons[i].update() == Button::Falling)
sreg.digitalWrite(i, sreg.digitalRead(i) ?
LOW :
HIGH);
}
constexpr PinStatus_t LOW
constexpr PinStatus_t HIGH
Dummy header file for Arduino builder.
void begin() override
Initialize the multiplexer: set the pin mode of the address pins and the enable pin to output mode.
A class for serial-in/parallel-out shift registers, like the 74HC595 that are connected to the SPI bu...
Array< T, N > generateIncrementalArray(U start=0, V increment=V(1))
Generate an array where the first value is given, and the subsequent values are calculated as the pre...
AnalogMultiplex< 4 > CD74HC4067
An alias for AnalogMultiplex<4> to use with CD74HC4067 analog multiplexers.
uint_fast16_t pin_int_t
Integer type used internally to store the index of (extended) GPIO pins.