Micropython how to play buzzer sound longer?

I’m almost done with a project but when I give a value that is to high. I barely hear de buzzer playing because it only plays for a sec. Does someone know how to play the buzzer sound longer?

import pycom
import machine
import time
from machine import Pin
import socket
from network import LoRa
import binascii
import array
from machine import PWM

############### Weightsensor ###############
def main():
    while True:
        adc = machine.ADC()             # create an ADC object
        apin = adc.channel(pin='P16')   # create an analog pin on P16
        val = apin()                    # read an analog value

        if val < 20:
            print(val)
            print("Weight is good")
            binaryString = bin(val)
            print(binaryString)
            time.sleep(5)
        if val > 20:
            print(val)
            print("Weight is to high")
            binaryString = bin(val)
            print(binaryString)
            # 50% duty cycle at 38kHz.
            pwm = PWM(3, frequency=78000)  # use PWM timer 0, with a frequency of 5KHz
            # create pwm channel on pin P12 with a duty cycle of 50%
            pwm_c = pwm.channel(0, pin='P20', duty_cycle=1.0)
            pwm_c.duty_cycle(0.3) # change the duty cycle to 30%
            time.sleep(5)


if __name__ == "__main__":
    main()

The buzzer plays in the second “if” statement.

Can someone help me out please?

Kind regards

Answer

I see multiple issues in your code: Why do you redefine again and again adc and apin in your while loop? move it before while True

Duty in your PWN should not influence pitch of your buzzer, so my advice would be omit all duty settings, or at least leave it to like 250 (experiment with it)

Pitch of buzzer is frequency value in your PWM declaration. You can change it with pwm.frequency(hertz) somewhere later. Or find best working setup in console by manualy entering commands.

How long will it sound is based on time.sleep()

In my opinion code should be something like that (have not tested on board):

import pycom
import machine
import time
from machine import Pin
import socket
from network import LoRa
import binascii
import array
from machine import PWM

############### Weightsensor ###############
def main():
    adc = machine.ADC()             # create an ADC object
    apin = adc.channel(pin='P16')   # create an analog pin on P16
    # 50% duty cycle at 38kHz.
    pwm = PWM(3, frequency=78000)  # use PWM timer 0, with a frequency of 5KHz
    # create pwm channel on pin P12 with a duty cycle of 50%
    pwm_c = pwm.channel(0, pin='P20')
    
    while True:

        val = apin()                    # read an analog value

        if val < 20:
            print(val)
            print("Weight is good")
            binaryString = bin(val)
            print(binaryString)
            time.sleep(5)
        if val > 20:
            print(val)
            print("Weight is to high")
            binaryString = bin(val)
            print(binaryString)
            
            pwm_c.freq(500)
            time.sleep(5)
            pwm_c.freq(0)


if __name__ == "__main__":
    main()