-1

I have made a circuit which should make sound using a passive resistor. The passive resistor has resonant frequency of 2KHz.

The textbook I am following has this code which I can't understand, specifically the bit in which the frequency is calculated using sin function. How does the alertor function work in the code below

import RPi.GPIO as GPIO
import time
import math

buzzerPin = 11    # define the buzzerPin
buttonPin = 12    # define the buttonPin

def setup():
    global p
    GPIO.setmode(GPIO.BOARD)         # Use PHYSICAL GPIO Numbering
    GPIO.setup(buzzerPin, GPIO.OUT)   # set RGBLED pins to OUTPUT mode
    GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)    # Set buttonPin to INPUT mode, and pull up to HIGH level, 3.3V
    p = GPIO.PWM(buzzerPin, 1)
    p.start(0);

def loop():
    while True:
        if GPIO.input(buttonPin)==GPIO.LOW:
            alertor()
            print ('alertor turned on >>> ')
        else :
            stopAlertor()
            print ('alertor turned off <<<')
def alertor(): <-- what is the purpose of for loop and sinVal to calculate new frequency?
    p.start(50)
    for x in range(0,361):      # Make frequency of the alertor consistent with the sine wave
        sinVal = math.sin(x * (math.pi / 180.0))        # calculate the sine value
        toneVal = 2000 + sinVal * 500   # Add to the resonant frequency with a Weighted
        p.ChangeFrequency(toneVal)      # Change Frequency of PWM to toneVal
        time.sleep(0.001)

def stopAlertor():
    p.stop()

def destroy():
    GPIO.output(buzzerPin, GPIO.LOW)     # Turn off buzzer
    p.stop()  # stop PWM
    GPIO.cleanup()                       # Release GPIO resource

if __name__ == '__main__':     # Program entrance
    print ('Program is starting...')
    setup()
    try:
        loop()
    except KeyboardInterrupt:  # Press ctrl-c to end the program.
        destroy()
2
  • what is the logic behind for using sin function in alertor method. Commented Dec 27, 2021 at 19:52
  • removed the code which wasn't needed Commented Dec 27, 2021 at 19:53

1 Answer 1

0

I wasn't clear about the question. I couldn't understand these code lines specifically

 for x in range(0,361):      # Make frequency of the alertor consistent with the sine wave
        sinVal = math.sin(x * (math.pi / 180.0))        # calculate the sine value
        toneVal = 2000 + sinVal * 500   # Add to the resonant frequency with a Weighted
        p.ChangeFrequency(toneVal)      # Change Frequency of PWM to toneVal
        time.sleep(0.001)

I get it now. The sin function takes value in radians, not degrees. The logic in math.sin converts degrees (from 0 to 360) into radians.

the next line adds sin's value plus 500 to resonant frequency of the passive buzzer. As sin's value goes from some positive max to equal negative max, the logic adds/subtracts an amount from resonant frequency, giving a siren like sound

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.