Digital Input / Outputs
If electronic boards couldn't interact with the outside world, they wouldn't be of much use. All these boards therefore offer the ability to measure and generate electrical signals.
This interaction is achieved through voltage variations. Before looking at how to use these functions with Node Blue, here are some useful concepts. There are two types of signals depending on how they are handled by microcontrollers: Digital signals and Analog signals. Let's start with digital signals.
Digital Inputs
Digital inputs can be used to sense the state of a button, a switch or a sensor, to receive a signal from another board, and many other things.
They are represented internally by only two states: 0 or 1, which correspond to voltage ranges that depend on the technology.
For example, TTL signals consider a voltage between 0 and 0.8V as 0 and a voltage between 2.4V and Vcc as 1 (Vcc can be 3.3V or 5V, depending on the variant). These values come from the technology used in input electronics. You'll find a very good Sparkfun article here that goes into more detail about this.
Be careful, most modern MCUs operate at 3.3V and will be destroyed if you apply 5V to an input (for example, the SamD21). Some other MCUs are "5V-tolerant" and will be able to accept 5V despite operating at 3.3V. MCUs that operate at 5V will have no problem if you apply 3.3V to an input. There are level converters available to convert from one voltage to another, but they should be avoided if possible, because they sometimes don't behave correctly at high frequency and will not be suited for some usage (SPI or I2C protocols for example).
In some cases, an input can end up "hanging," meaning it's not connected to anything (for example, with a button not pressed), and in this case, the voltage can reach any value, which is undesirable. A resistor can be added to force the input to:
- Gnd (0V), in this case the term "Pull down" resistor is used.
- Vcc (usually 3.3V or 5V), in this case the term "Pull up" resistor is used.
All MCUs propose at least a "Pull up" mode with a resistor embedded in the microcontroller (so there's no need to add this resistor externally), but only some offer a "Pull down" mode. On some MCUs, you'll find some other type of pull modes, for example on nRF52 series, you'll have a "Pull down sense" and "Pull up sense", which are able to wake up a sleeping MCU (when it's in deep sleep mode to reduce power consumption).
To put this into practice, connect a 2-pin push button between pin D0 of a board and a ground (Gnd) pin, and test the following design:
Open Examples/02-Digital/DigitalIn in Node Blue.
If you don't have any button, you can alternatively connect directly the D0 pin to Gnd with a wire, however this is not recommended because if you accidentally connect the pin to 5V, you may damage the input or the entire MCU if it's not 5V-tolerant.
We'll see later how the FormatText and UsbSerial modules work, but for now, just notice that they will be used to display the value of the input to which the button is connected. Open a serial monitor (# icon in the top of the window) and observe what happens when you press and release the button.
We can see that when the button is released, the value is 1 (thanks to the "Pull Up" resistor), and when it is pressed, the value changes to 0. If you want the opposite (the most common case), you must select Yes for the Invert option of the digital input module.
You may also notice that the value changes several times, even if you pressed the button only once. This is due to the mechanical bounce of the contact inside the button. Most mechanical buttons have this "defect", even the best quality ones. The bounces duration will be more or less high (this is where quality can make a difference), from 2 ms to more than 10 ms (beyond that, it becomes worrisome).
There are several methods to correct this problem: some involve adding external components to filter the signal (capacitors), while others operate solely through software.
In Node Blue, for "Digital In" modules, no debounce method is used. This module is a raw hardware pin reader and is for stable digital signals (like coming from sensors), so if you want to use mechanical buttons which may produce bounces, you should use a Button module instead.
Buttons
A Button module is a DigitalIn module combined with a software Debouncer.
Debouncing consists of eliminating parasitic pulses coming from commutation bounces, with software methods. To do this, Node Blue proposes two approaches:
-
Fast: As soon as the signal changes, this change is taken into account and any modifications are prohibited for a certain period of time. This is the most reactive method.
-
Safe: We wait until the signal remains unchanged for a certain period of time before validating it. This method is less reactive than the Fast method since it will systematically add a delay equal to the test duration, but the advantage is that this method also works to eliminate noise other than bounces.
Now open the button example project:
Open Examples/02-Digital/Button in Node Blue.
To see this algorithm in action, open the properties of the Button module and set the Debounce parameter to 10 ms. Upload the code and observe what happens: there is only one state change when the button is pressed or released.
Digital Outputs
Digital outputs are used to turn an LED on or off, send a signal to another board, and many other things. Be careful, the current output capacity is generally very low (40 mA, for example, for the ATMega32U4 of the Arduino Leonardo), sufficient to control a small LED, but too low to drive a relay or a motor, for which a power control circuit will be required.
For a value of "0," the output voltage will be close to 0V; for a value of "1," it will be 5V or 3.3V, depending on the board's operating voltage.
Keep your button connected and test this example, which will light up the board's LED when you press the button:
Open Examples/02-Digital/DigitalInOut in Node Blue.
Now let's move on to analog inputs.