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:

"pin""Digital"Vcc or
"pin""Logic" or
"pin""RisingEdgeCount"number of rising edges
"pin""FallingEdgeCount"number of falling edges
"pin""ToggleCount"number of toggles
"pin""LowCount"number of lows

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 of an Arduino Uno to one side of a push button. Connect the same side thorough a pull down resistor of kΩ 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 V. Pin will read a value of V when the button is pressed, and V when it is not. We will use this value to blink the LED at pin .

11.gif

Load the package.
In[1]:=
Click for copyable input
Turn the LED on pin on and off using a button attached to pin .
In[2]:=
Click for copyable input
Out[2]=
Turn on the LED using the logic value.
In[3]:=
Click for copyable input
Out[3]=

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.

14.gif

Connect an encoder to pin 2.

15.gif

If the number of falling edges are greater than , turn on the LED at pin .
In[4]:=
Click for copyable input
Out[4]=

Additional specifications

The following additional specifications can be given for digital inputs:

"Direction"counts the pin used to determine the direction
"MaxCount"countsthe maximum number of counts
"Pullup"basic and countsenable the microcontroller's pullup resistor

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, spec1->val1,|>.

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.

18.gif

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

pinB or {pinB, "Trailing"}postive if pinB trails pinA, negative otherwise
{pinB,"Leading"}positive if pinB leads pinA, negative otherwise
{pinB,, "Pullup"True}enable the pullup resistor on pinB

Direction specifications.

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

Read the encoder's position with direction using pins and .
In[6]:=
Click for copyable input
Out[6]=

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 True, False, or the default Automatic. If the value is 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.

21.gif

Enable the internal pullup resistor on pin .
In[8]:=
Click for copyable input
Out[8]=