"Inputs and Outputs" Program on ESP32

 Hello there!

In today's article there will be a simple project for "input and output" on ESP32. By using the same principle, the latter will show more advanced project. So be sure to check until the end of the article.



Simple I/O Project

STEP 1: Required Hardwares and Softwares

The hardwares will be used for the simple and advanced project.

1. ESP32 Development Board

2. PC with Arduino IDE

Makes sure to install Arduino to build the source code

3. Micro USB Cable

4. Mini LED Lamp

For the output indicator

5. Button

I will be using button as the input

6. Resistor

In this project, i will be using resistors with 10k Ohm and 330 Ohm resistance

7. Male-to-male Jumper Cable

8. Breadboard








STEP 2: Building the Circuit

The basic for simple circuit is shown below. The explanation for each component are:

  • The LED

From an output pin in ESP32, connect it with the positive pole of the LED. The negative pole connected to a 330 Ohm resistor then connect the other side of resistor to the GND pin of ESP32.

  • The Button
One side of the button will connect to 3v3 pin. The other side will connect to an input pin from ESP32 and a 10k Ohm resistor. The other side of resistor will connect to GND pin.

Repeat the circuit for each I/O component used.


STEP 3: Programming the Source Code

The source code i use is shown below.

First, set the pin number according to the pin you will be using. I use pin 4 for the input and pin 5 for the LED output. Then, initialize the button state for 0 or LOW (button is not pressed). Set the button pin as an input and the LED pin as an output. In the loop() section, buttonState will check if there is any button read using digitalRead. If the buttonState is HIGH (button is pressed), the LED will turn on and vice versa.
Verify the code to make sure there are no error by clicking check list button.

STEP 4: Uploading and Testing

After the source code has been verified, upload the code by clicking the arrow button. When the source code already uploaded into ESP32, check if it is working by pressing the button. The result will more or less like as shown.



Advanced I/O Project

In this section of the article, i will be making the more advanced I/O projects. I will be using 3 button and 2 LED lamps. The first and second buttons will act as a switch for LED 1 and 2 respectively. It means, the lamp will stay on/off after the button are pressed. The third button will initiate LED 1 and 2 to be blinked with 200 ms delay. The first and second button will also act as the turn off button for the blinking mechanism.

STEP 1: Required Hardwares and Softwares

The required hardwares and softwares are same as the simple project, but i will be using more cables, buttons, and LED lamps.

STEP 2: Building the Circuit

The graph of the circuit is shown below. The balloon with pin number will show which pin i will be using for each button or LED.

I assemble my circuit in breadboard. Since it use many long jumper cables, the breadboard i use is looking like a mess. It's better to use shorter cable so the board is neater.

STEP 3: Making the Source Code

The code i will be using to implement this project is shown below.


Initialize the button and LED pin according to the circuit i build. Then i initialize every type variable i will be using. The last_button variable will store last state of the button to make the button a switch. In the loop(), the last_button will have button value before it read the input. 

For the first 2 section of the loop, it check whether the last button state is HIGH and the current button state is LOW (the button is released after being pressed). After they check it, it will switch the value of the led.

The third section will also check if the last state of button 3 is HIGH and current state is LOW. Then it will enter the while loop. In the while loop, the first LED will switch it's state and the second LED will negate the state of first LED. After 200 ms delay, the program will check input on either button 1 or button 2. If there is an input, it will change the last button 3 state and exit the while loop. If there are no input, the lamp will blink with 200 ms delay.

After the source code done, verify it before uploading to ESP32.

STEP 4: Uploading and Testing

Upload the code into ESP32 development board. Then check if each buttons is working. The result will be in the video.

You can try more creative program by using I/O principle. Keep experimenting...!!!

Comments

Popular posts from this blog

Programming "Blinking Internal LED" on ESP32 using Arduino IDE

"Built-in Touch, Hall-Effect, and Temperature Sensor" on ESP32 using Arduino

Multiple I2C Devices using ESP32