Project II : Digital Input and Output

Rayhan Naufal Luthfi
5 min readFeb 13, 2022

--

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

Source : https://sea.banggood.com/ESP32-Development-Board-WiFi+bluetooth-Ultra-Low-Power-Consumption-Dual-Cores-ESP-32-ESP-32S-Board-Geekcreit-for-Arduino-products-that-work-with-official-Arduino-boards-p-1109512.html?imageAb=1&akmClientCountry=ID&cur_warehouse=CN

2. Male to Male Wire Cable
This cable is used to connect the circuit between ESP, LED Lights, and Breadboard

Source : https://images.tokopedia.net/img/cache/500-square/product-1/2019/1/14/1851958/1851958_52511d23-723c-4321-9af8-2bbbc744877c_700_700.jpg

3. Micro USB Cable
This cable is used to connect our computer to the development board

Source : https://d2xjmi1k71iy2m.cloudfront.net/dairyfarm/id/images/239/0823954_PE775936_S5.jpg

4. Breadboard
This component is a place where we can assemble existing circuits

Source : https://images.tokopedia.net/img/cache/500-square/product-1/2015/4/11/7161339/7161339_891cb22c-e00a-11e4-b29b-c5fe86772fba.jpg

5. LED Lamp
In this project we will turn on this LED Light with ESP32 and using Push Button

Source : https://m.media-amazon.com/images/I/61nWSDAUFiL._SL1024_.jpg

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

Source : https://www.twinschip.com/image/cache/catalog/Products%20Twins%20Chip%20Store%202020/330%20Ohm%20Resistor%20X%205%20Pieces/330%20Ohm%20Resistor%20-%20Twins%20Chip%201-600x315w.jpg

7. Push Button
We will use this Push Button as Digital Input in this project

Source : https://cdn3.volusion.com/btfzd.umflq/v/vspfiles/photos/141-2.jpg?v-cache=1421684805

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

Source : https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2019/04/led_button_esp32_bb.png?resize=1024%2C680&quality=100&strip=all&ssl=1

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.

Rayhan Naufal Luthfi
Information System Technology ITB 2020
18220048

--

--

No responses yet