Course Content
IoT Engineering Course using ESP32 with 12 Real-World Projects

📘 Digital Input & Output


🎯 Lesson Objective

By the end of this lesson, students will:

  • Understand digital signals (HIGH / LOW)

  • Learn how to use pinMode()

  • Learn how to use digitalWrite()

  • Learn how to use digitalRead()

  • Control an LED

  • Read a button input

  • Build simple decision-based hardware logic


1️⃣ What is Digital Signal?

Digital signal has only two states:

  • HIGH → 1 → 3.3V

  • LOW → 0 → 0V

Unlike analog (continuous values), digital is binary.

In ESP32:

HIGH = 3.3V
LOW = 0V

Digital signals are used for:

  • LEDs

  • Relays

  • Buttons

  • PIR sensors

  • Buzzers


2️⃣ What is GPIO?

GPIO = General Purpose Input Output

These are pins on ESP32 that can:

  • Send signals (Output)

  • Receive signals (Input)

Example:

GPIO 2
GPIO 4
GPIO 5

These pins connect to real hardware.


3️⃣ Digital Output – Controlling LED


🔹 Step 1 – Circuit

  • Connect LED positive to GPIO 2 (via resistor)

  • Connect LED negative to GND


🔹 Step 2 – Code

 
int ledPin = 2;

void setup() {
pinMode(ledPin, OUTPUT);
}

void loop() {
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
}
 

🔹 Explanation

pinMode(ledPin, OUTPUT);
→ Tells ESP32 this pin will send signal.

digitalWrite(ledPin, HIGH);
→ Sends 3.3V → LED ON

digitalWrite(ledPin, LOW);
→ Sends 0V → LED OFF

This is basic digital output control.


4️⃣ What is pinMode()?

Syntax:

 
pinMode(pinNumber, mode);
 

Modes:

  • OUTPUT

  • INPUT

  • INPUT_PULLUP

Example:

 
pinMode(2, OUTPUT);
pinMode(4, INPUT);
 

Always define pin mode inside setup().


5️⃣ Digital Input – Reading a Button


🔹 Circuit

  • Connect one side of button to GPIO 4

  • Other side to GND

  • Enable internal pull-up


🔹 Code

 
int buttonPin = 4;

void setup() {
Serial.begin(115200);
pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
int buttonState = digitalRead(buttonPin);

Serial.println(buttonState);
delay(500);
}
 

🔹 Explanation

digitalRead(buttonPin);
→ Reads current state (HIGH or LOW)

With INPUT_PULLUP:

  • Button not pressed → HIGH

  • Button pressed → LOW

Why?

Because internal pull-up resistor keeps pin HIGH by default.


6️⃣ Controlling LED with Button

Now combine input and output.

 
int ledPin = 2;
int buttonPin = 4;

void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
int buttonState = digitalRead(buttonPin);

if (buttonState == LOW) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}
 

Logic:

Button pressed → LED ON
Button released → LED OFF

This is basic automation logic.


7️⃣ Understanding INPUT_PULLUP

Without pull-up resistor:

Pin floats → unstable readings.

INPUT_PULLUP:

  • Activates internal resistor

  • Keeps pin stable

This is best practice for buttons.


8️⃣ Real IoT Example – Relay Control

Relay works same as LED.

 
int relayPin = 26;

void setup() {
pinMode(relayPin, OUTPUT);
}

void loop() {
digitalWrite(relayPin, HIGH); // Turn ON AC
}
 

Relay control = Digital output control.


9️⃣ Reading Digital Sensors

Digital sensors output HIGH or LOW.

Example:

PIR sensor:

 
int pirPin = 27;

void setup() {
Serial.begin(115200);
pinMode(pirPin, INPUT);
}

void loop() {
int motion = digitalRead(pirPin);

if (motion == HIGH) {
Serial.println("Motion Detected");
}
}
 

Digital input reading builds security systems.


🔟 Common Beginner Mistakes

❌ Forgetting pinMode()
❌ Using wrong pin number
❌ No resistor with LED
❌ Confusing HIGH/LOW logic
❌ Forgetting INPUT_PULLUP

Always double-check connections.


1️⃣1️⃣ Digital I/O vs Analog I/O

Digital Analog
HIGH/LOW 0–4095
Button Gas sensor
Relay LDR
LED Potentiometer

Digital = Binary
Analog = Range values


1️⃣2️⃣ Professional Coding Style

Better structure:

 
const int ledPin = 2;
const int buttonPin = 4;

void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
controlLED();
}

void controlLED() {
if (digitalRead(buttonPin) == LOW) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}
 

This uses modular programming.


📌 Lesson Summary

In this lesson, we learned:

  • What digital signals are

  • What GPIO pins are

  • How to use pinMode()

  • How to use digitalWrite()

  • How to use digitalRead()

  • How to use INPUT_PULLUP

  • Controlling LED

  • Reading button

  • Basic automation logic

You now understand how programming controls real hardware.

Scroll to Top