Working with Positive and Negative Logic

One of the challenges of electronics interfacing is dealing with the wide variety of electronics hardware that may be connected!

Examples of positive and negative logic input and output circuits.
Figure 1. An ‘active’ or ‘enabled’ digital input or output may be associated with either a high or low voltage level, depending on the associated electronic circuitry.

Internally, a microcontroller deals with digital values that are either true or false, on or off, or 1 or 0. However, there is no single definitive mapping of internal logic values and external voltage levels. Instead there are two – and these are called positive logic and negative logic.

The C++ version of the plcLib library supports inputs and outputs which use either positive logic or negative logic, and each type may be separately configured. In positive logic, an active input or output will correspond to a high voltage level. Conversely, in negative logic, a low voltage level is associated with an active input or output. Which setting is correct will depend on the connected hardware, as shown in the examples below.

Enabled inputs may be either active-high or active-low.
Figure 2. Enabled inputs may be either active-high or active-low, shown here linked via a push to make switch. The closed switch contacts overrides the resistor, which is connected to the opposite polarity.

The example at the left is typical of positive logic, with the input being pulled-up to the positive supply when the switch is pressed (i.e. active), or weakly pulled to a low logic level by the pull-down resistor. Conversely, the negative logic circuit at the right provides a strong low voltage level when the switch is pressed and a weak high logic level when the switch is inactive. (In practice, an additional resistor may be connected in series with the input, to eliminate the danger of a short circuit, should the digital pin be configured as an output, and the switch held in a closed position.)

Similarly, the enabled or disabled status of a digital output may be determined by the active or inactive state of a connected peripheral. For example an LED may turn on or off, or a motor may run or stop. Once again, digital output devices may be connected with either electrical polarity, as shown in the examples below.

Examples of positive and negative logic output circuits.
Figure 3. A connected digital output device, such as an LED, may be enabled or disabled by outputting either a high or low logic voltage level, depending on the associated circuitry.

In the the negative logic output circuit at the left, the LED is illuminated by pulling the output to a low voltage level. Conversely a high voltage level is required to illuminate the LED in the positive logic circuit at the right.

Positive or negative logic inputs and/or outputs may be separately configured to suit the connected hardware, by editing the relevant section of the plcLib.cpp file, as shown in the code extract below.

Remove the comments from one or both lines above to enable negative logic signal polarities, or restore them to revert back to positive logic, which is the default.

Recommended positive/negative logic configuration settings are given in Table 32 of the Reference Manual, for supported hardware. As a general rule, Grove digital I/O modules tend to use positive logic. However it should be noted that the Multifunction Shield (MFS) uses negative logic for both its push button inputs and LED outputs. Other boards may vary in their operation, depending on the type of external devices connected. For example, the Grove Shield FeatherWing board has limited connector availability, so dual port I/O modules were used during testing. It was found that the M5Stack Mini Dual Button Units connected to the inputs were active low, while M5Stack 2-Channel SPST Relay Units connected to outputs were active high!

It is also possible to change the polarity of individual inputs or outputs, simply by replacing positive logic commands with their negative logic equivalents. For example, replace: –

  • din with dinNot
  • dout with doutNot

(or vice versa).

Please see the IO > DigitalInputOutput example in the ‘live’ system for more details.


Posted

in

,

by