Sub topic 1
Digital Signals: What HIGH and LOW Mean
A digital input represents information with two logic states: HIGH and LOW. A microcontroller reads the voltage on an input pin and compares it with voltage thresholds. The result is a logical value that your program can test.
On a typical 5 V Arduino board:
HIGHmeans the pin voltage is high enough to represent a logic 1.LOWmeans the pin voltage is low enough to represent a logic 0.- A voltage between the two thresholds is undefined. The result can be unstable.
The exact thresholds depend on the Arduino board and its operating voltage. Do not assume that every board uses 5 V logic. A 3.3 V board has different electrical limits.
Prerequisites
You should understand the basic structure of an Arduino sketch, including setup() and loop(). You should also know that voltage is electrical potential, current is the movement of charge, and resistance limits current. Basic breadboard wiring and using digitalWrite() to control an LED are assumed.
Logic levels are voltage ranges
A digital pin does not recognize only one exact voltage. It recognizes ranges.
For a common 5 V Arduino, a voltage near 0 V is normally read as LOW, while a voltage near 5 V is normally read as HIGH. The input circuit uses thresholds to decide which state applies. The transition area between the thresholds is not a reliable logic state.
| Pin voltage | Typical interpretation | Program value |
|---|---|---|
| Near 0 V | Logic low | `LOW` or `0` |
| Near 5 V | Logic high | `HIGH` or `1` |
| Between valid thresholds | Undefined | Unreliable |
The voltage must also stay within the pin's safe electrical range. Applying a voltage higher than the board allows can damage the microcontroller. A signal from another circuit may need a level shifter, which is a circuit that converts one voltage range to another.
Configuring and reading a digital input
Before reading a pin, configure it with pinMode(). The INPUT setting makes the pin receive a signal rather than drive one. The digitalRead() function returns the current logic state.
This example reads pin 2 and reports its state through the Serial Monitor. Connect pin 2 to GND or to 5 V while testing. Do not leave it disconnected, because an unconnected input can change state from electrical noise.
const int inputPin = 2;
void setup() {
pinMode(inputPin, INPUT);
Serial.begin(9600);
}
void loop() {
int state = digitalRead(inputPin);
if (state == HIGH) {
Serial.println("HIGH: logic 1");
} else {
Serial.println("LOW: logic 0");
}
delay(250);
}digitalRead() returns the constants HIGH or LOW. On Arduino, these constants are commonly represented by 1 and 0, but using their names makes the purpose of the code clearer.
Common mistakes
A pin configured as INPUT has very high electrical resistance. It draws very little current, which is useful for sensing signals. However, it also means that a disconnected pin has no firm voltage. The pin may appear to read random HIGH and LOW values.
Another mistake is confusing a signal's voltage with its ability to supply current. A pin can measure a voltage without being able to power a load. Use an appropriate driver circuit for motors, relays, and other high-current devices.
A button must therefore be wired so the input has a defined state when the button is not pressed. Pull-down and pull-up resistors provide that defined voltage. The internal pull-up option, INPUT_PULLUP, can provide one inside many Arduino boards. These wiring choices determine whether a press reads HIGH or LOW.
Exercise
Exercise
What will a digital input usually read when it is connected to 0 V, and why should you avoid leaving the pin disconnected?
Show answer
It will usually read LOW because 0 V is a valid low logic level. A disconnected pin is floating, so electrical noise can make it change unpredictably between HIGH and LOW.