Tone: Buzzer
This Python code demonstrates how to control a buzzer to make a sound. The Arduino main control board is connected to a Windows or Linux computer, and a buzzer module is connected to the D8 pin of the main control board.
# -*- coding: UTF-8 -*-
# Experiment effect: Control the buzzer to make a sound
# Connection: Connect an Arduino main control board to a Windows or Linux computer, and connect a buzzer module to D8 of the main control board
import time
from pinpong.board import Board,Pin,Tone # Import Tone class to control the buzzer
Board("uno").begin() # Initialization, select the board type (uno, leonardo, xugu) and port number. If no port number is entered, automatic recognition will be performed
#Board("uno","COM36").begin() # Initialization by specifying the port under Windows
#Board("uno","/dev/ttyACM0").begin() # Initialization by specifying the port under Linux
#Board("uno","/dev/cu.usbmodem14101").begin() # Initialization by specifying the port under Mac
tone = Tone(Pin(Pin.D8)) # Implement analog output by passing Pin into Tone
tone.freq(200) # Play according to the set frequency
'''
while True:
tone.tone(200,500) # Play according to the set frequency and time until completed
time.sleep(1)
'''
while True:
print("freq=",tone.freq()) # Read and print the frequency
tone.on() # Turn on the buzzer
time.sleep(1)
tone.off() # Turn off the buzzer
time.sleep(1)
tone.freq(tone.freq()+100) # Play according to the set frequency