AC Power

The tiny computer can output 40 milliamperes on a pin. That is enough to light an LED, make sounds on a speaker, and even run some tiny motors. We saw earlier how we can use a transistor to control larger currents, up to a few amperes, as long as the voltage is kept under about 40 volts.

Often, however, we need more power. We want to control appliances, lamps, and other things that normally get their power from a wall socket.

Instead of a transistor, we use a device called a triac when we want to switch alternating current. The tiny computer could just connect a pin to the gate terminal of the triac, just like we did with the transistor. But that would mean there is a direct connection between the tiny computer and the power coming from the wall, and that can be dangerous. So instead, we use another device, called an optoisolator, to trigger the triac and turn on the lamp. An optoisolator is a transistor or triac that is triggered by light. The source of the light is an LED that is packaged with the triac in a little six pin chip. In this way, the tiny computer is only connected to an LED, and things are safe. The LED shines on a little triac (which isn't big enough to handle the full lamp current by itself), and that little triac triggers the big one.

Since we are dealing with 120 or 240 volt AC wall current, we won't be constructing this project on a solderless breadboard. We will use a general purpose printed circuit board, and solder the parts onto it.

The first part is the optoisolator. The part number is a MOC3012. We will place it on the right side of the board, like this:

The next part is the triac, the BT136. It looks a lot like our power transistor from an earlier project. It has three legs: a gate and two other legs that will carry the power. We put the gate leg in the hole right above the optoisolator, so the copper conductor on the other side of the board connects the gate of the big triac to one of the output legs of the little triac in the optoisolator, like this:

Next we connect a 100 ohm resistor (marked brown black brown gold) between the other leg of the optoisolator and the middle leg of the triac:

At this point we could just connect two wires to the first two pins of the optoisolator and we'd be done. Those two wires would go to pin 9 and ground on the tiny computer, and serve to turn on the little LED inside the optoisolator. But I like to be able to see when the optoisolator is triggered, so we will add a visible LED first:

Since there are two LEDs in series (the visible one and the one hidden in the optoisolator), we need to make sure they are both oriented properly, or neither one will light up. The positive lead of the LED is the long leg. It is called the anode. The shorter leg of the LED connects to the negative side of the power supply, and is called the cathode. Inside the plastic, the cathode lead is the larger part that is shaped as a dish to act as a reflector. The plastic on the cathode side is flat.

The LED in the optoisolator has the anode (positive) terminal connected to pin 1. The cathode (negative) is pin 2. So we need to connect the visible LED's anode (long leg) to a hole beneath the optoisolator's cathode (pin 2). The visible LED's cathode (short leg) we put in the hole to the left of the optoisolator.

Next we solder the wires that will lead to the tiny computer. We got ours from a length of CAT5 network cable, so they came already twisted together.. The blue wire will be connected to pin 9, and the white and blue wire will be connected to the tiny computer's ground pin. In the photo above, the blue wire connects to pin 1 of the optoisolator. The blue and white wire connects to the cathode of the visible LED.

Now we turn the board over. The neutral wire of the power cord is the one connected to the large pin in the plug. We solder the neutral wire to the pad on the right. The other, smaller pin on the plug is called the "hot" wire. We solder that to the middle pin of the big triac. The wires that go to the lamp are soldered to the third pin of the triac, and to the pad that we soldered the neutral wire to.

The last step is to connect the blue wire to pin 9 of the tiny computer, and the white and blue wire to ground.

At this point, before plugging the cord into the wall, we generally want to insulate the board so no one can accidentally touch the parts that are connected to 120 volts of alternating current. We do this by wrapping vinyl electrical tape around the board, or putting the board into an insulated box, or both. But in this case, I skip that step (not recommended) because in our next project we will be adding more parts to the board.

If the board is not insulated, do not plug it in.


A program to turn the lamp on for two seconds every ten seconds looks like this:

enum { AC_CONTROL_PIN = 9 };


  digitalWrite( AC_CONTROL_PIN, HIGH );
  delay( 2000 );
  digitalWrite( AC_CONTROL_PIN, LOW );
  delay( 10000 );