Wednesday, May 22, 2013

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.



About the 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() {
  usbMIDI.read();
}

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.

0 comments: