Previously, I've written about different way in which a Teensy or other Arduino-based microcontroller can be used to trigger button presses on a toy.
A relay can be used to the same effect, and may be successful in situations where other methods fail.
A relay is an electromagnetic coil. When a current is passed through the coil of sufficient voltage, the magnetic field physically moves a switch, thus making a connection that was previously unconnected.
A relay will have at least four pins, in two pairs. The first pair is the coil, and this is what is used to open or close the connection. The second pair is the actual connecting contact points that are either open or closed, depending on whether or not the coil is charged. A digital pin on the Teensy is used to control the coil, thus making or breaking a circuit.
A Darlington transistor array is used between the Teensy and the relay for three reasons. The digital pin on the Teensy cannot provide enough voltage for the relay to trigger. The digital pin on the Teensy cannot provide enough current for the relay to trigger. The relay may damage the Teensy if connected directly.
For these reasons, the ULN2003 Darlington transistor array chip is used as a switching source, with the Teensy pin as an input and the relay as the output. The connections are quite straightforward, as shown below:
• Teensy ground is connected to pin 8 of the ULN2003 (blue jumper wire)
• Teensy 5V is connected to pin 9 of the ULN2003 (blue jumper wire)
• ULN2003 pin 1 is connected to one side of the relay coil (white jumper wire)
• Teensy 5V is connected to the other side of the relay coil (green jumper wire)
• One side of the relay contact is going to one contact point of a button press on a toy (orange jumper wire)
• The other side of the relay contact is going to the other contact point of a button press on a toy (orange jumper wire)
Once this has been set up, a HIGH state on Teensy digital pin 0 will energise the relay, and the two orange wires will be electrically connected. A LOW state on Teensy digital pin 0 will stop the relay, and the two orange wires will be electrically disconnected.
Example code is shown below to turn the relay on or off depending on incoming note on / note off events.