Course Content
Hands-On ESP32 Robotics: Build Smart Robots Step by Step

🔌 Digital Input and Output

🎯 Lesson Objective

In this lesson, students will understand:

• What digital signals are
• The difference between digital input and digital output
• How microcontrollers read digital signals from sensors
• How microcontrollers control devices using digital signals
• Functions used for digital input and output in Arduino programming

This lesson explains how the microcontroller communicates with external devices using digital signals.


1️⃣ What is a Digital Signal?

A digital signal is a signal that can have only two possible states:

HIGH
LOW

These two states represent electrical voltage levels.

In most microcontroller systems:

HIGH usually means the pin receives voltage.

LOW usually means the pin receives little or no voltage.

For example:

HIGH → Logic value 1
LOW → Logic value 0

Digital signals are commonly used to control simple electronic components.


2️⃣ What is Digital Input?

A digital input is when the microcontroller receives a signal from an external device.

In this case, the microcontroller reads the signal and processes it.

Examples of digital input include:

🔘 Reading a push button
🔍 Detecting an object with an IR sensor
🛑 Detecting contact using a limit switch

The sensor sends either a HIGH or LOW signal to the microcontroller.

The program then checks this signal and decides what action to perform.


3️⃣ What is Digital Output?

A digital output is when the microcontroller sends a signal to control another device.

The microcontroller can set a pin to HIGH or LOW to activate or deactivate a component.

Examples of digital output include:

💡 Turning an LED on or off
⚙️ Controlling a motor driver input
🔔 Activating a buzzer
📡 Sending signals to modules

By controlling output pins, the microcontroller can operate different hardware components.


4️⃣ Setting Pin Mode

Before using a pin as input or output, the microcontroller must be told how the pin will be used.

This is done using the pinMode() function.

Example:

 
pinMode(5, OUTPUT);
 

This command sets pin 5 as an output pin.

Example:

pinMode(4, INPUT);
 

This command sets pin 4 as an input pin.

Setting the correct pin mode is important for proper circuit operation.


5️⃣ Writing Digital Output

To send a digital signal from the microcontroller, we use the digitalWrite() function.

Example:

 
digitalWrite(5, HIGH);
 

This command sets pin 5 to HIGH and turns the connected device on.

Example:

 
digitalWrite(5, LOW);
 

This command sets pin 5 to LOW and turns the device off.

This function is commonly used to control LEDs, motors, and other components.


6️⃣ Reading Digital Input

To read a digital signal from a sensor or switch, we use the digitalRead() function.

Example:

 
int sensorValue = digitalRead(4);
 

This command reads the signal from pin 4 and stores it in a variable.

The value will be either:

HIGH → Signal detected
LOW → No signal detected

The program can then use this value to make decisions.


7️⃣ Example of Digital Input and Output

Let us consider a simple example.

A push button is connected to the microcontroller.

When the button is pressed, an LED should turn on.

The program works as follows:

1️⃣ The microcontroller reads the button state.
2️⃣ If the button is pressed, the signal becomes HIGH.
3️⃣ The program detects the HIGH signal.
4️⃣ The program turns on the LED.

This demonstrates how input signals can control output devices.


8️⃣ Digital Signals in Robotics

Digital input and output are used in many robotics applications.

Examples include:

🤖 Line follower robots reading IR sensors
🚧 Bumper robots detecting limit switches
💡 LEDs indicating robot status
⚙️ Motor drivers receiving control signals

Digital signals allow the microcontroller to communicate with many components in a robotics system.


🚀 What Happens Next

Now that you understand how digital signals allow the microcontroller to communicate with sensors and devices, the next step is to learn about analog input and output, which allow the microcontroller to handle varying signals.

Scroll to Top