[Digi-Key Follow Me Issue 1] Task 2: Driving LEDs and Buzzers
[Copy link]
This task mainly includes three parts: driving LED lights, buzzers and OLED displays, but there are some problems with the OLED driver, so put it aside for now and sort it out later.
1. Driving LED
There is an LED light on the PICO W board. Please refer to the circuit diagram below for details. It is located on the left side of the USB port.
The LED is controlled through the GPIO port. We need to use the Pin package in the machine module to control it. Use Pin to create an LED object, and then control it by the level of the pin.
There are two ways to control the pin level, one is the on and off function, the other is the value function.
The details are as follows:
#点亮led
led.on()
#熄灭led
led.off()
#点亮led
led.value(1)
#熄灭led
led.value(0)
A time module is added here to control the light in a loop.
The Led.py file is as follows:
from machine import Pin
import time
#Led = Pin(25,Pin.OUT)
#Led = Pin("WL_GPIO0",Pin.OUT)
Led = Pin("LED",Pin.OUT)
#Led = Pin("GP7",Pin.OUT)
while True:
#Led.on()
Led.value(1)
print("LED灯亮")
time.sleep(0.3)
#Led.off()
Led.value(0)
print("LED灯灭")
time.sleep(0.3)
The effect is as follows:
led点亮
2. Buzzer driver
The buzzer driver needs to use PWM to drive, and the PWM pin is controlled by pin 16. The specific code is as follows.
# 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)
The effect is as follows:
驱动breez
|