top of page

ESP32 Digital Input and Output

In this article, I will tell you about my experience making the ESP32 read digital input from push button and control digital output an LED. This project is my second ESP32 project and it was given by my lecturers.


Prerequisites

I used Arduino IDE v1.8.19 in Windows 11 64-bit to write the code and upload the code to ESP32, you can download it in here. Just click on download, make sure it the right version and OS, and it will download a zip file. After the download is finished, extract the zip file and open the arduino.exe. It will open the IDE. You might use other IDE, but I recommend you to use Arduino IDE.


I also used some component that I bought at Jaya Plaza, electronic market located in Bandung. But you can get this component on online marketplace.


  1. an ESP32 microcontroller

  2. a Micro-USB type B to USB type A (Male-to-Male)

  3. Male-to-Male Jumper Wires

  4. a Push Button

  5. LED

  6. Resistor 330 Ω

  7. Resistor 10k Ω

  8. Breadboard

Step 1: Place and Connect Every Components

I used this schema as a reference for my circuit

ree

The GPIO5 connected to an LED, long legs, and GPIO4 connected to a push button. 3V3 pin (power) connected to positive circuit and GND pin to negative circuit. Then I connected the push button other legs (the one that didn't connect to GPIO4) to negative circuit and the first legs (the one that connected to GPIO4) to negative circuit with resistor 10k Ω. After that, I connected the LED, short legs, to negative circuit with resistor 330 Ω.


ree

Step 2: Upload the Code

I used Arduino IDE to write and upload the code. This is the code that I used:

// 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 might not be useful if you used different ESP32 models.

Step 3: Upload

Connect ESP32 with your laptop/PC so the Arduino IDE can upload the code to your ESP32 board. Click the Upload Arrow in the top left.



More about Input Output

So, I also do more experiment with the input and output ESP32. I created a circuit that receive 2 input to make 3 LEDs turn on/off and blink.


ree

Basically its the same thing from the first circuit I created, but with more components in it. I used Blue and Green LED that I borrowed from my friends and connected it just like I connected the Red ones from the simple input output circuit. Then, I connected the second push button. To connect the LED and Push Button to ESP32, I need to consider which pin I can use, so I search in internet what my ESP32 pin can do. This is the information I gained:


ree

Then, I write the code and upload it to my ESP32. This is the code:


// set pin numbers
const int buttonPin1 = 5;
const int buttonPin2 = 21;// the number of the pushbutton pin
const int ledPin1 =  4;
const int ledPin2 =  18;
const int ledPin3 =  19;// the number of the LED pin

// variable for storing the pushbutton status 
int buttonState1 = 0;
int buttonState2 = 0;

void setup() {
  Serial.begin(115200);  
  // initialize the pushbutton pin as an input
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  // initialize the LED pin as an output
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
}

void loop() {
  // read the state of the pushbutton value
  buttonState1 = digitalRead(buttonPin1);
  Serial.println(buttonState1);
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH
  if (buttonState1 == HIGH) {
    // turn LED on
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin3, HIGH);
  } else {
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
  }
  buttonState2 = digitalRead(buttonPin2);
  Serial.println(buttonState2);
  if (buttonState2 == HIGH) {
    // turn LED on
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin3, HIGH);
    digitalWrite(ledPin2, HIGH);
    delay(200);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin3, LOW);
    delay(200);
  } else {
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
  }
}

You can create many LED blinking patter by switch the LOW and HIGH.



 
 
 

Comments


bottom of page