Arduino Club

Digital Out

Digital output


A simple square-wave example

It is easy to set up a square-wave output with low and high levels of 0 V and 5 V, and low and high times that are set using the delay function. The code below generates a square wave the output high (5 V) for 1 ms and the output low (0 V) for 1 ms, so that the total period is 2 ms ( f = 500 Hz).

With an oscilloscope, it is easy to see the square wave as a function of time


An LED example

A very common use for digital outputs is to turn light-emitting diodes (LEDs) on and off. In this case, we have connected a red LED to pin 2 with 220-Ω resistor in series to limit the current. The code is exactly the same, except that the low and high times have been increased to 500 ms.


Current considerations

When external components are connected to a pin, current can flow into or out of the pin. The amount of current depends on the specific current requirements of the external parts. For instance, the LED in the previous example was drawing 15 mA or so from the digital pin whet it was "on". We should probably be aware of the current limitations of the digital pins on the Atmega328. For example, if we were to place a short circuit between a pin and ground, and then try to make the pin voltage go high, our EE-201 knowledge tells us that there should an infinite amount of current flowing from the pin, through the short circuit, and into ground. It is unlikely that the chip can supply an infinite amount of current.

First, we should understand that current can flow out of the pin (sourcing) or into the pin (sinking). Sourcing occurs when the digital output is high and there is a current path through some external components to ground. Sinking occurs when the digital output is low, and there is a current path from the positive power supply through the pin. The two situations are illustrated in the figure below, where resistors are representative external components.

Digging into the data sheet for the Atmega328, we find the following:

Operating a pin at maximum current for an "extended period time" would likely damage the output circuitry of the pin. (Internal transistors would probably burn out.) In our thought experiment of shorting a pin to ground and then trying to set the output high, the maximum current would be limited to 40 mA and the output would probably burn out in short order. (In other words, don't short the digital pins to ground or to the power supply.)

This explains the need for the limting resistor in LED example. If we connected the diode directly between the output and ground, the current would go to the max limit, the LED would shine brightly for a short time, and then go dark forever once the output burned up. We use the resistor to limit the current to a reasonable value, say half of max. When the red LED is on, that voltage across it is about 1.75 volts. A resistor that sets the current at 20 mA would be (5 V — 1.75 V)/(20 mA) = 162.5 Ω. Since we probably don't a resistor with that exact value, we can choose a standard value that is somewhat bigger – say 220 Ω – resulting in a slightly lower – and safer – current of 14.8 mA.


But the voltage isn't constant either

One of the annoying truths of working with real hardware is that "constant" voltages are never (or at least rarely) truly constant. For a high output, the voltage will droop from 5 V as more current is sourced. For low output, the voltage will float up, as more current is sinked. (EE-201 students will recognize this as an effect of there being a Thevenin equivalent resistance associated with the output circuitry.) To see the extent of the voltage variations, we can do a simple experiment of attaching various resistors to the output, measuring the actual pin voltages, and then calculating the currents. The results are shown in the table below:

source sink
R (Ω) Vo (V) io (mA) Vo (V) io (mA)
5.1 0 .004 0
4700 5.08 1.08 .029 1.08
2200 5.05 2.30 0.052 2.30
1000 4.99 4.99 0.107 5.0
560 4.89 8.7 0.189 8.77
220 4.55 20.7 0.463 21.1
150 4.38 29.2 0.653 29.5
100 3.90 39 0.953 41.1

The moral of the story is that we can never count on the "high" and "low" voltages being exact. If we need exact values, then we will need to add some circuitry that will provide them.