Getting Started With ESP32 & Arduino IDE: Simple I/O
Now that we’ve set up our ESP32 board and tried a simple blinking LED program (click here to see the previous article) we’re ready for the next thing we need to learn which is how to read digital inputs like a button switch to control digital outputs like an LED. So let’s get started!
In this project we want our ESP32 and our program to be able to read inputs from the outside world. One of the simplest example is none other than the pushbutton since it only has 2 states, on and off. In the future, we’ll be able to apply more complex forms of inputs such as from sensors or a string of characters typed from a keyboard, but let’s not get ahead of ourselves.
For our first project, we want an LED light to be on when we press on a push button and for it to be off when the button is not pushed. For our second project, there’ll be 2 LED lights. One LED will behave exactly as the previous project. The other LED will behave the opposite, so it’ll turn on when the button is not pressed and vice versa.
Required Hardware
The components that I used to build my circuit are as follows:
- ESP32
- Breadboard
- 5 mm LED
- 330 Ω resistor
- Pushbutton
- 10k Ω resistor
- Jumper wires
- Laptop
- Micro USB cable
Required Software
- Arduino IDE
- USB to UART port driver
These are the same softwares that we used in our last project, more details on the installation and setup process of both of these can be found in my last article (click here to see the previous article).
Project 1
Part 1: Assemble circuit
For this circuit we’re gonna use 1 pin as our input which is connected to the button (I used pin 4) and 1 pin for the output which is connected to the LED (I used pin 23). We’re also gonna use the 3.3V pin (indicated by the 3v3) and ground pin. Make sure to also connect the 10K Ω resistor and 330 Ω resistor to the button and LED in that order. The circuit should look something like this:

Part 2: Code
Here’s the code that I used:
// set pin numbers
const int buttonPin = 4; // the number of the pushbutton pin
const int ledPin = 23; // 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);
}
}
Part 3: Verify & Upload
follow the steps from my previous article to verify and upload your code. After that, you are ready to test it. If your LED lights up when the button is pushed that means your code is working. Congrats!
Project 2
For our second project, we’re only gonna make a few tweaks to the first one. Here, we want another light is always on unless the button is pressed so it will basically work in the opposite way from the first
Part 1: Assemble circuit
The only additional components that we need is another jumper cable, LED light, and 330Ω resistor. Connect the LED to the development board as you have in previous projects (I used pin 21) and your circuit should look something like this:

Part 2: Code
Much like the circuit, we’re only gonna make a few tweaks from the previous project’s code. Here’s the code that I used:
// set pin numbers
const int buttonPin = 4; // the number of the pushbutton pin
const int ledPin1 = 23; // the number of the first LED pin
const int ledPin2 = 21; // the number of the second 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 both 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(ledPin2, HIGH);
digitalWrite(ledPin1, LOW);
}
}
Part 3: Verify & Upload
Verify and upload your code and voilà! ze project iss done monsieur! (why did I turn french??) anyway you can go ahead and test out the code!
This article is part of the II2260 embedded systems course with Dr. Kusprasapta Mutijarsa, S.T, M.T. of Institut Teknologi Bandung (ITB). A special thanks goes out to Ardi for lending me a few of the components (lol thx bestie)
refrences: