π 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.