RobotLabs
  • Welcome
  • ESP32
    • PlatformIO
      • 1.1 Arduino Basics
      • 1.2 Install PlatformIO
      • 2.1 GPIO - LED Blink
      • 2.2 GPIO - LED Multi
      • 2.3 GPIO - LED Button
      • 3.1 PWM - LED Fade
      • 3.2 ADC - Analog Input
      • 4.1 IIC - OLED
    • MicroPython
    • ESP32 Projects
  • ROS
    • ROS2 Jazzy
      • 1.1 Install ROS 2 Jazzy
      • 1.2 Install VS Code
      • 1.3 Save ROS code on GitHub
      • 2.1 Create a Mobile Robot with URDF and Visualize it
      • 2.3 Create Launch Files to Display the URDF in RViz
      • 2.4 Simulation with Gazebo
      • 2.5 Gazebo sensors
      • 2.6 Sensor fusion and SLAM
    • MoveIt2
      • Moveit 2 joint position control with keyboard
      • Moveit2 joint velocity control for Panda robot arm
    • Perception
      • PCL-ROS
  • FOC
    • SimpleFOC
      • Install simpleFOC Lib
  • Template
    • Markdown
    • Interactive blocks
    • OpenAPI
    • Integrations
Powered by GitBook
On this page
  • Introduction
  • Hardware Circuit
  • 1. Parts Required
  • 2. Wiring
  • Coding and Testing
  • 1. Code design
  • 2. How the Code Works
  • 3. Uploading the Code
  • Summary
  1. ESP32
  2. PlatformIO

2.3 GPIO - LED Button

Previous2.2 GPIO - LED MultiNext3.1 PWM - LED Fade

Last updated 3 months ago

In previous guide we learned how to use IO port output, and here we will use IO port as input. This example reads digital inputs like a button switch and control digital outputs like an LED using the ESP32-S3 with PlatformIO IDE.

Introduction

A push button is an electronic switch. You can turn the switch on by pressing the switch button. When you release your hand, the switch is turned off.

To show you how to use digital inputs and digital outputs, we’ll build a simple project example with a pushbutton and an LED. We’ll read the state of the pushbutton and light up the LED accordingly as illustrated in the following figure.

Please note that when pushing the button swith, there is vibration that is not felt by humans, but it is completely sensed by the microcontroller. Usually we will use software delay of 10ms for debouncing. For example, when a button is pressed, the pin is low at the first reading, and then read the pin level again after a delay of 10ms. If it is still at a low level, it proves that the button has been pressed.

What is push button debounce?

The switch used in this experiment is a mechanical elastic switch. When the mechanical contacts are opened and closed, due to the elastic effect of the mechanical contacts, a key switch will not be immediately and stably connected when closed, nor will it be disconnected all at once when disconnected. Therefore, there is a series of jitters at the moment of closing and disconnecting. The measure to prevent this phenomenon is key debounce.

Hardware Circuit

1. Parts Required

Here’s a list of the parts to you need to build the circuit:

  • ESP32-S3-N16R8

  • LED

  • 330 - 1k Ohm resistor

  • Pushbutton

  • Breadboard

  • Jumper wires

2. Wiring

Before proceeding, you need to assemble a circuit with an LED and a pushbutton. We’ll connect the LED to GPIO 12 and the pushbutton to GPIO 5.

Coding and Testing

Here we use the GPIO pin for input, so when we use the pinMode method, the second parameter cannot pass OUTPUT, and we need to use the digitalRead method to get the input value.

Unlike output, when setting input pins, we need to configure pull-up or pull-down resistors to determine the high or low level in the circuit. To do this, we use the pinMode function and the INPUT_PULLUP or INPUT_PULLDOWN constant. Such as you want to set a pin as a pull-up resistor, you need to call the pinMode(pin, INPUT_PULLUP) function, where pin is the pin number.

The pull-up resistors and pull-down resistors is to determine the initial level state of the circuit. If you choose a pull-up resistor, the GPIO pin defaults to a high level. If we want to change the signal, we need to pass a low level and connect to the ground. If you choose a pull-down resistor, the GPIO pin defaults to a low level. If we want to change the signal, we need to pass a high level and connect to the power supply.

1. Code design

Copy the following code to your PlatformIO IDE.

#include <Arduino.h>
// set pin numbers
const int buttonPin = 5;  // the number of the pushbutton pin
const int ledPin =  12;    // the number of the LED pin

// variable for pushbutton status and led logic
bool buttonState = false;
int led_logic = 0;

void setup() {
  Serial.begin(115200);  
  // initialize the pushbutton pin as an input
  pinMode(buttonPin, INPUT_PULLDOWN);
  // 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);
  }
}
// For Debouncing
void loop() {
  if (digitalRead(button_pin)) {
    delay(20);
    if (digitalRead(button_pin) && !buttonState) {
      led_logic = !led_logic;
      digitalWrite(led_pin, led_logic);    
      buttonState = !buttonState;
      }
    else if (!digitalRead(button_pin)) {
        buttonState = false;  
      }
    }
}

2. How the Code Works

In the following two lines, you create variables to assign pins:

const int buttonPin = 5;
const int ledPin = 12;

The button is connected to GPIO 5 and the LED is connected to GPIO 12. Next, you create a variable to hold the button state. By default, it’s 0 (not pressed).

bool buttonState = false;

In the setup(), you initialize the button as an INPUT, and the LED as an OUTPUT. For that, you use the pinMode() function that accepts the pin you are referring to, and the mode: INPUT or OUTPUT.

pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);

In the 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 buttonState variable. As we’ve seen previously, you use the digitalRead() function.

buttonState = digitalRead(buttonPin);

The following if statement, checks whether the button state is HIGH. If it is, it turns the LED on using the digitalWrite() function that accepts as argument the ledPin, and the state HIGH.

if (buttonState == HIGH) {
  digitalWrite(ledPin, HIGH);
}

If the button state is not HIGH, you set the LED off. Just set LOW as a second argument to in the digitalWrite() function.

else {
  digitalWrite(ledPin, LOW);
}

You can add debouncing as the following code.

3. Uploading the Code

Summary

In this section, you’ve learned how to read digital inputs and control digital outputs with the ESP32 using PlatformIO IDE.

Finally, if you want to learn more about other controllers, take a look at our Youtube channel:

Refer the previous tutorial:

2.1 GPIO - LED Blink
ESP32 button pressed or not to turn LED on and off
RobotLabsYouTube
Logo