ESP32 Starter Kit
6. Expansion Code
We will control the brightness of LED via a potentiometer. As we know, it is influenced by PWM. However, the range
of analog value is 0-4095 while that of PWM is 0-255. Thus, a “map(value, fromLow, fromHigh, toLow, toHigh)”
function is needed.
Wiring Diagram
1. Drag the two basic blocks.
2. Add a variable block and set it to local. Select “int” as its type and name it as “pot”.
3. Drag a “map” function from “Data” and put it to the assignment position. Set the value of “map” to “read the
value of pot IO33”, whose range is from (0,4095) to (0,255).
4. Finally add an “LED analogWrite” block. Set the pin to IO25 and analog value to the variable “pot”.
272 Chapter 9. Scratch Tutorial