Overview
This post assumes that you are familiar with the previous Circuit Bending Basics Posts.
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!
Digital Pot and Toy Control
This article is based around the MCP4212 digital pot and the Teensy - so if you're not familiar with either, please visit the Teensy website, read up a little on Arduino coding and check out my Teensy Basics series of posts. Also, be sure to check out the MCP4212 tutorial.
This article will provide a simple framework that can be used to control the pitch / speed of a toy using MIDI pitch data. The higher the pitch, the higher the speed of the toy.
In general, the process is like this:
• Build the barebones MCP4212 and Teensy setup as shown in the tutorial
• Find the clock resistor of a toy that you wish to control
• Remove the resistor
• Connect the two resistive points on the toy's circuit to the P0A and P0W pins of the MCP4212
• Write some code to control pot 0 of the MCP4212 via MIDI
Connecting the Digital Pot to the Toy - Example
Once again, we revisit the musical mat toy from the previous Circuit Bending Basics.
The resistor in this particular toy is simple to remove. The two resistive connections on the board were soldered to two white wires, which were then connected to P0A and P0W pins of the MCP4212.
In the first image, these connections are highlighted in purple.
Writing the Code
The code takes the pitch value of a note-on MIDI message, and sends that value to the digital pot. The digital pot then sets this as a resistance.
The code uses the same SPI library as in the MCP4212 example. The code uses the same user-defined MCP4212 function as in the MCP4212. The changes in the fact that the usbMIDI functions are connected to the digital pot.
Download the code here: http://milkcrate.com.au/_other/downloads/projects/MCP4241_104EP_Example_2/MCP4241_104EP_Example_2.ino
Example Video
Friday, May 24, 2013
Circuit Bending Basics 7: MIDI-controlled Toy Pitch / Speed
Labels:
circuit bending,
circuit bending basics
How To: Interface with an MCP4241 Dual Digital Pot
Overview
The aim of this post is to present a basic practical understanding of interfacing with an MCP4241 dual digital pot. A digital pot is a digitally-controlled potentiometer, which is a very useful device. It allows a microcontroller to change a potential or a resistance using data values instead of having to turn a physical pot with a human hand. Many instances where a physical pot is used can be substituted with a digital pot.
Examples include: dimming LEDs, controlling the resistance of circuit bent toys, conditioning audio signals, setting the volume of audio signals, creating auto stereo panning devices, controlling an oscillator, creating a digital to analogue converter and so on.
The MCP4241
The MCP4241-104E is a chip with 100k digitally controlled pots that are accessed via an SPI bus. Additionally, the chip features both volatile and non-volatile memory and a number of other useful functions. The pots have 7-bits of resolution (i.e. 128 steps of resistance). It can be powered from 2.7V to 5.5V.
The MCP4241 comes from a larger family of chips, the MCP414X/416X/424X/426X family. More information can be found here.
The SPI Bus
The MCP4241 connects and communicates with a microcontroller (such as an Arduino or a Teensy) via the SPI Bus. SPI is a protocol that allows microcontrollers to interface easily with a large number of external chips and sensors.
SPI is a host / slave type bus. A single host microcontroller can interface with one or more slave chips or sensors.
SPI uses up to four pins for communication:
- CS (may also be called SS): chip select:
Used by the microcontroller to select each device. The host microcontroller has a CS output pin for every slave device that is to be used.
- SCK: serial clock:
Used by the host microcontroller to time the data that is moved to and from the slave device. There is one SCK connection that is shared amongst all SPI devices.
- SDI: slave data in:
Used by the host microcontroller to send data to a slave device. There is one SDI connection that is shared amongst all SPI devices.
- SDO: slave data out:
Used by the host microcontroller to receive data from a slave device. There is one SDO connection that is shared amongst all SPI devices.
All SPI slave devices require the CS and SCK pins. However, many device may not require both the SDI and SDO connections, as they might only either send or receive data.
With the MCP4241 chip, we want to set the resistance of a digital potentiometer using data from a host microntroller. As a result, we only need CS, SCK and SDI, because the information needs to travel only from the host to the slave device.
If you are using a Teensy, the following table shows the CS, SCK, SDI and SDO pins to use:
If you are using an Arduino, the following table shows the CS, SCK, SDI and SDO pins to use:
MCP4241 Physical Layout
The above diagram shows the layout of the 14-pin MCP4241 chip. Pin 1 is to the left of the half-circle indent. Pin 14 is to the right of the half-circle indent.
Pin 1 - CS - SPI Bus Chip Select
Pin 2 - SCK - SPI Bus Serial Clock
Pin 3 - SDI - SPI Bus Slave Digital In
Pin 4 - Vss - Connect to Ground
Pin 5 - P1B - Potentiometer Number 1, Terminal B
Pin 6 - P1W - Potentiometer Number 1, Wiper
Pin 7 - P1B - Potentiometer Number 1, Terminal A
Pin 8 - P1B - Potentiometer Number 0, Terminal A
Pin 9 - P1W - Potentiometer Number 0, Wiper
Pin 10 - P1B - Potentiometer Number 0, Terminal B
Pin 11 - WP - Write Protection - Connect to 5V for normal operation
Pin 12 - SHDN - Shutdown - Connect to 5V for normal operation
Pin 13 - SDO - SPI Bus Slave Digital Out
Pin 14 - Vdd - Connect to 5V
MCP4241 Breadboard Layout - Basic Functions - With Teensy
Set up the power for the MCP4241:
• Connect ground from the Teensy to the ground bus on the breadboard.
• Connect 5V from the Teensy to the 5V bus on the breadboard.
• Connect 5V to Vdd (pin 14) of the MCP4241.
• Connect ground to Vss (pin 4) of the MCP4241.
Initialise the additional function pins of the MCP4241:
• Connect 5V to SHDN (pin 12) of the MCP4241
• Connect 5V to WP (pin 11) of the MCP4241
Connect the SPI Bus to the MCP4241:
• Connect Teensy digital pin 0 to CS (pin 1) of the MCP4241
• Connect Teensy digital pin 1 to SCK (pin 2) of the MCP4241
• Connect Teensy digital pin 2 to SDI (pin 3) of the MCP4241
Connect to MCP4241 potentiometer 0 for testing:
• Connect P0W (pin 9 of the MCP4241 to a probe of a multimeter
• Connect P0A (pin 9 of the MCP4241) to the other probe of a multimeter
The example simply moves through all 128 possible values for both pots. Measuring the resistance between P0A and P0W should change over time as all 128 values are cycled through.
The aim of this post is to present a basic practical understanding of interfacing with an MCP4241 dual digital pot. A digital pot is a digitally-controlled potentiometer, which is a very useful device. It allows a microcontroller to change a potential or a resistance using data values instead of having to turn a physical pot with a human hand. Many instances where a physical pot is used can be substituted with a digital pot.
Examples include: dimming LEDs, controlling the resistance of circuit bent toys, conditioning audio signals, setting the volume of audio signals, creating auto stereo panning devices, controlling an oscillator, creating a digital to analogue converter and so on.
The MCP4241
The MCP4241-104E is a chip with 100k digitally controlled pots that are accessed via an SPI bus. Additionally, the chip features both volatile and non-volatile memory and a number of other useful functions. The pots have 7-bits of resolution (i.e. 128 steps of resistance). It can be powered from 2.7V to 5.5V.
The MCP4241 comes from a larger family of chips, the MCP414X/416X/424X/426X family. More information can be found here.
The SPI Bus
The MCP4241 connects and communicates with a microcontroller (such as an Arduino or a Teensy) via the SPI Bus. SPI is a protocol that allows microcontrollers to interface easily with a large number of external chips and sensors.
SPI is a host / slave type bus. A single host microcontroller can interface with one or more slave chips or sensors.
SPI uses up to four pins for communication:
- CS (may also be called SS): chip select:
Used by the microcontroller to select each device. The host microcontroller has a CS output pin for every slave device that is to be used.
- SCK: serial clock:
Used by the host microcontroller to time the data that is moved to and from the slave device. There is one SCK connection that is shared amongst all SPI devices.
- SDI: slave data in:
Used by the host microcontroller to send data to a slave device. There is one SDI connection that is shared amongst all SPI devices.
- SDO: slave data out:
Used by the host microcontroller to receive data from a slave device. There is one SDO connection that is shared amongst all SPI devices.
All SPI slave devices require the CS and SCK pins. However, many device may not require both the SDI and SDO connections, as they might only either send or receive data.
With the MCP4241 chip, we want to set the resistance of a digital potentiometer using data from a host microntroller. As a result, we only need CS, SCK and SDI, because the information needs to travel only from the host to the slave device.
If you are using a Teensy, the following table shows the CS, SCK, SDI and SDO pins to use:
If you are using an Arduino, the following table shows the CS, SCK, SDI and SDO pins to use:
MCP4241 Physical Layout
The above diagram shows the layout of the 14-pin MCP4241 chip. Pin 1 is to the left of the half-circle indent. Pin 14 is to the right of the half-circle indent.
Pin 1 - CS - SPI Bus Chip Select
Pin 2 - SCK - SPI Bus Serial Clock
Pin 3 - SDI - SPI Bus Slave Digital In
Pin 4 - Vss - Connect to Ground
Pin 5 - P1B - Potentiometer Number 1, Terminal B
Pin 6 - P1W - Potentiometer Number 1, Wiper
Pin 7 - P1B - Potentiometer Number 1, Terminal A
Pin 8 - P1B - Potentiometer Number 0, Terminal A
Pin 9 - P1W - Potentiometer Number 0, Wiper
Pin 10 - P1B - Potentiometer Number 0, Terminal B
Pin 11 - WP - Write Protection - Connect to 5V for normal operation
Pin 12 - SHDN - Shutdown - Connect to 5V for normal operation
Pin 13 - SDO - SPI Bus Slave Digital Out
Pin 14 - Vdd - Connect to 5V
MCP4241 Breadboard Layout - Basic Functions - With Teensy
Set up the power for the MCP4241:
• Connect ground from the Teensy to the ground bus on the breadboard.
• Connect 5V from the Teensy to the 5V bus on the breadboard.
• Connect 5V to Vdd (pin 14) of the MCP4241.
• Connect ground to Vss (pin 4) of the MCP4241.
Initialise the additional function pins of the MCP4241:
• Connect 5V to SHDN (pin 12) of the MCP4241
• Connect 5V to WP (pin 11) of the MCP4241
Connect the SPI Bus to the MCP4241:
• Connect Teensy digital pin 0 to CS (pin 1) of the MCP4241
• Connect Teensy digital pin 1 to SCK (pin 2) of the MCP4241
• Connect Teensy digital pin 2 to SDI (pin 3) of the MCP4241
Connect to MCP4241 potentiometer 0 for testing:
• Connect P0W (pin 9 of the MCP4241 to a probe of a multimeter
• Connect P0A (pin 9 of the MCP4241) to the other probe of a multimeter
Set up the multimeter:
• Set the multimeter to the 2000Ω range of resistance.
Programming the Digital Pot
In general, the digital pot is easy to program using the Arduino SPI library, which is included in the Arduino distribution. This library is compatible with Teensy.
The library is very easy to use, but requires a little setting up as follows:
• the CS / SS pin needs to be set up manually in the Arduino code
• the SCK, SDI and SDO do not need to be manually set up, as the library does this
A basic test program is shown below.
Note
that this includes a function called writeMCP4241. This function takes
two arguments - address and value. The address sets which pot to control
(either 0 or 1) and the value sets the value to write to the pot (0 -
127).
Download here: http://milkcrate.com.au/_other/downloads/projects/MCP4241_104EP_Example/MCP4241_104EP_Example.ino
Demonstration Video
Labels:
arduino,
digital manipulation,
teensy
Thursday, May 23, 2013
Circuit Bending Basics 6: Dual Multiplexing for Many Different Button Triggers
Overview
This post assumes that you are familiar with the previous Circuit Bending Basics Posts.
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!
Dual Multiplexing Concept
This post builds on a previous example, which explored the use of multiplexing in the context of triggering buttons of a toy.
In the previous example, a monophonic toy had eight musical buttons and many other non-musical sounds. The eight musical buttons were triggered using a multiplexer.
This post extends that basic idea by adding a second multiplexer to the mix.
Let's go back and examine the musical mat, and the way that the buttons are wired:
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:
Now, we have already triggered all of the A button group buttons using a single multiplexer in the previous example. But, if we look at the structure, there are actually a total of 32 trigger points, if all connections between numbers and letters are considered, like this:
So, the aim of this blog post is to describe and practically explore ways of combining two multiplexers to be able to recreate any one of the 32 connections shown above at will.
In fact, if there were up eight letters and eight numbers (64 connections), then this would be possible too.
Dual Multiplexing
In the previous example, we looked at the concept of having 8 inputs multiplexed to one output, like so:
Video
int ON_4066 = 6;
void setup() {
usbMIDI.setHandleNoteOn(OnNoteOn);
usbMIDI.setHandleNoteOff(OnNoteOff);
DDRB = B00011111;
pinMode(INH, OUTPUT);
pinMode(ON_4066, OUTPUT);
pinMode(11, OUTPUT);
digitalWrite(INH, HIGH);
digitalWrite(ON_4066, LOW);
digitalWrite(11, HIGH);
}
void loop() {
usbMIDI.read();
}
void OnNoteOn(byte channel, byte pitch, byte velocity) {
if(velocity > 0) {
PORTB = pitch % 32;
delay(1);
digitalWrite(INH, LOW);
digitalWrite(ON_4066, HIGH);
digitalWrite(11, HIGH);
}
else {
digitalWrite(INH, HIGH);
digitalWrite(ON_4066, LOW);
digitalWrite(11, LOW);
}
}
void OnNoteOff(byte channel, byte pitch, byte velocity) {
digitalWrite(INH, HIGH);
digitalWrite(ON_4066, LOW);
digitalWrite(11, LOW);
}
Toy Guitar Example
The same concept can be applied to other toys as well. Here is an example with a toy electric guitar:
Video
Code for Teensy
int INH = 5;
int delay_time = 100;
void setup() {
usbMIDI.setHandleNoteOn(OnNoteOn);
usbMIDI.setHandleNoteOff(OnNoteOff);
DDRB = PINB & B00001111;
pinMode(INH, OUTPUT);
pinMode(11, OUTPUT);
digitalWrite(INH, HIGH);
digitalWrite(11, HIGH);
}
void loop() {
usbMIDI.read();
}
void OnNoteOn(byte channel, byte pitch, byte velocity) {
if(velocity > 0) {
PORTB = pitch % 12;
digitalWrite(INH, LOW);
digitalWrite(11, HIGH);
}
else {
digitalWrite(INH, HIGH);
digitalWrite(11, LOW);
}
}
void OnNoteOff(byte channel, byte pitch, byte velocity) {
digitalWrite(INH, HIGH);
digitalWrite(11, LOW);
}
This post assumes that you are familiar with the previous Circuit Bending Basics Posts.
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!
Dual Multiplexing Concept
This post builds on a previous example, which explored the use of multiplexing in the context of triggering buttons of a toy.
In the previous example, a monophonic toy had eight musical buttons and many other non-musical sounds. The eight musical buttons were triggered using a multiplexer.
This post extends that basic idea by adding a second multiplexer to the mix.
Let's go back and examine the musical mat, and the way that the buttons are wired:
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.
Now, we have already triggered all of the A button group buttons using a single multiplexer in the previous example. But, if we look at the structure, there are actually a total of 32 trigger points, if all connections between numbers and letters are considered, like this:
So, the aim of this blog post is to describe and practically explore ways of combining two multiplexers to be able to recreate any one of the 32 connections shown above at will.
In fact, if there were up eight letters and eight numbers (64 connections), then this would be possible too.
Dual Multiplexing
In the previous example, we looked at the concept of having 8 inputs multiplexed to one output, like so:
The good thing about the 4051 chip is that it doesn't care what is an input and what is an output. That is to say, we can reverse this action, like so, and take one input and send it to many outputs (one output at a time, of course, depending on control mechanism):
Now, let's connect the normal multiplexer to the reverse mutliplexer,like so:
In this way, we can have up to 64 independent signals travelling through and being controlled by two 8 channel multiplexers.
Bringing it Together
If we combine the desire to emulate all of those button presses between the letter and number combination with the concept of a dual stage multiplexing setup, then we can end up with a concept like this:
Thus, any of the 32 possible sounds of the musical mat can be triggered!
Of course, in reality it looks a little messy...
Code for Teensy
int INH = 5; int ON_4066 = 6;
void setup() {
usbMIDI.setHandleNoteOn(OnNoteOn);
usbMIDI.setHandleNoteOff(OnNoteOff);
DDRB = B00011111;
pinMode(INH, OUTPUT);
pinMode(ON_4066, OUTPUT);
pinMode(11, OUTPUT);
digitalWrite(INH, HIGH);
digitalWrite(ON_4066, LOW);
digitalWrite(11, HIGH);
}
void loop() {
usbMIDI.read();
}
void OnNoteOn(byte channel, byte pitch, byte velocity) {
if(velocity > 0) {
PORTB = pitch % 32;
delay(1);
digitalWrite(INH, LOW);
digitalWrite(ON_4066, HIGH);
digitalWrite(11, HIGH);
}
else {
digitalWrite(INH, HIGH);
digitalWrite(ON_4066, LOW);
digitalWrite(11, LOW);
}
}
void OnNoteOff(byte channel, byte pitch, byte velocity) {
digitalWrite(INH, HIGH);
digitalWrite(ON_4066, LOW);
digitalWrite(11, LOW);
}
Toy Guitar Example
The same concept can be applied to other toys as well. Here is an example with a toy electric guitar:
Video
Code for Teensy
int INH = 5;
int delay_time = 100;
void setup() {
usbMIDI.setHandleNoteOn(OnNoteOn);
usbMIDI.setHandleNoteOff(OnNoteOff);
DDRB = PINB & B00001111;
pinMode(INH, OUTPUT);
pinMode(11, OUTPUT);
digitalWrite(INH, HIGH);
digitalWrite(11, HIGH);
}
void loop() {
usbMIDI.read();
}
void OnNoteOn(byte channel, byte pitch, byte velocity) {
if(velocity > 0) {
PORTB = pitch % 12;
digitalWrite(INH, LOW);
digitalWrite(11, HIGH);
}
else {
digitalWrite(INH, HIGH);
digitalWrite(11, LOW);
}
}
void OnNoteOff(byte channel, byte pitch, byte velocity) {
digitalWrite(INH, HIGH);
digitalWrite(11, LOW);
}
Labels:
circuit bending,
circuit bending basics
Circuit Bending Basics 5: On Desoldering SMD Resistors
Overview
This post assumes that you have read Circuit Bending Basics 1, 2, 3 and 4.
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!
SMD Resistors
Sometimes, the clock resistor of a toy is easy to remove.
The resistor may be a through-hole resistor like this, and can be removed manually or by de-soldering easily.
With other circuits, the clock resistor may be a tiny component - like those shown below - called "surface mount resistors". These are more difficult to remove and can prove to be a nuisance sometimes.
My method for removing these is as follows:
• The surface mount resistor has a body and two terminals.
• The aim of removing the resistor might be so that we can connect something in place of the resistor - in which case we need access to the two terminals
• Carefully heat up the area right next to and underneath the body of the SMD resistor with a soldering iron.
• You should notice that the body of the resistor becomes loose
• With a little force, flick the body with the soldering iron tip, thereby removing the body from the circuit board completely
• Add a little solder to each of the terminals that are left over
• Solder wires directly to the terminals, thereby creating easy access to the resistive points for substitution
• You can then replace the static resistor with a variable resistor (e.g. pot, digi pot, LDR, flex etc).
This post assumes that you have read Circuit Bending Basics 1, 2, 3 and 4.
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!
SMD Resistors
Sometimes, the clock resistor of a toy is easy to remove.
The resistor may be a through-hole resistor like this, and can be removed manually or by de-soldering easily.
With other circuits, the clock resistor may be a tiny component - like those shown below - called "surface mount resistors". These are more difficult to remove and can prove to be a nuisance sometimes.
My method for removing these is as follows:
• The surface mount resistor has a body and two terminals.
• The aim of removing the resistor might be so that we can connect something in place of the resistor - in which case we need access to the two terminals
• Carefully heat up the area right next to and underneath the body of the SMD resistor with a soldering iron.
• You should notice that the body of the resistor becomes loose
• With a little force, flick the body with the soldering iron tip, thereby removing the body from the circuit board completely
• Add a little solder to each of the terminals that are left over
• Solder wires directly to the terminals, thereby creating easy access to the resistive points for substitution
• You can then replace the static resistor with a variable resistor (e.g. pot, digi pot, LDR, flex etc).
Labels:
circuit bending,
circuit bending basics
Wednesday, May 22, 2013
Circuit Bending Basics 4: Multiplexing Common-Point Buttons
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() {
usbMIDI.read();
}
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.
Labels:
circuit bending,
circuit bending basics
Subscribe to:
Posts (Atom)


















































