title | layout | meta-description | share | author | about | cats | simple-description | acknowledgements | date | date-updated |
---|---|---|---|---|---|---|---|---|---|---|
Using a Rotary Potentiometer with a Microbit |
text-width-sidebar |
How to use use a rotary potentiometer and the microbit. Use a pot to fade an LED or alter the pitch of a note. |
true |
jez |
Use a potentiometer to provide an analog input to the microbit. |
external |
Potentiometer |
potentiometer image by budgetronics.eu (CC-SA 2.0) |
2016-12-23 10:20:00 UTC |
2016-12-23 10:20:00 UTC |
Potentiometers are a type of resistor. The incoming voltage is divided between the middle and the GND pin. How this is divided depends on the rotation of the knob.
We can read the value of the middle pin (or wiper) with .analog_read()
. The value it returns tells us about the rotation of the shaft.
{:.ui .dividing .header}
The outer pins are connected to 3V and GND. The the middle pin is connected to the microbit.
This is a 10k potentiometer; you can use others without problems.
{:.ui .dividing .header}
Show the reading of the potentiometer. The reading of pin0.read_analog() will range from 0 - 1023 depending on the rotation of the knob.
{% highlight python %} from microbit import *
while True: ## pot_reading variable is the reading from pin0 pot_reading = pin0.read_analog()
display.scroll(str(pot_reading))
sleep(10)
{% endhighlight %}
Show the reading of the potentiometer as a percent
{% highlight python %} from microbit import *
while True: # Calculate percent # read_analog() outputs 0 (0%) to 1023 (100%) # Reading of 1023 is 3.3v pot_percent = (pin0.read_analog() / 1023) * 100
display.scroll(str(pot_percent))
sleep(10)
{% endhighlight %}
Control the brightness of the pixel in the centre of the display (2,2) {% highlight python %} from microbit import *
while True: # set_pixel brightness if between 0 - 9 brightness = int((pin0.read_analog() / 1023) * 9)
display.set_pixel(2, 2, brightness)
sleep(10)
{% endhighlight %}
When button a is pressed show the reading of pin 0
Rotate the knob to fill the microbit display with lit pixels. Note the potentiometer is connected to PIN1 in this example. There's no good reason for it; it's a mistake.
- Dim an external LED
- Control the pitch of music
- Control the speed of a scroll