Digital Outputs

A digital output is the most elementary feature in a microcontroller. A pin that is a digital output is set either high or low. The high value is the microcontroller's operating voltage Vcc and the low value is 0.

Basic Operation

In the basic operation of a digital output pin the output is either high or low.

Load the package.

Needs["MicrocontrollerKit`"]

Turn on the LED at pin 13.

MicrocontrollerEmbedCode[(NonlinearStateSpaceModelFull[5, \[Null], 1, {}Automatic, ]),<|"Target"->"ArduinoUno","Outputs"->13->"Digital"|>, "/dev/tty.usbmodem14101"]

Any value greater than 0 is considered as a high value.

MicrocontrollerEmbedCode[(NonlinearStateSpaceModelFull[1.5, \[Null], 1, {}Automatic, ]),<|"Target"->"ArduinoUno","Outputs"->13->"Digital"|>, "/dev/tty.usbmodem14101"]

Turn off the LED.

MicrocontrollerEmbedCode[(NonlinearStateSpaceModelFull[0, \[Null], 1, {}Automatic, ]),<|"Target"->"ArduinoUno","Outputs"->13->"Digital"|>, "/dev/tty.usbmodem14101"]

Toggle the LED on or off every second.

MicrocontrollerEmbedCode[(NonlinearStateSpaceModelFull[Which[x>0,0,True,1], x, x, \[Null], 1, {}Automatic, ]),<|"Target"->"ArduinoUno","Outputs"->13->"Digital"|>, "/dev/tty.usbmodem14101"]

The specification "pin"->"Digital" is equivalent to "pin"-><|"Type"->"Digital"|>.

Pulse Generation

In the basic operation, the output remained high or low for the entire duration of the sampling period. However, by specifying the output type to be a "Pulse", pulses of varying widths can be generated.

The complete specification is "pin"-><|"Type"->"Pulse",(Subscript[spec, 1])->(Subscript[val, 1]),…|>. The following specs can be given:

Pulse specifications.

By default (Subscript[τ, p]) is assumed to be the same as τ which is the sampling period of sys. An explicit timer can also be specified. Otherwise it will be automatically chosen.

The output signal has the following form:

If (Subscript[τ, p]) is greater than τ then it effectively is τ. If Δ is greater than (Subscript[τ, p]) then it effectively is (Subscript[τ, p]).

A pulse of width 1 second occurs once every 5 seconds.

MicrocontrollerEmbedCode[(NonlinearStateSpaceModelFull[1, \[Null], 5, {}Automatic, ]),<|"Target"->"ArduinoUno","Outputs"->13->"Pulse"|>,"/dev/tty.usbmodem14101"]

The one-second pulse occurs twice every five seconds.

MicrocontrollerEmbedCode[(NonlinearStateSpaceModelFull[1, \[Null], 5, {}Automatic, ]),<|"Target"->"ArduinoUno","Outputs"->13-><|"Type"->"Pulse",SamplingPeriod->2|>|>,"/dev/tty.usbmodem14101"]

Use the timer 2 to generate the pulses.

MicrocontrollerEmbedCode[(NonlinearStateSpaceModelFull[1, \[Null], 5, {}Automatic, ]),<|"Target"->"ArduinoUno","Outputs"->13-><|"Type"->"Pulse",SamplingPeriod->2,"Timer"->2|>|>,"/dev/tty.usbmodem14101"]

All the following cause the LED on pin 13 to toggle every second.

MicrocontrollerEmbedCode[(NonlinearStateSpaceModelFull[1, \[Null], 2, {}Automatic, ]),<|"Target"->"ArduinoUno","Outputs"->13->"Pulse"|>,"/dev/tty.usbmodem14101"]

MicrocontrollerEmbedCode[(NonlinearStateSpaceModelFull[1, \[Null], 4, {}Automatic, ]),<|"Target"->"ArduinoUno","Outputs"->13-><|"Type"->"Pulse",SamplingPeriod->2|>|>,"/dev/tty.usbmodem14101"]

MicrocontrollerEmbedCode[(NonlinearStateSpaceModelFull[Which[x>0,0,True,1], x, x, \[Null], 1, {}Automatic, ]),<|"Target"->"ArduinoUno","Outputs"->13->"Digital"|>, "/dev/tty.usbmodem14101"]