Arduino Basics: Digital Output (LEDs, Buzzers, and Relays)
Table of Contents
Digital outputs allow your Arduino to control devices by setting pins to either HIGH (5V) or LOW (0V). These outputs are commonly used to drive LEDs, buzzers, and relays.
Controlling Digital Outputs
Use the digitalWrite()
function to set the state of an output pin:
- HIGH: Turns the device ON (sends 5V to the pin).
- LOW: Turns the device OFF (connects the pin to GND).
Example Devices
- LEDs: Light up when the pin is HIGH; always use a current-limiting resistor.
- Buzzers: Make sound when the pin is HIGH; can be passive (needs tone generation) or active (just ON/OFF).
- Relays: Switch higher-voltage circuits using a low-voltage digital signal.
Example: Blinking LED Pattern
int ledPin1 = 8;
int ledPin2 = 9;
void setup() {
pinMode(ledPin1, OUTPUT); // Set pins as output
pinMode(ledPin2, OUTPUT);
}
void loop() {
// Turn on LED 1, turn off LED 2
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, LOW);
delay(500); // Wait 500 milliseconds
// Turn off LED 1, turn on LED 2
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
delay(500);
}
Tips for Digital Output
- Always use a resistor with LEDs to prevent burning them out.
- Check the voltage and current rating for buzzers and relays to avoid damaging the Arduino.
- For multiple devices, consider using transistors or driver modules to safely control higher currents.
- Label pins clearly in your code for readability and easier debugging.
By mastering digital outputs, you can create visual indicators, sound alerts, or even control external devices like motors and home appliances through relays, making your Arduino projects interactive and dynamic.