Solenoids turn code into action.
Turns out using solenoids means using electrical parts I hadn’t used before.
The Transistor: Little power triggers big power
Transistors have a storied history, but they’ve always been a mystery to me. A little digging and I discovered that they let power flow through two of their pins — but only if a little power is applied to a third pin.
In my case, the "little power" comes from the Arduino. The power driving the solenoid itself comes from a 9-volt battery.
Two important notes on this: Even though my little solenoid can operate on just 5 volts, I learned that one shouldn’t use the 5 volts on the Arduino because a solenoid makes power spikes that are bad for the board. So my separate power supply is the 9-volt battery
Note two is that whenever you have separate power supplies on the same project, it seems important that they are always attached to the same ground (-), which I try to always note with black wires. The positive (+, red wires) power sides stay separate.
The Diode: A one-way street
Diodes are little valves that let electricity flow in only one direction, and now I get why that’s important here.
The solenoid’s piston jumps when electricity runs through the coil around it — an electromagnet moving another magnet. Ok, cool.
But magnets and coils also can generate electricity through induction. This is the principle behind dynamos and other power generators. So when the piston springs back into place, it generates some current.
The diode makes sure that generated current doesn’t go the wrong way in the circuit, disrupting the system.
Step 1: Wiring
I learned the basics for the Arduino-solenoid circuit from this instructable.
In the end, I wired it slightly differently because it’s easier for me to understand this way:
Step 2: Ardiuno coding
As in the instructable, I made a couple of modifications to the basic Arduino “Blink” sketch, and wired my solenoid to the LED pin (I didn’t even change the variable name from led). My program is here.
The “song” is stored as a series of numbers, representing the length of each note. In the main program loop, I have another loop that moves through the array, sending each note value to the
ding() function. The sequence taps out familiar tune.
- Small solenoid
- TIP120 transistor
- Rectifier diode (1N4004 works, too)
- 1K-ohm resistor (I dig this resistor pack)
- 9v battery clip
- Arduino Uno
- USB cable for programming the Arudino
- Jumper wires