-3

I'm working on a buzzer that has to make a simple sound. But I can't figure out about how to make this buzzer work in MicroPython. I've already tried the following code but it is not doing much. I'm a little bit stuck at this.

import pycom
import machine
import time
from machine import Pin
import board
import pulseio

buzzer = pulseio.PWMOut(board.D16, variable_frequency=True)

def main():
   buzzer.duty_cycle = ON
   buzzer.frequency = 440
   buzzer.duty_cycle = OFF
   
if __name__ == "__main__":
   main()

For more information about what I use as materials:

Buzzer:https://datasheet4u.com/datasheet-pdf/Ningbo/KPT-1410/pdf.php?id=868269

The materials for my project is this:

I think i'm getting close to it, but can't figure it out.

Zino
  • 1
  • 4
  • "Does someone know how to convert it to MicroPython?" isn't really what we do here. If you make a valiant attempt to convert it to MicroPython and you get stuck, then this is the place to get help. – nicomp Jan 15 '21 at 13:26
  • Sorry I will post my attempt in the topic – Zino Jan 15 '21 at 13:29
  • You find a good resource in https://learn.adafruit.com/circuitpython-essentials/circuitpython-pwm – Nour-Allah Hussein Jan 15 '21 at 13:42

1 Answers1

2

here is similar function to map function that you have used in arduino

def remap(value, leftMin, leftMax, rightMin, rightMax):
    # Figure out how 'wide' each range is
    leftSpan = leftMax - leftMin
    rightSpan = rightMax - rightMin
    # Convert the left range into a 0-1 range (float)
    valueScaled = float(value - leftMin) / float(leftSpan)
    # Convert the 0-1 range into a value in the right range.
    return rightMin + (valueScaled * rightSpan)

for example, if your ADC values are between 2000 and 1000 (on purpose i show that higher ADC value mean lower actual value) you can use function above with following call to get percentage value:

percent_value = remap(
    adc_pin.read(),
    1000, 2200,     # these values comes from experiment above when wet and dry
    100, 1
)

and look at the samples in documentation how to make buzz

import array
import pulseio
import pwmio
import board

# 50% duty cycle at 38kHz.
pwm = pwmio.PWMOut(board.D13, frequency=38000, duty_cycle=32768)
pulse = pulseio.PulseOut(pwm)
#                             on   off     on    off    on
pulses = array.array('H', [65000, 1000, 65000, 65000, 1000])
pulse.send(pulses)

# Modify the array of pulses.
pulses[0] = 200
pulse.send(pulses)
Lixas
  • 6,938
  • 2
  • 25
  • 42