Button Pressed Buzzer Tone

#This is an example project to Control a buzzer with push button 


from machine import Pin, PWM # from machine module import Pin class

from utime import sleep # from utime module import sleep function

button = Pin(16,Pin.IN,Pin.PULL_UP)# At initial condition,Pin 16 will interally will go High or(1)

'''create a variable 'button' and declare the Pin umber and

assigned as an INPUT,Internaly Pulled UP (Connect push button in between GND and Pin#16)'''

buzzer = PWM(Pin(13))# Created a variable 'buzzer' and declared PWM Pin#13

while True:

    if  button.value() == 0:# if button pressed-Pin 16 will contact with GND,so value will go-(0)

        buzzer.freq(500)#Buzzer will start to beep according to the frequency and duty.

        buzzer.duty_u16(90000)

        sleep(0.5)

        buzzer.duty_u16(0)

        sleep(0.05)

        buzzer.duty_u16(1)

    else:

        buzzer.deinit()# if button not pressed,Buzzer will stop

Comments

Popular posts from this blog

4 Stage Timer With DS3231RTC Module.

Interfacing KY038 sound sensor with Raspberry pi Pico

Interfacing MQ2 Gas & Smoke Sensor With Raspberry Pi Pico