SunFounder Thales Kit for Raspberry Pi Pico, Release 1.0
Code
• You can click the
Download Sketch
icon to download the code and open it with the desktop Arduino IDE.
• Or
OPEN IN WEB EDITOR
.
• Then upload the code to your Pico(
How it works?
Here, we connect the LED to the GPIO15, so we define a variable
ledPin
to represent GPIO15. You could also just
replace all the
ledPin
pins in the code with 15, but if you were to replace 15 with other pins you would have to
modify 15 one by one, which would add a lot of work.
#define ledPin 15
Now, you need to set the pin to OUTPUT mode in the
setup()
function.
pinMode
(ledPin,
OUTPUT
);
•
The above code has “set” the pin, but it will not light up the LED. Here, we use the
function to assign a
high level signal to
ledpin
, which will cause a voltage difference between the LED pins, causing the LED to light
up.
digitalWrite
(ledPin,
HIGH
);
If the level signal is changed to LOW, the ledPin’s signal will be returned to 0 V to turn LED off.
digitalWrite
(ledPin,
LOW
);
An interval between on and off is required to allow people to see the change, so we use a
delay(1000)
code to let
the controller do nothing for 1000 ms.
delay
(
1000
);
4.2.2 Fading LED
In last projects, we have used only two output signals: high level and low level (or called 1 & 0, ON & OFF), which
is called digital output.
However, in actual use, many devices do not simply ON/OFF to work, for example, adjusting the speed of the motor,
adjusting the brightness of the desk lamp, and so on. In the past, a slider that can adjust the resistance was used to
achieve this goal, but this is always unreliable and inefficient.
Therefore, Pulse width modulation (PWM) has emerged as a feasible solution to such complex problems. A digital
output composed of a high level and a low level is called a pulse. The pulse width of these pins can be adjusted by
changing the ON/OFF speed.
Simply put, when we are in a short period (such as 20ms, most people’s visual retention time), Let the LED turn on,
turn off, and turn on again, we won’t see it has been turned off, but the brightness of the light will be slightly weaker.
During this period, the more time the LED is turned on, the higher the brightness of the LED. In other words, in the
cycle, the wider the pulse, the greater the “electric signal strength” output by the microcontroller. This is how PWM
controls LED brightness (or motor speed).
•
Pulse-width modulation - Wikipedia
4.2. Projects
145
Summary of Contents for Thales Kit
Page 1: ...SunFounder Thales Kit for Raspberry Pi Pico Release 1 0 Jimmy SunFounder Jun 04 2021 ...
Page 2: ......
Page 4: ...ii ...
Page 6: ...SunFounder Thales Kit for Raspberry Pi Pico Release 1 0 2 CONTENTS ...
Page 140: ...SunFounder Thales Kit for Raspberry Pi Pico Release 1 0 136 Chapter 3 For MicroPython User ...
Page 164: ...SunFounder Thales Kit for Raspberry Pi Pico Release 1 0 160 Chapter 4 For Arduino User ...