Skip to content

Latest commit

 

History

History
125 lines (91 loc) · 2.76 KB

potentiometer-example-microbit.md

File metadata and controls

125 lines (91 loc) · 2.76 KB
title layout meta-description share author about cats simple-description date date-updated
Potentiometer and the Microbit
text-width-sidebar
Examples of using the potentiometer and the microbit.
true
jez
Examples of using a Potentiometer with the microbit.
component examples
Potentiometer Examples
2016-12-23 10:20:00 UTC
2016-12-23 10:20:00 UTC

All examples use a simple 10k potentiometer.

See the component article on this website about the LED.

{:.ui .header .dividing}

Dimming LED

Dim and LED based on the reading/rotation of the potentiometer.

Pot always pin 1

Hardware Required

  • LED
  • 220Ω resistor
  • 10kΩ potentiometer

{:.ui .image} diagram for blinking LED

Python PXT.io
{% highlight python %} from microbit import *

pot = pin1 led = pin0

while True: led.write_analog(pot.read_analog()) sleep(100) {% endhighlight %}

This should work a lot better than it does.

The LED flickers each time write_analog() is called. Hopefully someone has a solution.

{% highlight javascript %} let potReading = 0

basic.forever(() => { potReading = pins.analogReadPin(AnalogPin.P1) pins.analogWritePin(AnalogPin.P0, potReading) })

{% endhighlight %}

{:.ui .header .dividing}

RGB Colour Mixing

Mix colours based on the reading from three potentiometers.

Hardware Required

  • RGB LED
  • 3 × 220Ω resistors
  • 3 × 10kΩ potentiometer
  • microbit breakout board

{:.ui .image} colour mixing diagram circuit

{% highlight python %} from microbit import *

potentiometer pins

red_pot = pin0 green_pot = pin1 blue_pot = pin2

led pins

red_led = pin10 green_led = pin3 blue_led = pin4

while True: # get colors from pots # returned values range from 0 - 1023 red = red_pot.analog_read() green = green_pot.analog_read() blue = blue_pot.analog_read()

# write colours to LEDs
red_led.analog_write(red)
green_led.analog_write(green)
red_led.analog_write(blue)

sleep(100)

{% endhighlight %}