Lesson P13 – Digital Input and Output Programming
🎯 Learning Objectives
After completing this lesson, students will be able to:
✅ Understand Digital Input and Output
✅ Understand HIGH and LOW states
✅ Read push buttons and switches
✅ Control LEDs and relays
✅ Use digitalRead() and digitalWrite()
✅ Build interactive Arduino projects
✅ Understand pull-up and pull-down concepts
1. Introduction
Arduino interacts with the outside world using pins.
These pins can work in two modes:
Input
Arduino receives information.
Examples:
- Push Button
- Switch
- IR Sensor
- Vibration Sensor
Output
Arduino sends information.
Examples:
- LED
- Relay
- Buzzer
- Motor Driver
This process is called:
Digital Input and Output (Digital I/O)
Digital I/O is the foundation of almost every Arduino project.
2. What is Digital Signal?
A digital signal has only two states:
| State | Value |
|---|---|
| LOW | 0 |
| HIGH | 1 |
or
| State | Voltage |
|---|---|
| LOW | 0V |
| HIGH | 5V |
Arduino UNO understands only these two states for digital pins.
3. Real-Life Example
Imagine a room light switch.
Switch OFF
↓
Light OFF
Switch ON
↓
Light ON
Only two conditions exist.
This is a digital system.
4. Digital Output
When Arduino sends signals to a device, it acts as an Output.
Examples:
- LED ON/OFF
- Relay ON/OFF
- Buzzer ON/OFF
5. Configuring Output Pins
Before using an output pin:
pinMode(pin, OUTPUT);
must be used.
Example:
pinMode(13, OUTPUT);
Pin 13 is now configured as an output.
6. digitalWrite()
Used to send HIGH or LOW.
Syntax:
digitalWrite(pin, state);
Turning LED ON
digitalWrite(13, HIGH);
Pin Output:
5V
LED turns ON.
Turning LED OFF
digitalWrite(13, LOW);
Pin Output:
0V
LED turns OFF.
Complete Example
void setup()
{
pinMode(13, OUTPUT);
}
void loop()
{
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}
Result:
LED blinks continuously.
7. Digital Input
When Arduino receives information from external devices, it acts as an Input.
Examples:
- Push Button
- Toggle Switch
- IR Sensor
- Limit Switch
Configuring Input Pins
Syntax:
pinMode(pin, INPUT);
Example:
pinMode(2, INPUT);
Pin 2 becomes an input.
8. digitalRead()
Used to read digital signals.
Syntax:
digitalRead(pin);
Returns:
HIGH
or
LOW
Example
int buttonState;
buttonState = digitalRead(2);
Arduino stores button status.
9. Push Button Working Principle
Button Released:
LOW
Button Pressed:
HIGH
Arduino continuously checks the state.
Example Program
void setup()
{
pinMode(2, INPUT);
Serial.begin(9600);
}
void loop()
{
int buttonState;
buttonState = digitalRead(2);
Serial.println(buttonState);
delay(200);
}
Output:
0
1
0
1
depending on button state.
10. Controlling LED Using Push Button
This is one of the most important beginner projects.
Logic
If button pressed
↓
LED ON
Otherwise
↓
LED OFF
Program
void setup()
{
pinMode(2, INPUT);
pinMode(13, OUTPUT);
}
void loop()
{
if(digitalRead(2) == HIGH)
{
digitalWrite(13, HIGH);
}
else
{
digitalWrite(13, LOW);
}
}
11. Pull-Up and Pull-Down Resistors
A common beginner problem:
Button not pressed
↓
Random readings appear.
Why?
Because the input pin is floating.
Floating Input
A floating pin is not connected to HIGH or LOW.
Result:
Random values.
Example:
1
0
1
1
0
0
1
Solution
Use:
Pull-Up Resistor
or
Pull-Down Resistor
12. Internal Pull-Up Resistor
Arduino UNO contains built-in pull-up resistors.
Very useful.
Syntax
pinMode(2, INPUT_PULLUP);
Now external resistor is usually unnecessary.
Important Note
With INPUT_PULLUP:
| Button State | Reading |
|---|---|
| Released | HIGH |
| Pressed | LOW |
Logic becomes reversed.
Example
if(digitalRead(2)==LOW)
{
digitalWrite(13,HIGH);
}
Button press gives LOW.
13. Relay Control
Relays are common digital output devices.
Arduino sends:
HIGH
or
LOW
to control a relay.
Example
digitalWrite(8,HIGH);
Relay ON.
digitalWrite(8,LOW);
Relay OFF.
Applications
- Home Automation
- Pump Control
- Smart Appliances
14. IR Obstacle Sensor Example
IR Sensor Output:
Obstacle Present:
LOW
No Obstacle:
HIGH
(depends on sensor model)
Program Logic
if(digitalRead(2)==LOW)
{
Serial.println("Obstacle Detected");
}
15. Vibration Sensor Example
if(digitalRead(3)==HIGH)
{
Serial.println("Vibration Detected");
}
Applications:
- Security Systems
- Earthquake Monitoring
- Machine Monitoring
16. Real Project Examples
Smart Door Alarm
Input:
Button Switch
Output:
Buzzer
Automatic Water Dispenser
Input:
IR Sensor
Output:
Relay + Pump
Home Security System
Input:
Vibration Sensor
Output:
Alarm
Smart Dustbin
Input:
IR Sensor
Output:
Servo Motor
17. Common Beginner Mistakes
Mistake 1
Forgetting pinMode()
Wrong:
digitalWrite(13,HIGH);
without:
pinMode(13,OUTPUT);
Mistake 2
Using wrong pin numbers.
Mistake 3
Ignoring pull-up requirements.
Mistake 4
Confusing HIGH and LOW logic.
Especially with INPUT_PULLUP.
18. Best Practices
✅ Define pin modes in setup()
✅ Use INPUT_PULLUP when possible
✅ Label pin connections
✅ Test inputs using Serial Monitor
✅ Use comments for clarity
📊 Summary
In this lesson, we learned:
✅ Digital Signals
✅ Digital Input
✅ Digital Output
✅ pinMode()
✅ digitalRead()
✅ digitalWrite()
✅ Push Button Control
✅ Pull-Up Resistors
✅ Relay Control
Digital I/O is the most fundamental concept in Arduino programming because almost every sensor and actuator communicates through digital signals.
📖 Key Terms
Digital Input
Receiving HIGH or LOW signals.
Digital Output
Sending HIGH or LOW signals.
HIGH
Logic 1 (5V).
LOW
Logic 0 (0V).
digitalRead()
Reads digital signals.
digitalWrite()
Writes digital signals.
INPUT_PULLUP
Enables internal pull-up resistor.
Floating Input
Input pin without a defined state.
🎯 Quiz
1. Which function reads a digital pin?
A. digitalWrite()
B. digitalRead() ✅
C. analogRead()
D. pinMode()
2. What voltage represents HIGH on Arduino UNO?
A. 0V
B. 3.3V
C. 5V ✅
D. 12V
3. Which function sends a digital signal?
A. digitalWrite() ✅
B. digitalRead()
C. analogRead()
D. Serial.print()
4. What does INPUT_PULLUP do?
A. Increases voltage
B. Enables internal pull-up resistor ✅
C. Starts communication
D. Controls PWM
5. Which device is a digital input?
A. LED
B. Relay
C. Push Button ✅
D. Buzzer
🏠 Assignment
Task 1
Create a program that turns an LED ON when a push button is pressed.
Task 2
Display button status in Serial Monitor.
Task 3
Control a relay using Arduino.
Task 4
Use an IR sensor to detect an object and display a message.
Task 5
Explain the difference between INPUT, OUTPUT, and INPUT_PULLUP with examples.
➡️ Next Lesson: P14 – Analog Input and PWM Programming (Detailed Practical Lesson), which connects directly to sensors like LDR, potentiometer, MQ gas sensor, and motor speed/LED brightness control.