Project 2 : Simple I/O

Gresya Leman
9 min readFeb 11, 2022

Hello everyone!
Welcome back to my journey on learning how to use ESP32. In this second chapter, here’s a little tutorial on how to utilize GPIO on ESP32 as input and output.

Prequisites

Since I’m using Arduino IDE to program the code, make sure to have the devices used already set up. If it’s not yet set up, check my first tutorial in this link : https://graell.medium.com/project-1-getting-started-with-the-esp32-led-blink-864ea7f11997

STEP 1 : Require all of the necessary components

ESP32
In this project I used ESP32 DevKit V1, but you can use any kind of ESP32.

DOIT ESP32 DevKit V1

Micro USB Cable
This cable will be used as a connector between our device and the ESP32.

Micro USB Cable

Laptop/PC
Use the device of your choice, in this project I used my laptop which is Asus TUF Gaming A15.

Laptop

Breadboard

You can use either the short one or the long one, but in this project I used the one I have which is the long one.

Breadboard

Male-to-Male Jumper cable (5pcs)

In this project I used the male-to-male jumper cable because I secured the ESP32 on the breadboard. But you can use male-to-female jumper cable to connect to the ESP32.

Male-to-Male Jumper Cable

330 Ohm Resistor

This resistor will be used for the led light.

330 Ohm Resistor

5mm LED Light

You can use either the 3mm or the 5mm LED in any colors, but here I used a 5mm blue LED.

5mm Blue LED

Push Button Switch

You can use any kind of push button switch, but here I used a 4 pin tactile switch push button 6x6x5mm.

4 Pin Tactile Switch Push Button 6x6x5mm

10k Ohm Resistor

This resistor will be used for the push button switch.

10k Ohm Resistor

STEP 2 : Arrange the components on the breadboard

The components should roughly look like this. But you can use any pins other than GPIO 4 and 5 that can do GPIO function.

First, secure the ESP32 on the breadboard leaving 1 column of free space on the right side

Then attach the LED light on the breadboard with the positive leg on the right side (near the ESP32). Here the positive leg was on 6f and the negative leg was on 5f.

Attach the 330 ohm resistor next to the negative leg of the LED light (5i) and connect it to the negative side of the breadboard next to it.

Attach a male-to-male jumper cable (black) on a spot next to the positive leg of the LED (6f) to a spot next to the pin on the ESP32, here I used pin 22.

Moving on to the switch, attach the 4 pin tactile switch push button between the left and right side of the breadboard (9e, 9f, 11e, and 11f).

Attach the 10k resistor next to one of the legs of the switch (9i) and connect it to a spot on the negative side of the breadboard.

Attach a male to male jumper cable (white) and a spot between the leg of the switch and a leg of the resistor (9g) to a spot next to the pin on the ESP32, here I used pin number 21.

Attach another male-to-male jumper cable (gray) on a spot next to another leg of the switch (11j) to the positive side of the breadboard.

Then attach a male-to-male jumper cable (blue) from a spot next to the GND pin on the ESP32 to the negative side of the breadboard.

And attach another male-to-male jumper cable (purple) from a spot next to the 3V3 pin on the ESP32 to the positive side of the breadboard.

Connect the micro USB cable from the device (laptop) to the ESP32 and make sure the built in red LED on the ESP32 is on.

STEP 3 : Code the program

Open the Arduino IDE and make a new file. Save the file where ever you like it.

Then type in initialization of the variables used in the program before the void setup() and void loop() part. Here I used the variables buttonpin, ledpin, and buttonstate.

const int buttonpin = 21;
const int ledpin = 22;
int buttonstate = 0;

Since I’m using GPIO pin number 21 as a pin for my switch and GPIO pin number 22 for my LED light, I initialized the buttonpin as 21 and ledpin as 22. The buttonstate is a variable to hold the button state and it’s 0 by default (not pressed).

Then in the void setup() part, type in:

void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(buttonpin, INPUT);
pinMode(ledpin, OUTPUT);
}

Here I initialized that the button (switch) as input and the LED as output. The pinMode function is used to refer to the pin used and the mode (input or output).

And in the void loop() part, type in the main body of the program :

void loop() {
// put your main code here, to run repeatedly:
buttonstate = digitalRead(buttonpin);
Serial.println(buttonstate);
if(buttonstate == HIGH){
digitalWrite(ledpin, HIGH);
}else{
digitalWrite(ledpin, LOW);
}
}

So the whole file should look like this.

Click on the tick icon on the top left of the Arduino IDE to compile the code and wait until it says ‘Done compiling’.

Before uploading the code, don’t forget to make sure that the ESP32 is connected. Also make sure the Arduino IDE is set on the right board and port on the Tools menu.

Then click on the right arrow icon next to the tick icon to upload the code to the ESP32 and wait until it’s done uploading.

Now the LED light should turn on when the push button is pressed and will turn off when the switch is not pressed. The result will show up as in the video attached below.

https://drive.google.com/file/d/1yGc9L5fbYOjxWOY4YhvDj2ASD1guYOXF/view?usp=sharing

— ADVANCED I/O —

I tried making a variation of the circuit by adding 1 LED light and 1 push button switch. I coded the program so that the buttons do different function. Button 1 will make the LED lights blink alternately with 200ms delay, while the other one will turn on both of the LED lights simultaneously. This is how I made it.

Additional components required

I needed to add 1 more each of the 5mm LED light (red), 330 ohm resistor, 4 pin switch push button and10k resistor also 3 male-to-male jumper cables.

Arrangement made

To do this, I made the same arrangement as the simple one and added the LED light next to the first LED light with the same arrangement and connected it to pin number 19 on the ESP32. That also applied to the switch, and I connected it to pin number 23 on the ESP32. Then connect the micro USB cable from the device to the ESP32. The overall circuit should look like the image below.

Do some changes in the code

After the arrangements in the circuit is completed, open the Arduino IDE to do some changes in the code.

On the top, initialize another variable to represent the second LED light and the second switch in the circuit, here I used ledpin2 and buttonpin2. ALso we nees another variable to represent the state of the second switch in the circuit, here I used buttonstate2. And the top part should look like this.

 const int buttonpin = 21;
const int buttonpin2 = 23;
const int ledpin = 22;
const int ledpin2 = 19;
int buttonstate = 0;
int buttonstate2 = 0;

Then on the void setup() part, initialize the ledpin2 as an output, so the code should look like this.

void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(buttonpin, INPUT);
pinMode(buttonpin2, INPUT);
pinMode(ledpin, OUTPUT);
pinMode(ledpin2, OUTPUT);
}

Since I wanted to make the light blink alternately with 200ms delay when switch number 1 is pressed, and both of the LED to turn of when the switch number 2 is pressed, I have to add it on the main program, so the code on the void loop() part should look like this.

void loop() {
// put your main code here, to run repeatedly:
buttonstate = digitalRead(buttonpin);
buttonstate2 = digitalRead(buttonpin2);
Serial.println(buttonstate);
if(buttonstate == HIGH){
digitalWrite(ledpin, HIGH);
delay(200);
digitalWrite(ledpin, LOW);
digitalWrite(ledpin2, HIGH);
delay(200);
digitalWrite(ledpin2, LOW);
}else{
digitalWrite(ledpin, LOW);
}
if(buttonstate2 == HIGH){
digitalWrite(ledpin, HIGH);
digitalWrite(ledpin2, HIGH);
}else{
digitalWrite(ledpin, LOW);
digitalWrite(ledpin2, LOW);
}
}

The code overall should look like this.

Click on the tick icon on the top left of the Arduino IDE to compile the code and wait until it says ‘Done compiling’

Before uploading the code, don’t forget to make sure that the ESP32 is connected. Also make sure the Arduino IDE is set on the right board and port on the Tools menu.

Then click on the right arrow icon next to the tick icon to upload the code to the ESP32 and wait until it’s done uploading.

Now if I press the switch on the right, the LED lights should blink alternately, if I press the switch on the left, both of the LED lights should turn on simultaneously and both LED lights should turn off when the switch is not pressed. The result will show up as in the video attached below.

https://drive.google.com/file/d/1-5XCaLa-_SMXJ6Phkh7xsfT2SLGJacNq/view?usp=sharing

This is the end of the tutorial and I hope this helps!

I didn’t really face any significant struggles doing this project, but the micro USB port on my ESP32 broke and the code couldn’t be uploaded to the ESP32, so I had to buy a new one :(. But the rest of it went pretty smoothly and I am satisfied with how my experiment turned out.

See you in my next projects!

--

--