I am looking for a simple example how to wire and initialize the CD4021 mux. I tried using code from the Field, but it didn’t work. The Field uses cascaded CD4021s, which complicates its usability as an example.
Maybe this will be helpful, page down to ShiftIn11 for the code:
This file has been truncated.
author: 'Carlyn Maw, Tom Igoe'
title: 'CD4021B Shift Registers'
description: 'A list of examples to modify CD4021B shift registers'
tags: [Shift Registers]
> Updated by Scott Fitzgerald Feb 2014
## Shifting In & the CD4021B
Sometimes you'll end up needing more digital input than there are pins on your Arduino. Using a parallel to serial shift register you can collect information from 8 or more switches while only using 3 of the pins on your Arduino.
An example of a parallel to serial register is the CD4021B, sometimes referred to as an 8-Stage Static Shift Register. This means you can read the state of up to 8 digital inputs attached to the register all at once. This is called Asynchronous Parallel Input. "Input" because you are collecting information. "Parallel" because it is all at once, like hearing a musical cord. "Asynchronous" because the CD4021B is doing all this data collection at its own pace without coordinating with the Arduino.
The 8 inputs are translated into a series of HIGH and LOW pulses on the serial-out pin of the shift register. This pin should be connected to an input pin on your Arduino Board, referred to as the **data pin**. The transfer of information on the data pin is called "Synchronous Serial Output" because the shift register waits to deliver linear sequence of data to the Arduino until the Arduino asks for it. Synchronous Serial communication, either input or output, is heavily reliant on what is referred to as a **clock pin.** The clock pin is the metronome of the conversation between the shift register and the Arduino, it is what keeps the two systems synchronous. Every time the Arduino changes the clock pin from LOW to HIGH the shift register changes the state of the Serial Output pin, indicating the value of the next switch.
The third pin attached to the Arduino is a "Parallel to Serial Control" pin. It is referred to as a **latch pin**. When the latch pin is HIGH the shift register is listening to its 8 parallel inputs. When the latch pin is LOW, it listens to the clock pin and passes information serially. That means every time the latch pin transitions from HIGH to LOW the shift register will start passing its most current switch information.
The pseudo code to coordinate this all looks something like this: