Project II : Digital Input and Output
Hellooo!!! 👋
Welcome to my second project of ESP32. This project is a form of implementation of the knowledge that i got from Embedded System course. In this next project, we’re going to learn to use Digital Input and Output on ESP32 using Push Button.
#STEP 1 : Required Components/Hardware
First things first we need to know what components are needed for this project. These components are just the examples I used for this project, you can use different types of components that you can find easily on the internet.
1. ESP32 Development Kit
This component can be obtained at a price of 69.000 rupiah and can be found on various online shopping platforms. This component is the main brain of this project
2. Male to Male Wire Cable
This cable is used to connect the circuit between ESP, LED Lights, and Breadboard
3. Micro USB Cable
This cable is used to connect our computer to the development board
4. Breadboard
This component is a place where we can assemble existing circuits
5. LED Lamp
In this project we will turn on this LED Light with ESP32 and using Push Button
6. Resistor 330 ohm and 10k ohm
The resistor function is to reduce the voltage that enters the led lamp, so the lamp is not easily damaged
7. Push Button
We will use this Push Button as Digital Input in this project
8. Your Computer/PC
We will use Computer/PC as the power source for the ESP32 and to generate code that will be uploaded to the development board aka the ESP32
#STEP 2: Experiment Time
In this project we will make A simple push button circuit that implements Digital Input and Output. If we push the button, the LED light will turn on. Simple right? And this is the scheme for the circuit
And this is the result when we assemble the components together on the breadboard
After we assembled all the components, we need to right the code to use this circuit and then upload it to the ESP32. Here is the code that i used
// set pin numbers
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 23; // the number of the LED pin
// variable for storing the pushbutton status
int buttonState = 0;
void setup() {
Serial.begin(115200);
// initialize the pushbutton pin as an input
pinMode(buttonPin, INPUT);
// initialize the LED pin as an output
pinMode(ledPin, OUTPUT);
}
void loop() {
// read the state of the pushbutton value
buttonState = digitalRead(buttonPin);
Serial.println(buttonState);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH
if (buttonState == HIGH) {
// turn LED on
digitalWrite(ledPin, HIGH);
} else {
// turn LED off
digitalWrite(ledPin, LOW);
}
}
Try to push the button on your breadboard and the light should be turn on like this
As a form of creation, i’ll use 3 LED lights and a quite modification on the code. I’ll make the LED turn on when the button is not pressed and when we press the button the LED will blink alternately. This is the new ciruit and the modification code that i used
// variable for storing the pushbutton status
int buttonState = 0;
const int buttonPin = 2;
const int ledPin1 = 21;
const int ledPin2 = 23;
const int ledPin3 = 22;void setup() {
Serial.begin(115200);
// initialize the pushbutton pin as an input
pinMode(buttonPin, INPUT);
// initialize the LED pin as an output
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
}void loop() {
// read the state of the pushbutton value
buttonState = digitalRead(buttonPin);
Serial.println(buttonState);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH
if (buttonState == HIGH) {
// turn LED on
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
delay(1000);
digitalWrite(ledPin1, HIGH);
delay(250);
digitalWrite(ledPin1, LOW);
delay(250);
digitalWrite(ledPin2, HIGH);
delay(250);
digitalWrite(ledPin2, LOW);
delay(250);
digitalWrite(ledPin3, HIGH);
delay(250);
digitalWrite(ledPin3, LOW);
delay(250);
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, HIGH);
delay(250);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
delay(250);
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, HIGH);
delay(250);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
} else {
// turn LED off
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, HIGH);
}
}
And this is the result, honestly i’m quite satisfied
And so that’s my experience in working on the second project. Thank you for reading this article and I hope this article can be helpful for people who want to start trying to learn ESP32.