Analog Outputs

A microcontroller pin can only output a high Vcc value or 0. 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 3, 4, and 5 to an Arduino Uno as shown.

Load the package.

Needs["MicrocontrollerKit`"]

Turn on the LED connected to pin 3 at (3/4) of the maximum brightness:

MicrocontrollerEmbedCode[(NonlinearStateSpaceModelFull[((3 )/4)* 5, \[Null], 1, {}Automatic, ]),<|"Target"->"ArduinoUno","Outputs"->3->"Analog"|>,"/dev/cu.usbmodem14101"]

The following suboptions can be given:

Analog output suboptions.

For an output value y, the duty cycle is computed as (( y )/ymax)100, 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 3, and the LED on pin 4 is set to indicate that the output value is negative.

MicrocontrollerEmbedCode[(NonlinearStateSpaceModelFull[RandomReal[{-5,-1}], \[Null], 1, {}Automatic, ]),<|"Target"->"ArduinoUno","Outputs"->3-><|"Type"->"Analog","Negative"->4|>|>,"/dev/cu.usbmodem14101"]

A pin can also be used to indicate positive values.

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

MicrocontrollerEmbedCode[(NonlinearStateSpaceModelFull[RandomReal[{-5,-1}], \[Null], 1, {}Automatic, ]),<|"Target"->"ArduinoUno","Outputs"->3-><|"Type"->"Analog","Positive"->4|>|>,"/dev/cu.usbmodem14101"]

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

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

ℳ=MicrocontrollerEmbedCode[(NonlinearStateSpaceModelFull[RandomReal[{-5,-1}], \[Null], 1, {}Automatic, ]),<|"Target"->"ArduinoUno","Outputs"->3-><|"Type"->"Analog","Positive"->4,"Negative"->5|>|>,"/dev/cu.usbmodem14101"]

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.

ℳ["PulseWidthWaveform"]

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

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

The value 3.5` and the complementary value 1.5` will be output on pins 10 and 9:

MicrocontrollerEmbedCode[(NonlinearStateSpaceModelFull[3.5`, \[Null], 1, {}Automatic, ]),<|"Target"->"ArduinoYun","Outputs"->10-><|"Type"->"Analog","Complementary"->9|>|>,"/dev/cu.usbmodem14101"]