[Digi-Key Follow me Issue 1] 002: Driving buzzers and OLEDs
[Copy link]
The previous post has the development environment. This article introduces driving two peripherals: buzzer and OLED. Referring to the documents and codes provided by the big guys, my experimental process is as follows.
First, connect the peripheral hardware as shown below: The OLED screen is connected to the I2C1 interface, and the buzzer is connected to GPIO16.
Figure 1: Peripheral wiring
1. Buzzer PWM drive.
According to the introduction of 3.8. PWM in raspberry-pi-pico-python-sdk.pdf, write the following code.
# Example using PWM to fade an BEEP.
import time
from machine import Pin, PWM
# Construct PWM object, with BEEP on Pin(16).
pwm = PWM(Pin(16))
# Set the PWM frequency.
pwm.freq(1000)
# Fade the BEEP in and out a few times.
duty = 0
direction = 1
for _ in range(4 * 256):
duty += direction
if duty > 255:
duty = 255
direction = -1
elif duty < 0:
duty = 0
direction = 1
pwm.duty_u16(duty * duty)
time.sleep(0.001)
Code 1, buzzer test
Run the code and you can hear the buzzer volume gradually changing.
2. OLED driver
At first, I referred to the articles of the big guys and wanted to search for the ssd1306 driver in Thonny's package manager and add it. Unfortunately, my network was not strong enough. I could search it but could not install it. I simply ignored the package manager and directly saved the driver file "ssd1306.py" to the PICO board memory, which worked just as well.
Write the test program as follows.
from machine import Pin,I2C
from ssd1306 import SSD1306_I2C
import time
i2c=machine.I2C(1, sda=machine.Pin("GP6"), scl=machine.Pin("GP7"), freq=400000)
display = SSD1306_I2C(128, 64, i2c)
display.fill(0)
display.show()
display.text("hello eeworld",5,10)
display.show()
time.sleep(1)
Code 2, OLED test program
Then you can see the result on the screen, as shown below.
Figure 2. OLED test
Since the codes have been prepared by all the big guys, the basic test program can be used easily.
3. Comprehensive application of buzzer and OLED display
The basic test program above confirms that there are no problems with the buzzer and OLED display. Now let's make a comprehensive application of these two peripherals. I want to display little stars on the screen and play them with the buzzer. While playing, the LED light flashes according to the beat.
The specific code is as follows.
from machine import Pin,I2C
from ssd1306 import SSD1306_I2C
from machine import Pin, PWM
import time
led = machine.Pin('LED', machine.Pin.OUT)
i2c=machine.I2C(1, sda=machine.Pin("GP6"), scl=machine.Pin("GP7"), freq=400000)
display = SSD1306_I2C(128, 64, i2c)
display.fill(0)
display.show()
display.text("hello eeworld",5,10)
display.show()
time.sleep(1)
strokes_lib = {
"小":[[[0.48, 0.05], [0.48, 0.98]], [[0.28, 0.96], [0.51, 0.96]], [[0.76, 0.32], [0.99, 0.78]], [[0.21, 0.38], [0.02, 0.76]]],
"星":[[[0.49, 0.49], [0.49, 1.0]], [[0.14, 0.08], [0.87, 0.08], [0.85, 0.45]], [[0.14, 0.25], [0.89, 0.25]], [[0.14, 0.08], [0.14, 0.435], [0.89, 0.42]], [[0.13, 0.62], [0.93, 0.62]], [[0.16, 0.77], [0.89, 0.77]], [[0.02, 0.97], [1.0, 0.97]], [[0.22, 0.54], [0.02, 0.74]]],
}
display.fill(0)
def draw_hz(hz, x0, y0, font_size=16):
for stroke in strokes_lib[hz]:
for i in range(1, len(stroke)):
x1 = x0 + round(stroke[i-1][0] * font_size)
y1 = y0 + round(stroke[i-1][1] * font_size)
x2 = x0 + round(stroke[i][0] * font_size)
y2 = y0 + round(stroke[i][1] * font_size)
display.line(x1, y1, x2, y2, 1)
font_size = 40
(x0, y0) = (0, 0)
for hz in "小星星":
draw_hz(hz, x0, y0, font_size)
x0 += font_size
display.show()
# Construct PWM object, with BEEP on Pin(16).
pwm = PWM(Pin(16))
# 定义音调频率
tones = {'1': 262, '2': 294, '3': 330, '4': 349, '5': 392, '6': 440, '7': 494, '-': 0}
# 定义小星星旋律
melody = "1155665-4433221-5544332-5544332-1155665-4433221"
for tone in melody:
freq = tones[tone]
if freq:
pwm.freq(freq) # 调整PWM的频率,使其发出指定的音调
pwm.duty_u16(10000)
led.on()
else:
pwm.duty_u16(0) # 空拍时一样不上电
led.off()
# 停顿一下 (四四拍每秒两个音,每个音节中间稍微停顿一下)
time.sleep_ms(400)
pwm.duty_u16(0) # 设备占空比为0,即不上电
led.off()
time.sleep_ms(100)
display.fill(0)
display.show()
Code 3, comprehensive application
The actual effect is shown in the following video.
003
Video 1: Comprehensive application
References:
https://shenlb.blog.csdn.net/article/details/121440994
https://blog.csdn.net/fatway/article/details/118859714
|