## 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.

### Circuit Bending Basics 3: Emulating Button Presses with Arduino / Teensy

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

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. This opens the door for many possibilities, such as MIDI control, oscillator control and so on.

Finding the Button Terminals
Almost every button that you press on a toy is just physically connecting two points on a circuit together. When you let of the button, the two points disconnect.

There are several ways to find two points on a circuit that, when connected, will trigger a sound.

The first - and most obvious - is to look at the circuit where the original button is located. You will notice that two distinct paths of the circuit meet at the button. These two paths are caused to connect when the button is pressed, and are then disconnected when the two paths are disconnected.

Follow these two paths, and you might find a point in the direct path of the circuit (i.e. before hitting any other components) that can be easily soldered onto or clipped onto.

Another option is to deal with the main circuit board, and see where the buttons connect with the main chip, and go from there. The same as above applies.

Let's work with an example, using the same musical mat as in the previous two Circuit Bending Basics.

The above image shows the main circuit board of the musical mat, with the connectors (white horizontal and vertical pins) that go to the buttons.

The above image shows the main circuit board of the musical mat, but the reverse side. 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.

This basic understand of how the button presses work allows for the opportunity to trigger a button press with an Arduino or a Teensy.

The Arduino or Teensy can be used to trigger a button by connecting one of these pairs together, for example by electrically connecting points A and 1.

However, the microcontroller by itself cannot emulating the pressing of a button by itself in many cases - instead, we need to a use a simple chip to act as an intermediary. This chip is a 4066 chip.

The 4066 chip is cheap - and even locally costs only \$0.90 cents. One 4066 chip will allow you to trigger or emulate up to 4 button presses.

The chip is very simple. Let's take a look at it's functional diagram and physical layout.

The 4066 is a set of four "on / off" switches that can be controlled electrically instead of physical. There are four functional blocks of the chip. Let's deal with only one. Look at the relation between pins 1, 2 and 13. Pins 1 and 2 are like the two terminals of a button or a switch. Pin 13 is the control mechanism for these two terminals.

If 5V is connected to pin 13 (the control), then it is as if pins 1 and 2 (the terminals) are connected together. If 0V (ground) is connected to pin 13 (the control), then it is as if pins 1 and 2 (the terminals) are disconnected from each other.

Thus, we can use a signal - for example from an Arduino, a Teensy or an oscillator, to turn pin 13 on or off, thus connecting and disconnecting the link between pins 1 and 2.

Connecting the 4066 to the Toy and Arduino / Teensy
The black square represents the half circle indent on the top of the physical chip.

There are only a few connections that need to be made:
• Connect one of the points on the keyboard that form a "button press" (as outlined above) to pin 1 of the 4066 - this is one of the terminal pins of the 4066
• Connect the other point on the keyboard that forms a "button press" (as outlined above) to pin 2 of the 4066 - this is one of the terminal pins of the 4066
• Connect ground (from the negative terminal on the battery compartment of the toy) to ground on the Arduino
• Connect ground from the Arduino / Teensy to pin 7 of the 4066
• Connect 5V from the Arduino / Teensy to pin 14 of the 4066
• Connect digital pin 0 from the Arduino / Teensy to pin 13 of the 4066 - this is the control pin of the 4066

Below is a diagram of these connections:

Wiring Examples
Now, let's assume that we wanted to connect the musical mat circuit from the earlier images to the 4066. In fact, we want to be able to simulate the pressing of the 'C button' of the mat, which corresponds to the connection marked A with the connection marked 1 in the photo below.

Note that the black wire coming out of the top of the musical mat circuit board is

Here is a connection diagram for Arduino:
Here is a connection diagram for Teensy

Programming the Arduino / Teensy - Basic Example
Now that the 4066 is correctly connected between the toy and the Arduino / Teensy, we can program the Arduino / Teensy to trigger the button easily.

As discussed earlier, whenever the control pin of the 4066 is HIGH (5V), the connection between the terminal pins - and thus the button points -  is made, and a sound is triggered. Whenever the control pin of the 4066 is LOW (ground), the connection between the terminal pins - and thus the button points -  is broken, and the sound is not triggered.

The simplest example that demonstrates and tests this process is to write a loop that sets the digital pin of the microcontroller to high, wait for a moment, sets the pin to low and then waits for a moment - for example:

void setup() {
pinMode(0, OUTPUT);
}

vodid loop() {
digitalWrite(0, HIGH);
delay(500);
digitalWrite(0, LOW);
delay(500);
}

The above code should trigger the sound every second.

Programming the Teensy - MIDI Note On / Off Example
If you have connected your circuit using a Teensy, it is easy to use a USB MIDI note to trigger a sound / button press. An example sketch is provided below:

void setup() {
pinMode(0, OUTPUT);
usbMIDI.setHandleNoteOn(OnNoteOn);
usbMIDI.setHandleNoteOff(OnNoteOff);
}

void loop() {
}

void OnNoteOn(byte channel, byte pitch, byte velocity) {
if(velocity > 0) {
digitalWrite(0, HIGH);
}

else {
digitalWrite(0, LOW);
}
}

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

Demonstration Example

Conclusion
The method shown in this article - although simple - can be scaled very easily. Every 4066 chip has 4 functional blocks, and can thus trigger up to four buttons each. The Teensy alone can interface with a total of six 4066 chips, allowing for the emulation of 24 individual button presses.

MIDI can be used to control the buttons, but any Arduino program can be used to control the buttons as well.

## Monday, May 20, 2013

### Using Topwin to Convert to and from BIN and HEX Files

I have been looking for a convenient, foolproof, free and easy way to convert simple binary files to Intel hex files and back.

My current solution is to simply use an EPROM programming software package (Topwin) to import and export to and from binary and Intel hex files. It works well, but it is time and computer resource consuming (as it uses Windows XP under parallels).

## Sunday, May 19, 2013

### Useful Combo: Samsung Galaxy 4 + Focusrite Scarlett 2i2

Just a quick note: the Focusrite Scarlett 2i2 audio interface works well with the Samsung Galaxy S4 when using an OTG USB cable and the USB Audio Recorder App.