This week we’ll talk about actual devices you’d use with digital and pulse width modulation (PWM) outputs, both techniques we introduced in the two previous columns of Off-The-Shelf Hacker. These gadgets connect to pins and generally produce some kind of physical action, like creating light or moving a mechanism.
Digital Output Devices
LEDs are the simplest devices to use with digital outputs.
Apply the right voltage to them and they turn on. Take it away and they go out. Also, the current through a typical small LED needs to be limited to a reasonable value, like around 20 mA, by a resistor. LEDs come in all different colors and are even available in two and three color combinations. I use a three-color LED in the “Ozone Tube” in my Steampunk Conference Badge.
Naturally, the three-color LED has a common and three leads for the red, green and blue diodes. There are common anode and common cathode versions of three-color LEDs, so you should be aware of that when ordering parts.
Relays, solenoids and stepper motors are the other major output device category.
The relay is a coil of wire that magnetically activates a switch. You can use small reed-relays directly with the output pin on a microcontroller because the current draw is pretty low for the relay’s coil. Larger relays may need a transistor added into the circuit, so the GPIO pin signal is strong enough to activate the relay’s coil. You’d use a relay to turn on/off a 110-volt lamp, for example.
Similarly, you might use a larger relay to cycle the power to your water heater or pool pump, if you managed those appliances with a microcontroller; perhaps in a home automation system.
Solenoids are similar to a relay in that they use a coil of wire to cause a metal plunger to move. Energize the coil and it creates a magnetic field that pulls the metal plunger into the windings. They usually produce a short linear motion and can be fairly powerful, depending on how much current they draw. You’d typically run all but the tiniest solenoids through a transistor or relay circuit, because the current carrying capacity of GPIO pins is quite small, normally in the 20 to 30 mA range.
Lastly, the stepper motor is a brushless digital device that has multiple coils for moving a metal rotor, depending on the order of coil activation. Each pulse moves the rotor one step forward or backward. Even a small stepper can draw significant current, so it’s wise to use a ULN2803 – eight-channel Darlington driver IC, an H-bridge chip like the TI SN754410 or a little packaged driver board to handle the motor’s power requirements.
Clearly, you can do a lot of cool things just by turning pins on and off.
PWM Output Devices
Pulse width modulation devices offer a bit of analog output behavior in a purely digital world.
As we discussed in last week’s article, LED brightness is controlled using PWM. Give the LED a longer “on” cycle and the thing will appear brighter.
Another common device that you might use in your projects is the servo motor. A servo takes a PWM signal from an output pin and converts it into a specific angle of rotation on its shaft. The electronics in the servo motor interpret the width of the pulse and then move the motor to the corresponding angle. There are numerous libraries used with the various microcontrollers to take all the mystery out of working with servos. Since the GPIO pin is just sending a pulse signal, you don’t usually worry too much about exceeding the pin’s current handling capacity.
Servos come in a huge variety of sizes, speeds and torque. Servos also hold their positions, assuming no change in pulse width, as long as power is applied. You can find a pretty good selection of small servos at local hobby stores, in the RC airplane section. I used a couple of very tiny servos for pan and tilt movement, in my Steampunk Eyeball project.
Last up, the motor driver. This device is a breakout board that lets you control things like DC motors using pulse width signals.
Usually, the inputs will be two inputs plus ground and +VCC. The inputs control the speed and direction of a motor.
Engineering Output Gotchas
Always be aware of the current handling capabilities of your microcontroller output pins. Typically they range from about 20 to 30 mA. By extension, you won’t want to exceed the total current capability of all the output pins added together.
So how do you light up a string of a twenty 20 mA LEDs, without burning out an output pin? Easy! Use a driver board that will handle the combined required current of the LEDs.
Also, different microcontrollers use different voltages on their output pins. Common values are 3.3 and 5.0 volts. You’ll want to take into account the output pin voltage requirements to the output device when designing your circuits. These values are contained in the device and microcontroller spec sheets.
Another area you should watch is with the inductive loads. Any time you turn on a magnetic coil, then quickly turn it off again, it will create a pulse, as the magnetic field collapses. The bad thing is that the pulse goes back into the output pin and can damage the microcontroller. You can prevent this problem by using a small signal diode across the coil terminals. Here’s a great explanation.
To me, the real magic of Off-The-Shelf Hacking is seeing things happen in the real world of physical computing. There’s a whole lot of talk about the Internet of Things (IoT) and how it will change our day-to-day lives.
The microcontroller to output device interface is a significant challenge to putting physical computing and the IoT to work. Hopefully, this little three-part series on outputs has helped.
So, enough chit-chat about the tech, let’s get out there and build physical stuff.
The New Stack is a wholly owned subsidiary of Insight Partners. TNS owner Insight Partners is an investor in the following companies: Shelf, Real, Bit.