SunFounder Thales Kit for Raspberry Pi Pico, Release 1.0
How it works?
To enable Serial Monitor, you need to start serial communication in
setup()
and set the datarate to 9600.
void
setup
() {
pinMode
(ledPin,
OUTPUT
);
Serial
.
begin
(
9600
);
}
•
In the loop function, the value of the potentiometer is read, then the value is mapped from 0-1023 to 0-255 and finally
the value after the mapping is used to control the brightness of the LED.
void
loop
() {
int
sensorValue
=
analogRead
(sensorPin);
Serial
.
println
(sensorValue);
int
brightness
=
map
(sensorValue,
0
,
1023
,
0
,
255
);
analogWrite
(ledPin, brightness);
}
•
is used to read the value of the sensorPin (potentiometer) and assigns it to the variable
sensorValue
.
int
sensorValue
=
analogRead
(sensorPin);
• Print the value of SensorValue in Serial Monitor.
Serial
.
println
(sensorValue);
• Here, the
map(value, fromLow, fromHigh, toLow, toHigh)
function is required as the potentiometer value read
is in the range 0-1023 and the value of a PWM pin is in the range 0-255. It is used to Re-maps a number from
one range to another. That is, a value of fromLow would get mapped to toLow, a value of fromHigh to toHigh,
values in-between to values in-between, etc.
int
brightness
=
map
(sensorValue,
0
,
1023
,
0
,
255
);
• Now we can use this value to control the brightness of the LED.
analogWrite
(ledPin,brightness);
4.2.5 Measure Light Intensity
A photoresistor or photocell is a light-controlled variable resistor. The resistance of a photo resistor decreases with
increasing incident light intensity; in other words, it exhibits photo conductivity.
Therefore, we can use a photoresistor to measure light intensity, and then show it through 5 LEDs.
154
Chapter 4. For Arduino User
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 ...