Digital Inputs

The signal at a microcontroller's input pin can take a high or low value. The input bit in the microcontroller for that pin will be set or cleared based on the pin's value. These values or the changes in these values can be used as measures of the digital input.

The various types of digital inputs:

Digital input types.

Basic Operation

The specification "Digital" and "Logic" returns the value of the pin. "Digital" returns the value of the voltage at the pin while "Logic" returns the value of the input bit.

Let us use a microcontroller to read a digital signal. Connect pin 2 of an Arduino Uno to one side of a push button. Connect the same side thorough a pull down resistor of 10kΩ to the ground. The pull down resistor prevents the pin from reading spurious values when the button is not pressed. Connect the other side of the push button to 5V. Pin 2 will read a value of 5V when the button is pressed, and 0V when it is not. We will use this value to blink the LED at pin 13.

Load the package.

Needs["MicrocontrollerKit`"]

Turn the LED on pin 13 on and off using a button attached to pin 2.

MicrocontrollerEmbedCode[(StateSpaceModelFull[1, 1, , ]),<|"Target"->"ArduinoUno","Inputs"->"2"->"Digital","Outputs"->"13"->"Digital"|>,"/dev/tty.usbmodem14101"]

Turn on the LED using the logic value.

MicrocontrollerEmbedCode[(StateSpaceModelFull[1, 1, , ]),<|"Target"->"ArduinoUno","Inputs"->"2"->"Logic","Outputs"->"13"->"Digital"|>,"/dev/tty.usbmodem14101"]

Counts

Counting the transitions also gives information about the signal. For example, the transitions in the encoder signal that occur in a fixed time interval can be used to determine the speed of a rotating shaft. The transitions can be quantified by counting the number of rising edges, falling edges, toggles, or lows.

Connect an encoder to pin 2.

If the number of falling edges are greater than 5, turn on the LED at pin 13.

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

Additional specifications

The following additional specifications can be given for digital inputs:

Additional digital input specification.

The basic specification pin->type is equivalent to pin-><|"Type"->type|>. With additional specifications, the channel must be given as pin-><|"Type"->type, (Subscript[spec, 1])->(Subscript[val, 1]),…|>.

The specification "Pullup" is applicable to all digital inputs, while "Direction" and "MaxCount" is only applicable to the various counts.

With two digital inputs that are out of phase it is possible to assign a sign to the counts. For example, in an encoder with two outputs, if the signal on pinB trails that on pinA that can be considered as clockwise rotation, and if the one on pinB leads that of pinA that can be considered as counterclockwise rotation.

The specification to obtain the direction is pinA-><|"Type"->type, "Direction"->dirSpecs|>. The dirSpecs can have the following values:

Direction specifications.

The specification pinA-><|…,"Direction"->{pinB, "Trailing"},…|> is equivalent to pinB-><|…,"Direction"->{pinA, "Leading"},…|>.

Read the encoder's position with direction using pins 2 and 3.

MicrocontrollerEmbedCode[(StateSpaceModelFull[1, 0.1`, , ]),<|"Target"->"ArduinoUno","Inputs"->"2"-><|"Type"-> "RisingEdgeCount","Direction"->"3"|>, "Outputs"->"Serial"|>,"/dev/tty.usbmodem14101"]

The specification "MaxCount" specifies the maximum number of counts that are possible during one sampling period. It is used to allocate the memory to store the counts. The default value is 256.

The specification "Pullup" specifies if the internal pullup resistor of the microcontroller should be enabled.  It can take values (RefLink[True, paclet:ref/True]), (RefLink[False, paclet:ref/False]), or the default (RefLink[Automatic, paclet:ref/Automatic]). If the value is (RefLink[True, paclet:ref/True]), the pin will read Vcc by default.

In the setup below, the LED at pin 13 will turn off whenever the button is pressed.

Enable the internal pullup resistor on pin 2.

MicrocontrollerEmbedCode[(StateSpaceModelFull[1, 1, , ]),<|"Target"->"ArduinoUno","Inputs"->"2"-><|"Type"->"Digital","Pullup"->True|>,"Outputs"->"13"->"Digital"|>,"/dev/tty.usbmodem14101"]