Press the buzzer 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 buzzer esp32c3
This tutorial is more than 1 year 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 press-buzzer-arduino-esp32c3.ino
const int buttonPin = 4;
const int buzzerPin = 5;
bool buttonState = 0;

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(buzzerPin, OUTPUT);
  digitalWrite(buzzerPin, LOW);
  Serial.begin(115200);
}

void loop() {
  buttonState = digitalRead(buttonPin);
  if (buttonState == LOW) {
    Serial.println("Button pressed!");
    digitalWrite(buzzerPin, HIGH);  // Buzzer sound is on
    delay(1000);
    digitalWrite(buzzerPin, LOW);  // Buzzer sound is off
    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: clean
	arduino-cli compile --fqbn $(BOARD) --output-dir $(BUILD) ./

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

clean:
	rm -rf build

Prototype

A photo of the actual setup.

Press the buzzer with Arduino on ESP32-C3 prototype

Schematic

Wire up the hardware accordingly

Press the buzzer with Arduino on ESP32-C3 schematic

Serial console

Serial output from the firmware.

Press the buzzer with Arduino on ESP32-C3 serial console

Description

Press the button and hear the buzzer sound for 1 second.

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

References