Basic Input Output — Embedded System ESP32 Project #2

Hilmi Baskara Radanto
4 min readFeb 12, 2023

--

Konnichiwazzup all!

After successfully operated LED ESP32 Blink, let’s go to the next level of ESP32 learning. This time I’m gonna learn the concept of input output in embedded system with ESP32 by turning LED lamp ON and OFF with push button.

Component Needed:

  1. Breadboard
  2. ESP32
  3. 5 Male-to-Male Jumper Cable
  4. 10k-Ohm resistor
  5. 330 Ohm resistor
  6. LED lamp

Schematic Diagram

In reality, it’s actually flexible to build the circuit as long as it consistent with the concept

How to build the circuit?

  1. Prepare all the components needed
  2. Place the ESP32 on the breadboard, preferably at the edge of the breadboard
  3. Connect the 3v3 pin of ESP32 to the positive side of breadboard, marked with red color
  4. Connect Ground(labelled GND on ESP32) to the negative side of breadboard, marked with blue color.
  5. After successfully connecting ESP32 and breadboard, proceed to connect ESP32 with push button and LED. Connect the GPIO4 (labelled D4) to one of the push button pin. Then connect GPIO5 (labelled B5) with LED’s positive pin (the longer one).
  6. Connect LED lamp with 330 Ohm resistor to negative side with breadboard and place 10k Ohm resistor between push button and negative side of breadboard. Resistor is used to reduce the voltage input to the components.
  7. Last, connect the other pin of push button with the positive side of breadboard.
  8. Circuit is done, let’s go to the programming!

Programming

Once again, Arduino already have the source code to program our today’s project. To access, go to File > Example > Digital > Button. I made slight modification to the code to adjust the ESP32’s pin, change to buttonPin to 4 and ledPin to 5.

Arduino is so rich, isn’t it?
Final version of the code.

Result

LED connected with 330 ohm.

You can try some variations to the resistor connected to LED lamp. Theoretically, the bigger ohm the resistor, the more dim the light will be.

with 10k Ohm resistor

My self-curiousity is still high up in the sky. So, I experimented with two LEDs by tweaking some lines of code and circuit, and Voila! Now I can turn two LED on at the same time!!

/*
Button

Turns on and off a light emitting diode(LED) connected to digital pin 13,
when pressing a pushbutton attached to pin 2.

The circuit:
- LED attached from pin 13 to ground through 220 ohm resistor
- pushbutton attached to pin 2 from +5V
- 10K resistor attached to pin 2 from ground

- Note: on most Arduinos there is already an LED on the board
attached to pin 13.

created 2005
by DojoDave <http://www.0j0.org>
modified 30 Aug 2011
by Tom Igoe

This example code is in the public domain.

https://www.arduino.cc/en/Tutorial/BuiltInExamples/Button
*/

// constants won't change. They're used here to 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 = 19;

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin1, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
// initialize buzzer
pinMode(ledPin2, OUTPUT);
}

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

// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
}
}
Cool…

And that’s all for me. Let’s call it a day!

Arigathanks Gozaimuch!

--

--