ESP32

Esp32 Digital Inputs and Digital Outputs with Examples

Description:

Esp32 Digital Inputs and Digital Outputs with Examples- This tutorial is the perfect starting point for anyone looking to explore the exciting possibilities of physical computing. By following along with this comprehensive tutorial, you’ll gain the skills and confidence needed to master the art of reading digital inputs with ease and controlling digital outputs like a pro. Using the ESP32 microcontroller and the powerful Arduino IDE, you’ll learn how to effortlessly manipulate button switches and LED outputs to bring your projects to life in new and innovative ways. So why wait? Dive into the world of physical computing today and unleash your creativity with the help of this beginner-friendly tutorial.




Amazon Product links:

Esp32 Board

Leds

Breadboard

10k Resistor

330ohm Resistor

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

Before we begin our programming adventure with the ESP32, it’s important to ensure that you have the necessary tools at your disposal. Specifically, you’ll need to have the ESP32 boards add-on installed in your Arduino IDE. With this crucial component in place, you’ll be able to harness the full power of the ESP32 microcontroller and bring your ideas to life in new and exciting ways. So, be sure to double-check that you have this add-on installed before we dive into the world of ESP32 programming together!

ESP32 WROOM 32D Pinout and Arduino IDE Board Manager Installation



ESP32 Control Digital Outputs

To begin controlling the GPIO of your ESP32 microcontroller, you’ll first need to set the desired pin as an OUTPUT. This can be easily accomplished using the pinMode() function, which allows you to specify the exact pin you wish to control. With this powerful function at your disposal, you’ll be able to manipulate your ESP32’s GPIO pins with ease, allowing you to experiment with a wide range of exciting physical computing projects. So why wait? Start exploring the possibilities of the pinMode() function

pinMode(GPIO, OUTPUT);

Controlling digital outputs with the ESP32 is a breeze with the digitalWrite() function, which takes two arguments – the GPIO pin number (int number) and the desired state, either HIGH or LOW. This simple yet powerful function enables you to easily manipulate digital outputs and create exciting physical computing projects with ease. Whether you’re looking to experiment with LED displays, motor control, or other innovative projects, the digitalWrite() function offers the control and precision needed to bring your ideas to life.

digitalWrite(GPIO, STATE);

When it comes to using the GPIOs on your ESP32 microcontroller, it’s important to note that almost all of them can be utilized as outputs, with the exception of GPIOs 6 to 11 (which are connected to the integrated SPI flash) and GPIOs 34, 35, 36, and 39, which can only be used as inputs. This means that you’ll have plenty of flexibility when it comes to controlling digital outputs, allowing you to experiment with a wide range of exciting physical computing projects.



ESP32 Read Digital Inputs

If you’re looking to read digital inputs, such as buttons or switches, with your ESP32 microcontroller, the process is remarkably straightforward. To begin, you’ll need to set the desired GPIO pin as an INPUT using the pinMode() function, as demonstrated in the following code snippet:

pinMode(GPIO, INPUT);

Once you’ve successfully configured the pin as an INPUT, you can begin reading data using the digitalRead() function, which takes the GPIO pin number as its argument.

digitalRead(GPIO);

With this simple yet powerful function at your disposal, you’ll be able to detect the state of buttons or other digital inputs with ease, providing you with the real-time data you need to power a wide range of physical computing projects.

It’s important to note that all of the GPIO pins on the ESP32 can be used as inputs, with the exception of GPIOs 6 to 11, which are connected to the integrated SPI flash. However, with the vast majority of pins available for your use, you’ll have plenty of flexibility when it comes to designing and implementing exciting new physical computing projects.

So whether you’re a seasoned microcontroller enthusiast or just getting started with physical computing, the ESP32 offers the power and flexibility needed to take your projects to the next level.



Pushbutton with Esp32:

In this tutorial, we will take you through a simple project example that demonstrates how to use digital inputs and outputs effectively. The project involves a pushbutton and an LED, and we will illustrate how to read the state of the pushbutton and light up the LED accordingly as you can see in the below figure.

By following this tutorial, you will gain an understanding of how digital inputs and outputs work and how to use them in practical applications. You will also learn how to interface with physical devices and use them to control digital electronics.

Through hands-on experimentation and step-by-step guidance, you will gain a deeper understanding of the concepts behind digital inputs and outputs, and be able to apply them in your own projects.

pushbutton states button pressed and button not pressed




Pushbutton with esp32 circuit diagram:

The circuit diagram for a pushbutton and an LED connected to an ESP32 microcontroller typically looks like this:

esp32 Digital Inputs and Digital Outputs pushbutton and an led circuit diagram

Connect one pin of the pushbutton to a digital input pin on the ESP32. In my case I am using GPIO5

Connect the other pin of the pushbutton to ground.

Connect the positive leg of the LED to a digital output pin on the ESP32 through a current limiting resistor. In my case I am using GPIO4

Connect the negative leg of the LED to ground.

When the pushbutton is pressed, it creates an electrical connection between the input pin and ground, causing the input pin to read a LOW signal. The ESP32 can detect this change in state, and in response, send a HIGH signal to the output pin connected to the LED, illuminating it. When the pushbutton is released, the input pin reads a HIGH signal, and the ESP32 turns off the LED by sending a LOW signal to the output pin.

The circuit is simple and easy to understand, making it an ideal starting point for anyone interested in learning about digital inputs and outputs using the ESP32 microcontroller. With this basic circuit in place, you can begin to experiment and create more complex projects by adding additional components and functionality.

In this article, I am using the esp32 development board.

esp32 development board for digital inputs and digital outputs

If you want the make the esp32 development board read my article home automation with esp32 wifi and Bluetooth



Simple Pushbutton Programming:

Programming Explanation:

This ESP32 code performs the following steps:

Two constant integer variables are defined: btnPin is set to 5, and ledPin is set to 4. These constants will be used to refer to the pins of the ESP32 microcontroller to which a button and an LED are connected.

const int btnPin = 5; 

const int ledPin =  4;

An integer variable btnState is defined and initialized to 0. This variable will be used to store the state of the button, whether it is pressed or released.

int btnState = 0;

The setup() function is defined. This function is executed once when the microcontroller is powered up or reset.

The first line in setup() initializes the serial communication with a baud rate of 115200. This enables the ESP32 to communicate with a computer or other device connected via USB.

Serial.begin(115200); 

The next line configures the btnPin as an input pin, which means that the ESP32 will read the voltage level of this pin to determine whether the button is pressed or released.

pinMode(btnPin, INPUT);

The following line configures the ledPin as an output pin, which means that the ESP32 will send a voltage to this pin to turn the LED on or off.

pinMode(ledPin, OUTPUT);

The loop() function is defined. This function is executed repeatedly as long as the microcontroller is powered on.

The first line in loop() reads the current state of the btnPin and stores it in the btnState variable. The digitalRead() function is used to determine whether the button is pressed or released.

btnState = digitalRead(btnPin);

The next line prints the value of btnState to the serial monitor, which allows the programmer to observe the state of the button in real time.

  Serial.println(btnState);

An if statement is used to determine whether the button is pressed or released. If btnState is HIGH (i.e., the button is pressed), the LED is turned on by setting the voltage on ledPin to HIGH using the digitalWrite() function. If btnState is LOW (i.e., the button is released), the LED is turned off by setting the voltage on ledPin to LOW.

  if (btnState == HIGH) {

    // turn LED on

    digitalWrite(ledPin, HIGH);

  } else {

    // turn LED off

    digitalWrite(ledPin, LOW);

  }

The loop() function then repeats from step 8, reading the state of the button and updating the state of the LED as necessary.



Testing:

now upload the code to the Esp32 microcontroller board, for this click on tools, in tool click on board, and select the esp32 Dev module.

select esp32 board Digital Inputs and Digital Outputs

Then click on the upload button

esp32 Digital Inputs and Digital Outputs program uploading

After uploading the code to your Esp32 microcontroller, test your circuit by pressing the pushbutton. As you can see I connected all the components as per the circuit diagram.

esp32 Digital Inputs and Digital Outputs controlling wiring

If everything is wired correctly and the code is functioning properly, the LED should light up when the pushbutton is pressed. This is because the code is set up to read the state of the pushbutton and turn on the LED when the pushbutton is pressed, and turn off the LED when the pushbutton is released.



Esp32 Toggle Switch programming:

This example will teach you how to connect a button to your ESP32 and toggle an LED by pressing the button. Whether you’re using a push button or a switch, the process for connecting them is very similar. We’ll be using the Arduino IDE to program the ESP32. I am using the same circuit diagram for this example

Esp32 toggle switch Circuit diagram:

esp32 Digital Inputs and Digital Outputs pushbutton and an led circuit diagram

Esp32 Toggle switch Programming:

Programming Explanation:

This code demonstrates how to toggle the state of an LED using a push button on an ESP32 microcontroller, with a delay of 1 second between button presses. Here’s a step-by-step explanation of the code:

Two integer constants are defined to specify the button and LED pin numbers.

const int btnPin = 5;  

const int ledPin =  4;    

Three integer variables are defined: btnState to hold the current state of the button, flagState to indicate whether the button has been pressed before or not, and led_state to hold the current state of the LED.

int btnState = 0;

int flagState = 0;

int led_state = LOW;

The setup function initializes the serial connection for debugging with brad rate 115200, and sets the pinMode of the button pin as INPUT and the LED pin as OUTPUT.

Serial.begin(115200);  

pinMode(btnPin, INPUT);

pinMode(ledPin, OUTPUT);

The loop function is called repeatedly after setup, and it starts by reading the state of the button using digitalRead, and storing it in the btnState variable.

btnState = digitalRead(btnPin);

If the btnState is HIGH (i.e., the button is pressed) and flagState is LOW (i.e., it hasn’t been pressed before), then the led_state is toggled using the NOT operator (!). This will cause the LED to turn on if it was off or turn off if it was on.

The new led_state is then written to the LED pin using digitalWrite to turn the LED on or off.

if (btnState == HIGH && flagState == LOW) {

    Serial.println(“The button is pressed”);

     led_state = !led_state;

    // control LED arccoding to the toggled state

    digitalWrite(ledPin, led_state); 

}

Finally, a delay of 1 second is used to slow down the loop, so that multiple button presses within a short time frame do not cause unintended behavior.

delay(1000);

This code provides a basic example of how to toggle an LED using a push button on an ESP32 and can be modified and expanded upon to meet specific project requirements.

Testing




Summary:

Congratulations, you have just embarked on an exciting journey of learning how to program the ESP32 microcontroller using the powerful and versatile Arduino IDE. With this getting started tutorial, you have acquired the foundational knowledge and skills needed to start creating your own digital projects using the ESP32. to learn how to use PWM pins in ESP32 then read this article.

By following this article, you have learned how to read digital inputs and control digital outputs with the ESP32 using the familiar Arduino programming language. You have also gained an understanding of how to connect various electronic components, such as pushbuttons and LEDs, to the ESP32 microcontroller using breadboards and jumper wires.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button