What you will make:
I assume you have already accomplished how to turn a LED on and off. Wouldn’t it be great if we are also able to control its brightness? Today we are going to do just that. We will use a potentiometer to control brightness, just like we use knob to control the speed of a ceiling fan.
What you will need:
For this project you will need:
- Connect the components as shown in schematic.
- Upload the code provided above and you are done. You can now use the potentiometer to control the brightness of LED.
Inside the forever() function, we are using the read analog pin() function to read values from the pin our potentiometer is attached to. The values we receive are from the range 0 to 1023, but the set pwm pin() can only output values from 0 to 255. Luckily mBlock software can translate one range of values to the other, in our case translating the range 0 – 1023 to 0 – 255. Finally we are using the set pwm pin() function to write a variable output value to the pin LED is attached to. Note that the set pwm pin() function will only work on the pins which are marked with the tilda (~) sign on the Arduino board, known as the PWM pins.
For now you just need to know that these pins can output variable values, other than HIGH and LOW values that you are well familiar with.