Embedded System Project 2: I/O With ESP32

Fernaldi Fauzie
5 min readFeb 2, 2021

Our journey with ESP32 is still only first page of many pages! This time, I would like to share about my second project with ESP32, which is about I/O with ESP32. At this point, I would like to do 2 experimentation, first one is digital I/O with ESP32 and second one is analog I/O with ESP32. Check it out!

Digital Inputs and Digital Outputs

Before I start, I need to prepare :

  • Microcontroller ESP32 Development Board
  • USB Type A Male to Micro USB
  • Breadboard
  • 5 mm LED (2)
  • 330 Ohm resistor (2)
  • Pushbutton
  • 10k Ohm resistor
  • Jumper wires (I’m using 6 male to male jumper wires)

For some reason, when I made schematic diagram in circuito.io, I can’t modify the ESP32 type, resistor type, which ESP32 pin used, the location of components (all of it automatically), which mean this schematic diagram is not 100% same with what I made, but in general this schematic diagram can bring you to understand more about what I made.

So, there are some notable difference like :

  • I used 330 Ohm resistor instead of 100 Ohm resistor in Schematic Diagram
  • ESP32 pin that I used for input and output.

So, this is the result of schematic diagram with circuito.io

Schematic Diagram

After I made this schematic diagram, I still don’t feel really satisfied with what I have done. So I tried to look and find another alternative to make schematic diagram, and then that’s when I found fritzing. Finally I can make a better schematic diagram and here it is!

Also Schematic Diagram but better

Of course, different setup with mine can also work, as long you be careful with what you do. Here is good ESP32 pinout reference from randomnerdtutorials.com if you need it.

As I am done with schematic diagram, let’s move to the code. You may look at my code and demo below. I am using pushbutton to do blink with 2 LED.

My Demo for Digital I/O

This demo is basically LED blink with pushbutton. If I press the pushbutton, first LED will turn on, while second LED will turn off, vice versa. I put 1000 ms (1 second) so that I don’t confused when looking at the blink. If I don’t press the pushbutton, all led will be turned off.

In conclusion, with pushbutton, you can do more things as you can use if, else if, and else in the code to put more condition. In my case, if I push the pushbutton, my 2 LED will do blink which is if first LED turned off, the other one will be turned off, vice versa. There are only 2 cases, the LED turned on or turned off because I am using digital signal as digital signal uses discrete values for representing the information. While if I don’t push the pushbutton, nothing will happened.

We Are Done With Digital Inputs and Digital Outputs, But How About Analog Output Instead of Digital Output?

So I kind of curious, what if I used analog output instead of digital output? Do I can use it like before with pushbutton? So now I am modifying my code as I am still using same circuit as before that can be seen at schematic diagram upward. This time, I want to generate PWM signals with the ESP32 using Arduino IDE.

Oh right, before you guys going here, make sure to pay attention to some things like :

  • PWM channel in ESP32 that can be used. There are 16 channels in ESP32 which is from 0 to 15.
  • PWM signal frequency for LED. In my case, I am using 5000 Hz.
  • Signal’s duty cycle resolution, we have resolutions from 1 to 16 bits. In my case, I am using 8-bit resolution, which means I can control the LED brightness using value from 0 to 255, although at the code below I am using value from 15 to 255 because I modified it.
  • GPIO or GPIOs that the signal will appear upon. I am using ledcAttachPin(GPIO, channel) function for this.
  • LED brightness that can be controlled with PWM. I am using ledcWrite(channel, dutycycle).

In this code, I am still using pushbutton to put more condition with my experiment. This time :

  • When the pushbutton pressed, the LED will not blink, but instead it will increase LED brightness, starting from dutyCycle equal to 15, keep increasing until it equal to 255. Then. it will decrease LED brightness, starting from dutyCycle equal to 255, keep decreasing until it equal to 15. Because its code at void loop, it will keep looping.
  • When the pushbutton released, it will turn off all LED.
Pressing the pushbutton
Releasing the pushbutton

To conclude, analog signal uses continuous values for representing the information. In my case, with analog signal I can adjust the brightness of LED more freely compared while I am using digital signal, because digital uses discrete values for representing the information. In my case, with digital signal I can only turn on or turn off the LED, can’t do more specific like analog signal.

It seems that my story about I/O is finally come to the end. I hope that this story can bring you guys more knowledge and of course, insight that can help you guys to more interested in ESP32 as our journey with ESP32 is still pretty long! See you in my next story!

--

--