Use PWM ("Pulse Width Modulation") to control the brightness level of the LED.
Important:
Connect the LED to D3 using a Grove cable
Like this:
Create the Codecraft sketch ➡
Create two variables: pot, brightness
Use the map block to map the pot variable (values range from 0 to 1023) to the LED brightness variable (values range from 0 to 255)
The "LED pin set to" block is in the Grove Analog menu, which sets the LED to a brightness value .
(Note it is different from the Grove Digital "LED pin State" block, which can be set to ON and OFF.
Click the "Connect" button and select the correct device for your kit.
Turn the rotary potentiometer. Is the LED brightness changing?
Then, modify the Arduino sketch to do something different, such as:
Reverse the relationship between the potentiometer and the brightness value.
Use a Count block to fade the LED brightness up (or down, or both). Start the fade when you press the button.
Example: LED Dimmer PWM Up and Down.cdc
Use the "Pick random" block to display random brightness levels.
Your ideas?
The LED is internally connected, on the printed circuit board, to pin D4. However, it's not possible to use the "LED Pin ... set to" block with D3. That is because only some of the pins on the microcontroller are capable of PWM. For this exercise, we used a Grove cable to connect the LED to pin 4 (while it is also connected to pin 3. That's ok...)
Look carefully at the labels next to the pins on upper left of the microcontroller. The pins with a tilde (~) after the number are PWM outputs.
3 not PWM
4~ PWM