ESP32 Project: Input Output

Rania Sasi Kirana
6 min readFeb 11, 2023

--

a step-by-step guide

Hello and welcome back! Let me reintroduce myself, my name is Rania Sasi Kirana and this is a story about a project I did in my class, II2260 Embedded System in Bandung Institute of Technology. If you’re new here, kindly check out my first article on ESP32 Project: LED blink.

About this project

Today I’ll be telling you about how a microcontroller interacts with the outside world, specifically how to read digital inputs and control digital outputs using the ESP32 board programmed with Arduino IDE. In this project, the push button switch will act as the digital input and LED as the digital output.

More on ESP32

ESP32 communicates with the outside world using input and output pin called General Purpose Input Output (GPIO) as interface, where this pin can be programmed as an input or an output. ESP32 has a variety of I/O interfaces, namely Analog-to-Digital Converter (ADC) and Digital-to-Analog Converter (DAC) interfaces, Serial-Parallel Interface (SPI), I2C, I2S, and UART interfaces, PWM output channel, and GPIO Capacitive Sensing. On ESP32, even though each pin can be switched, but still, there’s a default pin for every particular need, which pins are good for input, and which are for output.

Step 1: Software Required

Before we start, make sure you’ve installed Arduino IDE and the ESP32 boards add-on as we will program the ESP32 using Arduino IDE. If you haven’t installed it yet, don’t worry because I did a step-by-step guide on installing it in my first article on ESP32 Project: LED Blink.

Step 2: Hardware Required

  • DOIT ESP32 DevKit v1
  • LED
  • Resistor 330 ohm
  • Resistor 10k ohm
  • Push button switch
  • Breadboard
  • Jumper wires
female to male jumper wires
male to male jumper wires

The female to male jumper wires is used to connect the components on the breadboard to the ESP32’s pins, while male to male jumper wires is used to connect between components on the breadboard.

Step 3: Onto the Next Step

After preparing your hardware and setting up your software, we can go ahead and start the project. All you need to do now is replicate the scheme underneath.

source: https://RandomNerdTutorials.com/esp32-digital-inputs-outputs-arduino/

After you’re done replicating the scheme, you can go ahead and plug in the ESP32 to your computer using a USB cable. Then, open Arduino IDE and select board and port accordingly. You can just copy and paste the code below to the main program of the Arduino IDE. Upload and verify to run the program.

// Complete Instructions: https://RandomNerdTutorials.com/esp32-digital-inputs-outputs-arduino/

// 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);
}
}

On a side note, if you’re having trouble selecting the right board and port, uploading, or verifying, you can check out my first article on ESP32 Project: LED Blink as I’ve explained the step-by-step process on selecting the right board and port to connect the ESP32 to your computer and running the program.

If the project were done successfully, when you press the push button switch, the LED should turn on and when you let go of the push button switch, the LED should turn off. But if it doesn’t work, then you’re not done yet.

Step 4: What if it didn’t work?

I didn’t successfully do this project on my first attempt. So, I’m going to share problems that occur when I did this project and how to fix it.

When it didn’t work the first time, I switched the legs of the LED to see if I had put the positive pole (anode) and the negative pole (cathode) on the wrong sides. The LED turned on instantly after I did that. The problem now is that it shouldn’t have turned on because I didn’t press the push button switch yet. The code dictates that the LED only were supposed to turn on if the push button switch were pressed, or else it will be off.

Fortunately, my friend had encountered the same problem. You can try unplugging the USB cable from your computer, then plug it again. You don’t have to rerun the program, you can just push the button switch to see if the LED turn on. And mine worked after that, the LED turns on when I press the push button switch and turns off when I release it.

The Finished Project

Bonus Project: 2 LED

For the bonus project, I built the same circuit, but I added one more LED. All you need is an extra LED, male to male jumper wire, and resistor. It’s basically the same as the previous project, only you have to connect the extra LED to the circuit.

Bonus Project: 2 LED

Then, you can use the exact code from the previous code to program the extra LED since you basically just connected the LED to the same circuit.

// Complete Instructions: https://RandomNerdTutorials.com/esp32-digital-inputs-outputs-arduino/

// 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);
}
}

According to the code, when you press the pushbutton, both LED will turn on, and when you release it, both LED will turn off.

Bonus Project: 2 LED

--

--