## Wednesday, May 22, 2013

### Circuit Bending Basics 4: Multiplexing Common-Point Buttons

Overview

This post assumes that you have read Circuit Bending Basics 1, 2 and 3.

This post also assumes that you are familiar with Arduino and / or Teensy.

Circuit bending is about the non-thereoretical exploration of sound making circuits via shorting different points together. Take a toy (that is battery powered - important!) and let's get to work!

The aim of this post is to demonstrate how to trigger / emulate button presses of a toy using a microcontroller. In particular, this methodology - of using a multiplexer to emulate button presses - will only work in certain situations (as described below).

Nonetheless, this opens the door for many possibilities, such as MIDI control, oscillator control and so on.

Common-point Buttons
In the previous Circuit Bending Basics article, we looked at how to connect two points on a circuit, thereby emulating button presses.

Many circuits feature a "common point" series of buttons - that is to say, a series of buttons whereby, when pressed, two points are connected. However, more than one button shares a single, electrical connection point.

As an example, let's have another look at the musical mat from the previous three Circuit Bending Basics posts.

The above image shows the main circuit board of the musical mat. The horizontal and vertical pins are soldered to the main board, and here we can see those connections.

Upon testing all of the button pins, a pattern emerged. Let's consider the button pins as either being a letter (for the horizontal ones) or a number (for the vertical ones), as shown in the above image.

By connecting any of the pins labeled with letters (in purple) with the pins labeled with numbers (in green), a sound is triggered. Interesting, the letter determines the sound "category" whilst the number determines the actual button pressed.

Connecting A to 1 is the same as pressing the physical toy button on the musical mat for the note C. Connecting A to 2 is the same as pressing the physical toy button on the musical mat for the note D. Connecting A to 3 is the same as pressing the physical toy button on the musical mat for the note E and so on.

Let's illustrate this with a diagram:

A connection between point A and point 1 is the same as pressing the C note on the mat.
A connection between point A and point 2 is the same as pressing the D note on the mat.
A connection between point A and point 3 is the same as pressing the E note on the mat.
A connection between point A and point 4 is the same as pressing the F note on the mat.
A connection between point A and point 5 is the same as pressing the G note on the mat.
A connection between point A and point 6 is the same as pressing the A note on the mat.
A connection between point A and point 7 is the same as pressing the B note on the mat.
A connection between point A and point 8 is the same as pressing the C 8ve note on the mat.

Let's call this type of button series "common point buttons", because they all share a common point (i.e. point A). In fact, in the above example, let's call the 'A' the common point, and all of the other points individual points.

We can use this to our advantage, but to do so, we have to understand and explore the 4051 analog multiplexer.

Multiplexing with a 4051
A multiplexer has more than one input and only one output. Only one input is ever connected to the output at any one time. A selection mechanism chooses which of the inputs is connected to the output at any one time.

This is a representation of a "2 to 1" multiplexer. The multiplexer has two inputs, one output and a selection mechanism to select between the two inputs.

An example of an analog multiplexer-demultiplexer (mux demux) is the 4051 chip. The 4051 has eight inputs (or outputs), one output (or input) and a control mechanism for selecting which of the eight connections is connecting to the common output (or input).

Let's have a look at this physical and function layout of the 4051 chip:

VDD = 5V power
VSS = chip ground
VEE = analog signal ground
INH = ground for normal operation, 5V for inhibit
IN / OUT = multiple connections
OUT / IN = common connection
A, B and C = selection mechanism

The 4051 has eight channels (numbered 0 - 7) and a common connection pin.

Depending on the status of the three address pins A,B and C in combination determines which of the eight channels passes its present voltage to the common connection pin.

Logically, we can think of the eight channel pins as eight gates, of which only one can be open (ie. connected to the common pin) at any one time. The relationship between which gate is currently selected via the three address pins can be seen below in the truth table.

From the truth table, we can see that A, B and C are digital inputs. Different combinations set different multiplexer states. A is the lowest bit, B is the middle bit and C is the highest bit.

Examples of multiplexer states are as follows:

Multiplexing Common-Point Buttons Concept
With our common point buttons we have:
• Multiple buttons that share common points

With our multiplexer we have:
• A device that connects things together, that share a common point

Obviously, they complement each other!

We can summarise this complimentary relationship in the following diagram:

Basically, the 4051 "selects" which of the button pins 1 - 8 to connect with the common button pin A. A microcontroller such as the Arduino or the Teensy can control the multiplexer easily.

Connecting the Multiplexer - Example
But, how do we connect the multiplexer to the circuit?

In general, we connect the common pin of the buttons to the common output pin on the mulitplexer, and we connect each individual button pin to each input pin on the multiplexer.

For example, with the music mat:
• Connect the "A" pin to the common output pin of the 4051
• Connect the numbered pins "1", "2", etc to the input pins 1, 2 etc of the 4051.

Now, lets connect the multiplexer to the Arduino or Teensy!
• Arduino / Teensy ground goes to pins 6, 7 and 8 of the 4051 as well as ground from the toy
• Arduino / Teensy 5V goes to pin 16 of the 4051
• Digital pins 0, 1 and 2 of the Teensy or digital pins 8, 9 and 10 go to pins 11, 10 and 9 of the 4051

Example wiring for Teensy:

Example wiring for Arduino:

Actual Wiring Example:

Programming the Arduino / Teensy - Basic Example
To write a simple program for the Arduino / Teensy, we need to consider that we want to manipulate PORTB of the microcontroller. As a result, this will select the 4051. Below is some example code, which will switch between all eight buttons, changing once every 100ms.

void setup() {
DDRB = PINB | B00000111;
}

void loop() {
for(int i = 0; i < 8; i ++) {
PORTB = i;
delay(100);
}
}

Programming the Teensy - MIDI Note Pitch Example
An extended example links a MIDI note with selecting the button press. Different pitches will trigger different buttons.

int delay_time = 100;

void setup() {
usbMIDI.setHandleNoteOn(OnNoteOn);
usbMIDI.setHandleNoteOff(OnNoteOff);
DDRB = PINB & B00000111;
pinMode(ON_PIN, OUTPUT);
pinMode(11, OUTPUT);
pinMode(11, HIGH);
}

void loop() {
}

void OnNoteOn(byte channel, byte pitch, byte velocity) {
if(velocity > 0) {
PORTB = pitch % 8;
digitalWrite(11, HIGH);
}
else {
digitalWrite(11, LOW);
}
}

void OnNoteOff(byte channel, byte pitch, byte velocity) {
digitalWrite(11, LOW);
}

Demonstration Video

Conclusion
Many toys seem to use a common buttons approach to circuit structure, and this technique allows relatively easy control over the button pressing.

The code and circuit can both be refined and extended.