Embedded System Project 2: ESP32 Digital Input / Digital Output (I/O)

Michelle Lim
6 min readFeb 11, 2023

--

Hi, I’m Michelle and today I’ll be continuing my blog series of the embedded systems project! ✨🤩 (You can read the previous blog here 🤗)

The second project I’m making is starting to be a little tricky as it needs to read digital input (push button switch) to control digital output (LED). However, as the instruction’s pretty clear on the module given, you guys should be able to still follow it! 😉 Now, shall we begin? :)

PREPARATIONS BEFORE STARTING THE PROJECT

1. Buy the Components

As for this project, there are several components you need to buy aside from the ESP32 development board and the micro-USB cable. I’m buying it together with other components that will be needed for later projects, but you can also buy the ones you need for this project only (the list is written down below). For references, I attached the link and pictures of where I bought it (not an endorsement). 😊

This project requires you to have:

  1. Jumper wires for Breadboard (better buy it in a set that consists of all male-to-male, male-to-female, and female-to-female); Link: https://shp.ee/qpd72ka
  2. Breadboard 800 points (you can also buy 2 of breadboard 400 points that can be attached together; I’m buying this one); Link: https://shp.ee/67hmfga
  3. Resistor 330 Ohm; Link: https://shp.ee/v457jqa
  4. Push button; Link: https://shp.ee/rzmfgna
  5. Resistor 10K Ohm; Link: https://shp.ee/h85zuia
  6. LED (any color would be fine, I’m using red so that it will be easily visible); Link: https://shp.ee/5vjpp4a
  7. ESP32 Development Board (used in the previous project)
  8. Micro-USB Cable (used in the previous project)

The components you need on the list (number 1–6) are squared red on the pictures down below. I bought it all in the same store: CNC STORE BANDUNG (Shopee Link: https://shp.ee/kqysdda)

p.s. there are some components that I buy more in quantity for emergency, such as the push button and LCD; but you actually only need one of each so you don’t have to buy more😉

the components I bought all at once for this and later projects

ESP-32 PROJECT 2: Digital Input — Digital Output (I/O)

Brief explanation: The ESP32 has an input pin and output pin that works as an interface to communicate with others. These pins are called GPIO (General Purpose Input Output). Even though every pins are interchangeable, there are default pins for any kinds of interface. These usage of pins depends on the program code. For reference, you can read more about the ESP32 pins here. Let’s now start the project 😄

1. After preparing all the components, you can arrange it by this schematic on the breadboard.

This is my arrangement for reference:

my schematic arrangement

I connected the power pin 3V3 to the positive part of the breadboard (the brown wire), and the ground pin GND to the negative part of the breadboard (the black wire). After that, I used a male-to-male red wire to connect the power rails (the positive part) and terminal strip of the bread board. Then, I connected the LED with a male-to-male jumper wire (the red wire) to GPIO 5 with 10K Ohm resistor. The LED has 2 pins, the shorter one is cathode(-) and the longer one is anode(+). I also connected the push button with 330 Ohm resistor with a male-to-male jumper wire (the white one) to GPIO 4 according to the schematics referred. After arranging, I connected the ESP32 to my laptop with a micro-USB cable.

2. Write the code

File > New Sketch > Type in the code

Code explanation: We need to initialize first the number of GPIO used for the LED and the button, which is 4 for the button and 5 for the LED. The push button status is then set to 0, when it is not pressed. On the setup() code that will only run once, we’ll see Serial.begin(115200) which means that the baud rate is 115200 bits per second. Then, we initialize the push button pin as the input and the LED pin as the output. On the loop() code, it will first read the state of the push button as input. If the buttonState is HIGH (when the button is pressed), the ledPin will be written HIGH, and the LED will be turned on, and vice versa. As for the code Serial.println(buttonState), it means that the button status will be printed on Serial Monitor on 115200 baud. It will be printed 1 if pressed, and 0 if not.

After writing the code, don’t forget to compile and upload it!😉

and Voila! the LED will turn on when the push button is pressed, and off when the push button is not pressed 😆

push button pressed > LED on ; push button released > LED off

More explorations

After experimenting on 1 LED, I decided to add another LED to the schematic. I connected a Blue LED with a 10K Ohm resistor just like with the Red LED, and connected it to GPIO16 (purple wire); as shown in the picture below.

Modified schematic

I also modified the code twice, the first one is to use the pushButton in turning on and off both LEDs together.

Turning on and off both LEDs together

Code explanation: the ledPin2 (the blue LED) is connected to GPIO16 as shown in the code const int ledPin2 = 16. Then I added the ledPin2 as an output, together with the ledPin1. After that, I included it in the loop together with ledPin1. When the pushbutton is pressed (buttonState == HIGH), both LEDs will be turned on, and vice versa.

Another modification of the code is to use the pushButton in switching between both LEDs.

Switching between two LEDs

Code explanation: After initializing the ledPin2 and adding it to the output, I changed the code in the loop. When the pushbutton is pressed (buttonState == HIGH), the red LED will be turned on and the blue LED will be turned off. On the contrary, when the pushbutton is not pressed, the blue LED will be turned on, and the red LED will be turned off.

In conclusion, the LED pin output depends on the pushbutton pin input. If the pushbutton is pressed, the buttonState is HIGH, the LED pin will be turned on, and vice versa.

So that’s it, we’re done with our second Embedded System Project: ESP32 Digital Input / Digital Output (I/O). (yeah!! 🥳) Stay tune for the next projects and stay safe and healthy 🥰

--

--