SunFounder Thales Kit for Raspberry Pi Pico, Release 1.0
Code
The LED will gradually become brighter as the program runs.
import
machine
import
utime
led
=
machine
.
PWM(machine
.
Pin(
15
))
led
.
freq(
1000
)
for
brightness
in
range
(
0
,
65535
,
50
):
led
.
duty_u16(brightness)
utime
.
sleep_ms(
10
)
led
.
duty_u16(
0
)
How it works?
Here, we change the brightness of the LED by changing the duty cycle of the GP15’s PWM output. Let’s take a look
at these lines.
import
machine
import
utime
led
=
machine
.
PWM(machine
.
Pin(
15
))
led
.
freq(
1000
)
for
brightness
in
range
(
0
,
65535
,
50
):
led
.
duty_u16(brightness)
utime
.
sleep_ms(
10
)
led
.
duty_u16(
0
)
•
led = machine.PWM(machine.Pin(15))
sets the GP15 pin as PWM output.
• The line
led.freq(1000)
is used to set the PWM frequency, here it is set to 1000Hz, which means 1ms
(1/1000) is a cycle. The PWM frequency can be adjusted, for example, the steering wheel needs to work at
50Hz, the passive buzzer can change the tone by changing the PWM frequency. However, there is no limit when
using LEDs alone, we set it to 1000Hz.
• The
led.duty_u16()
line is used to set the duty cycle, which is a 16-bit interger(2^16=65536). When we
assign a 0 to this function, the duty cycle is 0%, and each cycle has 0% of the time to output a high level, in
other words, turn off all pulses. When the value is 65535, the duty cycle is 100%, that is, the complete pulse
is turned on, and the result is equal to ‘1’ as a digital output. If it is 32768, it will turn on half a pulse, and the
brightness of the LED will be half of that when it is fully turned on.
3.4.9 Colorful Light
As we know, light can be superimposed. For example, mix blue light and green light give cyan light, red light and
green light give yellow light. This is called “The additive method of color mixing”.
•
Based on this method, we can use the three primary colors to mix the visible light of any color according to different
specific gravity. For example, orange can be produced by more red and less green.
In this chapter, we will use RGB LED to explore the mystery of additive color mixing!
78
Chapter 3. For MicroPython User
Содержание Thales Kit
Страница 1: ...SunFounder Thales Kit for Raspberry Pi Pico Release 1 0 Jimmy SunFounder Jun 04 2021 ...
Страница 2: ......
Страница 4: ...ii ...
Страница 6: ...SunFounder Thales Kit for Raspberry Pi Pico Release 1 0 2 CONTENTS ...
Страница 10: ...SunFounder Thales Kit for Raspberry Pi Pico Release 1 0 6 Chapter 1 Introduction to Raspberry Pi Pico ...
Страница 12: ...SunFounder Thales Kit for Raspberry Pi Pico Release 1 0 8 Chapter 2 What is Included in This Kit ...
Страница 13: ...SunFounder Thales Kit for Raspberry Pi Pico Release 1 0 2 1 Components List 2 1 Components List 9 ...
Страница 42: ...SunFounder Thales Kit for Raspberry Pi Pico Release 1 0 38 Chapter 2 What is Included in This Kit ...
Страница 140: ...SunFounder Thales Kit for Raspberry Pi Pico Release 1 0 136 Chapter 3 For MicroPython User ...
Страница 164: ...SunFounder Thales Kit for Raspberry Pi Pico Release 1 0 160 Chapter 4 For Arduino User ...