About this tutorial:
ESP32 Basics
1. Introduction to ESP32
2. ESP32 Arduino Integrated Development Environment
3. VS Code and PlatformIO
4. ESP32 pins
5. ESP32 input and output ☑
6.ESP32 Pulse Width Modulation
7. ESP32 analog input
8. ESP32 interrupt timer
9 .ESP32 deep sleep
ESP32 Protocol
ESP32 web server
ESP32 LoRa
ESP32 BLE
ESP32 BLE Client-Server
ESP32 Bluetooth
ESP32 MQTT
ESP32 ESP-NOW
ESP32 Wi-Fi
ESP32 WebSocket
ESP32 ESP-MESH
ESP32 mailbox
ESP32 SMS
ESP32 HTTP get POST
HTTP GET Web APIs
HTTP POST Web APIs
ESP32 server articles
Keep updating, follow bloggers and don't get lost! ! !
ESP32 Sensor Module
Keep updating, follow bloggers and don't get lost! ! !
ESP32 Ultimate Combat
More than 100 ESP32 practical projects, please pay attention! ! !
In this getting started guide, you will learn how to read digital inputs like push button switches and control digital outputs like LEDs using the ESP32 with Arduino IDE.
prerequisites
We will program the ESP32 using the Arduino IDE. So make sure you have the ESP32 board add-on installed before proceeding:
ESP32 control digital output
First, you need to set the GPIO you want to control as an output . Use the pinmode() function as follows:
pinMode(GPIO, OUTPUT);
To control a digital output, you just need to use the digital write() function, which accepts as parameters a GPIO (int number) and a state, either high or low .
digitalWrite(GPIO, STATE);
All GPIOs can be used as outputs except GPIOs 6 to 11 (connected to the integrated SPI flash memory) and GPIOs 34, 35, 36 and 39 (input-only GPIOs);
Learn more about ESP32 GPIO: ESP32 GPIO Reference Guide
ESP32 read digital input
First, set the GPIO to be read as an input , using the pinmode() function as follows:
pinMode(GPIO, INPUT);
To read a digital input, such as a button, you can use the digitalread() function, which accepts as an argument the GPIO (integer) you are referring to.
digitalRead(GPIO);
All ESP32 GPIOs can be used as inputs except GPIO 6 to 11 (connected to the integrated SPI flash).
Learn more about ESP32 GPIO: ESP32 GPIO Reference Guide
Engineering example
To show you how to use the digital input and digital output we will build a simple example project with a button and LED. We will read the state of the button and light the LED accordingly as shown in the image below.
schematic diagram
Before continuing, you'll need to assemble a circuit with an LED and a button. We connect the LED to GPIO 5 and the button to GPIO 4 .
required parts
Here is the list of parts needed to build the circuit:
ESP32 (read Best ESP32 Dev Boards)
5mm LED
330 ohm resistor
Button
10k ohm resistor
Breadboard
Jumper wires
the code
Copy the code below to your Arduino IDE.
// 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);
}
}
Detailed code
In the following two lines, you create variables to assign pins:
const int buttonPin = 4;
const int ledPin = 5;
The button is connected to GPIO 4 and the LED is connected to GPIO 5. When using the Arduino IDE with ESP32, 4 corresponds to GPIO 4 and 5 corresponds to GPIO 5 .
Next, you create a variable to hold the button state. By default it is 0 (not pressed).
int buttonState = 0;
Inside steup() , you initialize the button as an input , and the LED as an output . To do this, you use pinmode() to accept the function of the pin you are referring to, and the mode: input or output .
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
Inside loop() is where you read the button state and set the LED accordingly.
In the next line, you read the button state and save it in the button state changeable. As we have seen before, you use the number read() function.
buttonState = digitalRead(buttonPin);
The following if statement checks if the button state is HIGH . If so, it uses the digitalWrite() function that accepts as parameters LEDPin , and state HIGH .
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
}
If the button state is not high , you turn the LED off. Just set the low number as the second argument to the write() function.
else {
digitalWrite(ledPin, LOW);
}
upload code
Before clicking the upload button, go to Tools > Boards and select the board you are using.
Go to Tools > Ports and select the COM port the ESP32 is connected to. Then, press the upload button and wait for the " upload complete " message.
If you see a lot of dots (…__…__) and “Failed to connect to ESP32: Timed out waiting for packet header” message in the debug window, it means you need to press the ESP32 onboard BOOT button after the dots start appearing.
demonstration
After uploading the code, test your circuit. When you press the button, your LED should light up:
Close when you release it:
Summarize
With this getting started guide, you learned how to read digital inputs and control digital outputs with the ESP32 using the Arduino IDE.