Introduction
Digital Signals are the signals that exist in two states. It could be ON and OFF, HIGH and LOW, 1’s and 0’s, Positive and Negative, and so on. Digital signals are easily read by electronic devices and that is the reason why we are globally switching from analog to digital transmission.
Working Principle
A Push Button has two states ON and OFF and is used as an digital input device. The push button is the most simplest input device to understand. In this Project, the LED is going to be out digital output device. The LED will be programmed only to turn ON and OFF. When the push button is pressed the LED will turn ON and in the other case will remain OFF. This concept can be used to program any digital devices as well.
Components Required
- ESP32
- LED x 1
- Push Button x 1
- 1kΩ Resistors x 2
- Breadboard x 1
How does the Push Button work with the ESP32?
The Push button when pressed will complete the circuit and provide a HIGH signal to the Pin D13. We will then code the ESP32 to set pin D12 as HIGH.
Code – Simplified
int button_state = 0; // At first Keep button OFF, this variable is used to know the button status
const int push_button = 13; // Pin to which the Push Button sends the signal
const int led = 12; // Pin at which the LED will turn ON and OFF
void setup() {
Serial.begin(115200); //begins are serial communication between the microcontroller and the computer
pinMode(push_button, INPUT); // defines the push button as an input device
pinMode(led, OUTPUT); //defines the LED as an output device
}
void loop() {
button_state= digitalRead(push_button); //reading the state of the button
Serial.println(button_state); //printing the state of the button as 0 or 1
//check if the button state is HIGH or LOW
if (buttonState == HIGH) {
digitalWrite(led, HIGH); //Turning LED ON
} else {
digitalWrite(led, LOW); //Turning LED OFF
}
}
Conclusion
You have now learnt how to control digital devices using the ESP32. Now you are all set to make more projects wireless projects with the ESP32. To learn more you can find the next tutorial here.
One Reply on “Digital Input and Output with the ESP32”