Simple I/O on ESP32
Hello! I will explain the steps to make a simple I/O on ESP 32. We will learn how to light the LED by pressing the push button.
First, we need to prepare the hardware or components needed for this project.
1. ESP32
2. Laptop/PC
3. Micro USB
4. Breadboard
5. LED 5 mm
6. Resistor 330 ohm
7. Resistor 10k ohm
8. Male-to-male jumper
9. Push button
After you're done preparing the components, make sure the Laptop/PC you're using has the Arduino IDE and USB to UART driver installed. You can refer to my first post (https://gratgrut.blogspot.com/2022/02/blinking-led-on-esp32.html) on how to download and install them.
Then, you can start by arranging the circuitry first.
1. Attach the ESP32 on the breadboard. If you have a wide type ESP 32 that only left 1 slot in the breadboard, make sure to leave the slot with 3V3 empty, like this picture.
2. Connect 3V3 with the positive side of the power rails by using a jumper.
3. Connect the ground (GND) to the negative side of the power rails by using a jumper.
4. Make a circuit by connecting the ground to both the button and the LED. Use a 10k ohm resistor for the connection with the push button.
5. Attach a 330-ohm resistor for the connection with the LED.
6. Attach the push button, connected to the 10k ohm resistor.
7. Connect the push button with GPIO 4.
8. On the other side of the push button, connect it with the positive side of the power rails by using a jumper so it will be connected to the ESP 32.
9. Attach the LED connected to the 330-ohm resistor. The negative pin is on the same side as the ground.
11. Connect the LED with GPIO 5 by using a jumper.
After you're ready with the circuitry, you can connect the ESP32 board to your laptop/PC.
1. Open arduino.exe
2. Write this code. As you can use different GPIO(s), make sure to write it in constant so you can easily change it to match the GPIO you're using.
const int buttonPin = 4;
const int ledPin = 5;
// variable for storing the pushbutton status
int buttonState = 0;
void setup() {
// put your setup code here, to run once:
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() {
// put your main code here, to run repeatedly:
// 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);
}
}
3. Upload the code to the ESP32 board.
4. Voila! You can turn the LED on by pushing the button.
more ideas:
You can add one more LED and set them the turn off when the push button is pressed.
1. Add one more LED, 330-ohm resistor, and a jumper to connect it with the ESP32. I'm connecting it to GPIO 19.
2. Set the code like this.
const int buttonPin = 4;
const int ledPin = 5;
const int led2 = 19;
// variable for storing the pushbutton status
int buttonState = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
// initialize the pushbutton pin as an input
pinMode(buttonPin, INPUT);
// initialize the LED pin as an output
pinMode(ledPin, OUTPUT);
// initialize another LED pin as an output
pinMode(led2, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
// 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);
// turn LED 2 off
digitalWrite(led2, LOW);
} else {
// turn LED off
digitalWrite(ledPin, LOW);
// turn LED 2 on
digitalWrite(led2, HIGH);
}
}
3. You can see them taking turns in turning on and off by pushing the button :)
Or you can make it blink by setting the code like this
const int buttonPin = 4;
const int ledPin = 5;
const int led2 = 19;
// variable for storing the pushbutton status
int buttonState = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
// initialize the pushbutton pin as an input
pinMode(buttonPin, INPUT);
// initialize the LED pin as an output
pinMode(ledPin, OUTPUT);
// initialize another LED pin as an output
pinMode(led2, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
// 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);
// turn LED 2 off
digitalWrite(led2, LOW);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
digitalWrite(ledPin, HIGH);
} else {
// turn LED off
digitalWrite(ledPin, LOW);
// turn LED 2 on
digitalWrite(led2, HIGH);
delay(500);
digitalWrite(led2, LOW);
delay(500);
digitalWrite(led2, HIGH);
}
}
Then you can see the LED blinking when you press the button!
That's all! Hope this helps :)












Comments
Post a Comment