Simple I/O on ESP32

Pavita
4 min readFeb 10, 2022

Hello everyone, in this blog I will tell you about my second experience with ESP32, which is simple I/O. I made a system which external LED light can turn on by using a button. So, when you press the button, the LED light will turn on, but if you let go the button, the LED light will turn off.

What you need in this project are :

  1. ESP32

2. Micro USB Cable

3. Laptop/PC with Arduino IDE app installed

4. LED Light

5. Male to male jumper cable

6. Resistor 330 Ohm & Resistor 10k Ohm

7. Switch push button

After you have all of this equipment, you need to arrange the breadboard like this :

Now what you need to do is to arrange jumper cables, you need to 5 jumper cables to make it. And now you need to make your breadboard like this:

After you make it like this you need to add the code to your Arduino IDE. This is the code you need to add:

// set pin numbers
const int buttonPin = 4; // the number of the pushbutton pin
const int ledPin = 5; // 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);
}
}

And then upload your code to your ESP32. AND YEAY!! This is the result:

But I’m curious, what if I arrange it with 2 LED light. So, I add one more LED light, which is the red one. To add one more LED light I need one more jumper cable and one more resistor. The arrangement will look like this :

And with jumper cables :

Of course I need to change the code, so my code will look like

// set pin numbers
const int buttonPin = 4; // the number of the pushbutton pin
const int ledPin1 = 5; // the number of the LED pin
const int ledPin2 = 23;
// 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(ledPin1, OUTPUT);
pinMode(ledPin2, 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, HIGH);
digitalWrite(ledPin2, LOW);
} else {
// turn LED off
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
}
}

And after I upload the code to ESP32, this is the result :

So, this is the end of my experience with ESP32 I/O. Thank you for your attention. In approx. one week, I will update my next experience with ESP32 :)

Pavita Andrea (18220014)

--

--