742 views|2 replies

75

Posts

1

Resources
The OP
 

[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

This post is from DigiKey Technology Zone

Latest reply

[attach]701533[/attach] Didn't it make a noise?   Details Published on 2023-5-30 21:06
 
 

6570

Posts

0

Resources
2
 

Didn't it make a noise?

This post is from DigiKey Technology Zone

Comments

There is a sound, but it seems not loud.   Details Published on 2023-5-30 21:41
 
 
 

75

Posts

1

Resources
3
 
Jacktang posted on 2023-5-30 21:06 Didn’t it make a sound?

There is a sound, but it seems not loud.

This post is from DigiKey Technology Zone
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list