How to Make a Buzzing Sound in Raspberry pi

You want to make a buzzing sound with the Raspberry Pi. Today I show you how to Make a Buzzing Sound in Raspberry pi

Required Component  :

1.Raspberry pi
 
 
 
 

This book will help you to gain more knowledge of Raspberry pi  Software and Hardware Problems and Solutions

Raspberry Pi Cookbook

Circuit diagram:

shows how you can wire this Buzzer using a solderless breadboard and male-to-female jumper leads 
Connecting a piezo buzzer to a Raspberry Pi
Connecting a piezo buzzer to a Raspberry Pi4

Code :

import RPi.GPIO as GPIO
import time
buzzer_pin = 12
GPIO.setmode(GPIO.BCM)
GPIO.setup(buzzer_pin, GPIO.OUT)
def buzz(pitch, duration):
 period = 1.0 / pitch
 delay = period / 2
 cycles = int(duration * pitch)
 for i in range(cycles):
 GPIO.output(buzzer_pin, True)
 time.sleep(delay)
 GPIO.output(buzzer_pin, False)
 time.sleep(delay)
while True:
 pitch_s = raw_input("Enter Pitch (200 to 2000): ")
 pitch = float(pitch_s)
 duration_s = raw_input("Enter Duration (seconds): ")
 duration = float(duration_s)
 buzz(pitch, duration)
Piezo buzzers don’t have a wide range of frequencies, nor is the sound quality remotely good. However, you can vary the pitch a little. The frequency generated by the code is very approximate. The program works by simply toggling the GPIO pin 12 on and off with a short delay in between. The delay is calculated from the pitch. The higher the pitch (frequency), the shorter the delay needs to be.

If you want to know more about raspberry pi then click on the link below

RASPBERRY PI TUTORIALS FOR BEGINNERS

No comments

Theme images by Dizzo. Powered by Blogger.