Working Example : Pi Pico in ESPHome with Piezo Buzzer

Hi All,

Wanted to share a Piezo sensor I created today which you can switch on/off using a Pi Pico W. A simple setup that I scoured the internet for help with but didn’t see much in the way of forums/tutorials for the exact scenario.

image

Bearded Tinkerer on Youtube got me started off on the right foot by installing the “ESPHome(Dev)” add-on: https://www.youtube.com/watch?v=-nJsHa-pTFw

Then the code I used on the Pico itself (installed using Thonny):

import machine
import utime

# Import the necessary modules for ESP8266 communication
from machine import UART
import network

# Define the GPIO pin connected to the piezo buzzer
buzzer_pin = machine.Pin(14)
pwm = machine.PWM(buzzer_pin)

# Function to generate a siren cadence
def siren():
    print("Siren function started")
    while True:
        print("Siren sound loop started")
        for frequency in range(100, 1000, 10):  # Increase frequency
            pwm.freq(frequency)
            utime.sleep_ms(10)
        for frequency in range(1000, 100, -10):  # Decrease frequency
            pwm.freq(frequency)
            utime.sleep_ms(10)
        utime.sleep(1)  # Pause for 1 second between cadences

# Function to configure and connect to Wi-Fi
def connect_to_wifi(ssid, password):
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    if not wlan.isconnected():
        print('Connecting to WiFi...')
        wlan.connect(ssid, password)
        while not wlan.isconnected():
            pass
    print('Connected to WiFi:', ssid)
    print('IP Address:', wlan.ifconfig()[0])

# Call the function to connect to Wi-Fi
connect_to_wifi("YOUR_WIFI_SSID", "YOUR_WIFI_PASSWORD")

# Call the siren function to start the siren sound
siren()

and then following the youtube steps, here is the working code i had for the piezo buzzer and switch:

working esphome dev code:

esphome:
  name: pi-pico
  friendly_name: pi pico

rp2040:
  board: rpipicow
  framework:
    # Required until https://github.com/platformio/platform-raspberrypi/pull/36 is merged
    platform_version: https://github.com/maxgerhardt/platform-raspberrypi.git

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "DTxN/fHjSpMgbCWmmjDAE311qMsvh6IFo4g0gM8/QJ4="

ota:
  password: "e8cf0230113196b45ed1778f0e279df0"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  manual_ip:
    static_ip: 192.168.1.15
    gateway: 192.168.1.1
    subnet: 255.255.255.0
  # Enable fallback hotspot in case wifi connection fails
  ap:
    ssid: "Pi-Pico Fallback Hotspot"
    password: "rTVq29iVdIgH"

# Define buzzer output and switch on GPIO14
output:
  - platform: gpio
    pin: GPIO14
    id: piezo_buzzer
    inverted: false  # Invert the logic so GPIO14 is OFF by default

switch:
  - platform: output
    name: "Buzzer Switch"
    id: buzzer_switch
    output: 'piezo_buzzer'

# Toggle buzzer based on switch state

I have the piezo wired to GPIO14 and GND. Upon powering up the pico, the piezo buzzer is off by default. When i toggle the switch in home assistant it turns the piezo on. I’ll call this functionality in automation when a door sensor is opened.