Digital Pins
Digital pins can be either HIGH (5V) or LOW (0V). Nothing in between.
The Three Functions
pinMode(pin, mode)
Sets a pin as INPUT or OUTPUT. Call this in setup().
pinMode(13, OUTPUT); // For LEDs, buzzers
pinMode(2, INPUT); // For buttons, switches
pinMode(2, INPUT_PULLUP); // Built-in pull-up resistor
digitalWrite(pin, value)
Sets an OUTPUT pin HIGH or LOW.
digitalWrite(13, HIGH); // 5V, LED on
digitalWrite(13, LOW); // 0V, LED off
digitalRead(pin)
Reads the state of an INPUT pin. Returns HIGH or LOW.
int state = digitalRead(2);
if (state == HIGH) {
// Button not pressed (with pull-up)
}
Pin Numbers
| HERO Board | Available Digital Pins | |------------|----------------------| | Digital I/O | 2-13 | | PWM capable (~) | 3, 5, 6, 9, 10, 11 | | LED built-in | 13 | | Avoid | 0, 1 (Serial TX/RX) |
INPUT vs INPUT_PULLUP
INPUT: Pin floats when nothing connected (reads random values)INPUT_PULLUP: Internal resistor pulls pin HIGH when nothing connected
With INPUT_PULLUP:
- Button not pressed → reads HIGH
- Button pressed (connected to GND) → reads LOW