Intro to Embedded System with ESP32 [Digital I/O]

Reinhart Lim
4 min readFeb 11, 2023

--

In this blog, I will continue the ESP 32 project that I began last week. I’ll share my experience regarding Digital I/O with ESP32 by creating a small project using LED and a push button.

ESP 32 features I/O pins to communicate with others. These pins are called General Purpose Input Output (GPIO). GPIO can be used to receive signals from digital input or analog. It also can produce signals for digital output or analog.

Here are, the pins for ESP32 DevKit DOIT and the reference we’ll use later for our 2nd project.

https://randomnerdtutorials.com/esp32-pinout-reference-gpios/

— Component Required

  • ESP32
  • LED
  • 330 Ohm resistor
  • Push button
  • 10k Ohm resistor
  • Breadboard
  • Jumper wires

Here’s the circuit we’ll follow to do this experiment.

https://randomnerdtutorials.com/esp32-digital-inputs-outputs-arduino/
  1. Using a female-to-male jumper wire, connect the 3v3 pin near the red line on the breadboard.
  2. Using a female-to-male jumper wire, connect the GND (Ground) pin near the blue line on the breadboard.
  3. Connect the push button and LED to the breadboard, then connect the GPIO 4(D4) and GPIO 5(D5) pins as indicated in the image above.
  4. Finally, connect the 330 Ohm resistor to the LED cathode and then the 10k Ohm resistor to the push button as shown in the picture above.

This is what it looks like after setting up the circuit.

— Code

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

The code I’ll use in this project is provided by RandomNerdTutorial however the Arduino IDE has a similar code (File > Examples > Digital > Button) with different pin numbers constant that you may alter based on the used pin.

First, buttonPinand ledPinconstant must be assigned with the connected pin number at the ESP32 thenbuttonStateconstant is initialized with 0.

In setup()function is used to initialize the push button and led pin. After that, the loop() function is used to loop the read state of the push button.

This is the result after running the code on Arduino IDE.

Result

The scenario was when the push button was pushed, the LED light will turn on and vice versa.

— Modified Experiment

// set pin numbers
const int buttonPin = 4; // the number of the pushbutton pin
const int ledPin = 5; // the number of the LED pin
const int ledPin2 = 18;

// 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);
pinMode(ledPin2,OUTPUT);
}

void loop() {
// read the state of the pushbutton value
buttonState = digitalRead(buttonPin);
Serial.println(buttonState);

if (buttonState == LOW) {
// turn LED on
digitalWrite(ledPin, LOW);
digitalWrite(ledPin2,HIGH);
} else {
// turn LED off
digitalWrite(ledPin, HIGH);
digitalWrite(ledPin2, LOW);
}
}

I made a slight modification to the previous code and circuit by adding a green LED that will light up when the button is not pressed. The red LED will light up when the button is pressed.

Modified

By following the step above you’ve already finished this simple project about ESP32 Digital I/O. Congrats 🎉. This is simply a simple experiment with digital I/O; you may try more complex circuits and programs.

I hope this blog can be useful for those who are learning and getting started using ESP32. That’s all for this project, stay tuned for the next experiment with ESP32 !!

--

--