To let a LED blink with the Arduino is already a known task. To use an Optokoppler is also not that hard. What do I want to say? Both ideas to use an external device are limited to the operating voltage of the Arduino board: 5V.
The moment you want to drive a device with more that those 5V you have to think about something different. As an example lets take an electromagnetic valve which operates with 12VDC. The first solution would be a relais. Such relais come also on handy relais-boards, as the picture below shows.

A relais is basically a switch. Except that you don't switch it per hand, but through a solenoid. The advantage is that you can switch a high current with a low one.
Another solution is the transistor circuit. A Transistor works with the same idea as a relais (switch higher voltage with a lower one), but its physically structure is completely different.
The basic principle of a transistor is, that you seperate in- and output (Collektor & emitter) via potential differences. This is done by the layers of the transistor (PNP or NPN). If you bring in a voltage via the base into the transistor it's going to be conductive. You see now, that we've created a switch without any moveable part. If you want to know more about a transistor, here's a cool description.
Now the real part: The application.
The Components
The amount of used parts isn't that much:
Pcs. | Component | Model |
1 | Arduino | Uno or Nano |
1 | NPN-Transistor | TIP120 |
1 | Resistor | 1kΩ |
1 | Rectifier diode | 1N4001 |

Additionally I want to add a button. With it you can set the action to open or close the valve.
- Pushbutton
- Pull-down resistor for the button (10kΩ)
The Circuit

On the breadboard the valve is shown as a moving bolt, but the principle is the same (move something with a solenoid).

You can also see that the transistor and the controller must share the same ground line. Otherwise the circuit won't be closed. Furthermore you can see that the Arduino is driven by 12VDC form the power plug through the Vin-pin. That way you don't need a second power supply.
Be careful, now you've two different volatage levels. The higher one (12VDC) must never be combined with the lower one (5VDC).
Recitfier Diode
As already said I want to drive an electromagnetic valve. It has a solenoid which generates an electromagentic field, which moves an iron core. That way you can open/close the valve.

And because of that reason you need a rectifier diode. It is possible that the solenoid 'stores' a small amount of voltage. It could pull back when you cut off the supply voltage - this can damage the controller. With the use of a recifier diode this can be prevented - but be aware of the mounting direction, if you put it in wrong you create a short circuit!
The Code
Due to the short program I will describe it here and not on GitHub.
// Define in- & outputs
#define Transistor 2
#define Button 3
void setup()
{
pinMode(Transistor, OUTPUT);
pinMode(Button, INPUT);
// The transiostor is turned off at the beginning
digitalWrite(Transistor, LOW);
}
void loop()
{
digitalRead(Button) ? digitalWrite(Transistor, HIGH) : digitalWrite(Transistor, LOW);
/* The line above replaced the following ones
if (digitalRead(Button))
digitalWrite(Transistor, HIGH);
else
digitalWrite(TRansistor, LOW);
*/
}
The program waits until the button is presses. If that happens the transistor is going to be turned on. That's it.
Questions and comments: deloarts.wordpress.com