Push button with Arduino on ESP32-C3

Updated on 7 November 2023
dev board ESP32-C3-DevKitM-1
chip ESP32-C3-MINI-1-N4
features push button press esp32c3
This tutorial is more than 6 months old. If the steps below do not work, then please check the latest versions and the documentations of the individual tools used.

Before starting

Dependancies

Ensure the following dependancies are downloaded and available:

Pre-requisites

Try these simpler or similar examples:

Buy the components

Code

Download code push-button-arduino-esp32c3.ino
const int buttonPin = 4;  // Define the pin connected to the button
bool buttonState = 0;  // Variable to store the button state

void setup() {
  // Set the button pin as input with pull-up
  pinMode(buttonPin, INPUT_PULLUP);
  Serial.begin(115200);
}

void loop() {
  buttonState = digitalRead(buttonPin);
  if (buttonState == LOW) {
    Serial.println("Button pressed!");
    delay(1000);
  }
}

Makefile

BOARD?=esp32:esp32:esp32c3
PORT?=/dev/cu.usbserial-*
BUILD=build

.PHONY: default lint compile upload clean

default: lint compile upload clean

lint:
	cpplint --extensions=ino --filter=-legal/copyright *.ino

compile:
	arduino-cli compile --fqbn $(BOARD) --output-dir $(BUILD) ./

upload:
	arduino-cli upload --fqbn $(BOARD) --port $(PORT) --input-dir $(BUILD)

clean:
	rm -r build

Prototype

A photo of the actual setup.

Push button with Arduino on ESP32-C3 prototype

Schematic

Wire up the hardware accordingly

Push button with Arduino on ESP32-C3 schematic

Serial console

Serial output from the firmware.

Push button with Arduino on ESP32-C3 serial console

Description

Press the button and see the serial console as Button pressed!.

Compile and upload the firmware via the Arduino IDE or arduino-cli with Makefile.

References