This article assumes that you have read Teensy Basics 1 and Teensy Basics 2.
Buttons and toggles are great ways of setting states so that different things can happen in your program. This post will cover the physical connections of various types of switches and how to read these switches with the Teensy.
Switch and Button Types
The majority of switches and buttons can be categorised into at least four different, common categories:
• SPST
• SPDT
• DPST
• DPDT
Let's take a look at these four different types. Even though there are different variations on each, the basic concept is described below.
SPST is short for "Single Pole Single Throw". This indicates a very simple button or switch, whereby either a connection is made or is broken, depending on the physical state of the switch. We can represent the SPST switch with the following diagram.
The SPST switch itself has two terminals. In an off state, these terminals are disconnected. In an on state, these terminals are connected.
In an audio context, we can conceptually think of the SPST switch or button as muting or unmuting a mono signal, like so.
SPDT is short for "Single Pole Double Throw". This indicates a switch whereby either a connection is made between points A and B, or a connection is made between points A and C. We can represent the SPDT switch with the following diagram.
The SPDT switch itself has three terminals. In an off state, two terminals are connected (one of them the common) and the remaining terminal is disconnected. In an on state, the previously disconnected terminal is connected to the common, leaving the previously connected terminal disconnected.
In an audio context, we can conceptually think of the SPDT switch or button as a mono signal selection switch, like so. Normally, signal A goes through to the "common" terminal. However if the switch is activated, signal B can now go through to the common terminal instead of signal A.
DPST is short for "Double Pole Single Throw". This indicates a switch whereby either a connection is made or broken between two pairs of terminals.
The DPST switch itself has four terminals. We can think of the DPST switch as simply being two SPST switches that are switched using the same physical mechanism. We can have two signals that go through two pairs of terminals, and either the signal goes through or it doesn't, depending on the physical state of the switching mechanism (which connects or disconnects both pairs of terminals simultaneously).
In an audio context, we can conceptually think of the SPST switch or button as muting or unmuting a stereo signal, like so.
DPST is short for "Double Pole Double Throw". This
indicates a switch whereby either a connection is made between points A
and B AS WELL AS D and E, or a connection is made between points A and C AS WELL AS points D and E. We can represent
the DPDT
switch with the following diagram.
The DPDT switch itself has six terminals - two lots of three. We can think of the DPDT switch as simply being two SPDT switches that are switched using the same physical mechanism. In an off state, two
terminals in each lot of three are connected (one of them the common) and the remaining
terminal is disconnected. In an on state, the previously disconnected
terminal is connected to the common in each lot of three, leaving the previously connected
terminal disconnected.
In an audio context, we can conceptually think of the DPDT switch or button as a stereo signal selection switch, like so. Normally, signals A.L and A.R go through to the "common" terminals. However if the switch is activated, signals B.L and B.R can now go through to the common terminals instead of signal A.L and A.R.
Connecting Switches or Buttons to Teensy
The most common switches and button types that you will connect to a Teensy are SPST and SPDT. The connections for both are as follows.
An SPST switch connection is summarised with this diagram.
• Connect 5V to one terminal of the switch
• Connect the other terminal of the switch to a Teensy digital pin
• Also connect the other connection of switch to one leg of 10k – 100k resistor
• Connect the other leg of resistor to ground
When button is not pressed, the Teensy input pin is connected to ground via the 10K resistor
When button is pressed, the Teensy input pin is connected to 5V, as this provides the path of least resistance.
A resistor that is used in this way is called a “pull down resistor”, because it pulls the Teensy digital signal down to ground if the button is not pressed. If this resistor was not in the circuit, the Teensy digital pin would not be connected to anything whenever the button is not pressed, leaving the pin 'floating' – this would result in noise, as the pin randomly oscillates between LOW and HIGH.
A SPDT switch connection is summarised with this diagram.
• Connect 5V to an outside terminal of the switch
• Connect ground to the other outside terminal of the switch
• Connect a Teensy digital pin to the common terminal of the switch (most often this is physically the middle terminal)
When the switch is “off”, the common terminal (and thus the Teensy pin) is connected to ground. When the switch is “on”, the common terminal (and thus the Teensy pin) is connected to 5V.
• Connect one or more switches to Teensy following the above instructions
Voltage Thresholds
The digital pins of the Teensy can act as inputs or outputs. When acting as inputs, the pins are either HIGH or LOW (1 or 0), depending on the voltage applied to them. The Teensy has a voltage threshold to trigger the transition from 0 to 1 and 1 to 0.
This threshold voltage is not 2.5V (i.e. half of 5V) as might be expected. Instead, this threshold voltage is between 1.1V and 1.7V depending on the power supply voltage of the Teensy, which is normally from around 3V to a maximum of 5.5V.
Of interest might be these "voltage thresholds" that trigger a LOW to HIGH transition or a HIGH to LOW transition.
The following graph shows the voltage thresholds for a LOW to HIGH transition:
The following graph shows the voltage thresholds for a HIGH to LOW transition:
• Though this section may seem irrelevant, there may be more complex digital circuits where this information will come in handy!
Reading A Switch in Teensy
To read a switch or button, we need to use set the relevant digital pin of the Teensy to be an input, like so:
The function digitalRead( ) with an argument of the pin number to read will return the value (0 or 1 for LOW or HIGH - or ground or 5V) of that particular digital pin. This returned value can be stored in a variable or used in another function.
We can use this function to read the value of pin 0 into a variable, like so:
And then, we can use that variable to actually do something with the state of our digital pin, whose state is set by the physical button or switch. The code below turns on the onboard Teensy LED (which is found on digital pin 11) whenever the button is pressed or the switch is turned on.
No comments:
Post a Comment