Control LED Brightness Using a Potentiometer
You might have already played with an LED and gotten it to blink using the micro:bit.
In this guide, you will be introduced to the potentiometer and program it using MakeCode and on the Arduino IDE to change an LED's brightness.
Once you have finished this guide, you will know how to use potentiometers with the micro:bit. You will make an LED light dimmer with it! Potentiometer can be used for all sorts of projects, from volume control to MIDI, just to name a few examples.
|Parts Used in This Guide|
Step 1 The Module
Let's start with taking a closer look at the adjustable potentiometer module!
There are four pins here:
- Signal (SIG)
- No Connection (NC)
- Voltage Common Collector (VCC)
- Ground (GND)
Step 2 Connect module to breadboard
Step 3 Connect GND to GND
Step 4 Connect VCC to 3.3V
Step 5 Connect P0 to SIG
Step 6 Connect GND to LED (Negative lead)
Step 7 Add a resistor
Step 8 Connect P2 to LED
How it works
Now as for how this will work ...
We want to make it so that turning the potentiometer will change the brightness of the LED.
To do so, we need to get two values:
- the potentiometer's angle at a given time
- the brightness value based on the angle, at a given time
This is so that we can tell the micro:bit how bright the LED should be.
Step 9 On start
So, open up MakeCode editor and create a new project.
At the very start, we define two variables that will never change, "vcc" and "full angle". You might know that vcc stands for, "Voltage Common Collector" but what does 'full angle' refer to?
It refers to the full range of values of the potentiometer.
- We've connected the 'VCC' pin of the potentiometer to '3.3V' on the micro:bit. So we have set vcc to equal 3.3.
- We also know that the full range of values on the potentiometer is 300. So we have set full angle to equal 300.
Step 10 The Full MakeCode!
At the beginning of the 'forever' block, we create another variable called, 'sensorVal'. It is the analog value which is read from Pin 0 of the micro:bit. Remember, we connected the signal pin of the potentiometer to Pin 0 of the micro:bit! We want to give it a new value that shows how far the potentiometer has been turned.
- Because we want to get the angle of the potentiometer and its voltage at a given time, we need to make some calculations! As mentioned in the previous step, this is so that we can calculate a value for 'brightness'.
- We calculate the voltage at a given time by the equation: sensorVal x vcc / 1023
- Grab "set ... to ..." blocks from the 'Variables' tab
- Create new variables, "sensorVal" "voltage" ,"degrees", and "brightness". You can make a new variable by clicking on the 'Variables' tab
- Use the blocks under 'Math' tab to make the calculations.
- You can place blocks such as "0 x 0 " into another "0 x 0" block, by replacing the '0' with it
- Variable blocks can be added into the math blocks
- Next, we calculate the degrees of the potentiometer at a given time, using: voltage x full angle / vcc
- The range of the potentiometer is 0 to 300, so we need to convert this to 0 to 255 to control the brightness of the LED. To do this, we remap the values from the range 0 to 300, to the range 0 to 255. From 'Pins' tab, grab a "map from low from high to low to high' block.
- Finally, grab an 'analog write pin ... ' block:
- 'brightness' to Pin 2 (connected to positive leg of LED)
Step 11 Upload the code
Alright, now let's upload the hex file to the micro:bit.
- Connect the micro:bit to your computer using a microUSB cable
- Click on the Download button on the bottom left-hand corner of MakeCode editor
- Find the hex file
- Open up Finder on the MacOS or Explorer on Windows, and drag the hex file into MICROBIT under 'Devices' on the macOS.
- The micro:bit will flash for a few seconds while the code uploads