Analog Outputs

A microcontroller pin can only output a high Vcc value or . To get values in between the microcontroller uses pulse width modulation (PWM).

PWM works by setting the output high for only a percentage of time during each sampling period. The percentage is called the duty cycle of the PWM signal. If the sampling period is constant and fast enough the end result is a signal whose value is the same percentage of Vcc. Thus to achieve PWM the microcontroller outputs a series of on-off pulses.


To follow along, connect LED-resistor pairs to pins , , and to an Arduino Uno as shown.


Load the package.
Turn on the LED connected to pin at of the maximum brightness:

The following suboptions can be given:

"MaxValue"the maximum absolute value ymax of the output
"Negative"the pin to indicate a negative output value
"Positive"the pin to indicate a positive output value
"Timer"the timer to generate the output pulses
"TimerMode"the mode to operate the timer
"TimerTop"the TOP value of the timer
"SamplingPeriod"the sampling period of the output's timer
"Complementary"the complementary pins

Analog output suboptions.

For an output value y, the duty cycle is computed as , where ymax is the maximum of the magnitude of possible output values. The duty cycle must be nonnegative and any value greater than 100 is clipped. The maximum absolute value can be explicitly specified as {"pin", "Analog","MaxValue"->ymax}. By default, ymax is the operating voltage Vcc.

The analog pins can only be output positive values. If the output is negative, the absolute value is output. A second digital pin can be used to indicate when a negative value is output on the analog pin.

The absolute value is output on pin , and the LED on pin is set to indicate that the output value is negative.

A pin can also be used to indicate positive values.

The LED on pin is set if the output value is positive.

Explicitly indicate both positive and negative values using two digital pins.

Pin 4 is set for positive values and pin 5 for negative values.

The suboptions "Timer", "TimerMode", and "TimerTop" specifies the timer to use in generating the waveform and various other parameters of the timer. More information on the timer and the waveforms can be found in the microcontroller's datasheet. The property "PulseWidthWaveform" gives information about the parameters that were used to generate the waveform.

Some microcontrollers can output complementary output values. The "Complementary" suboption specifies the pin to output the complementary value.

Connect LED-resistor pairs to pins and on an Arduino Yun.


The value and the complementary value will be output on pins and :