Twiddly Dials and Dimming LEDs

Just a quick one here – I was going to go on to making tanks and buggies and cool stuff like that, but I decided I’d better explain a little bit about these Things first…

Thrilling, huh? Useful though, and we’ll be using them a lot although they won’t always look like that. That thing is a potentiometer, also known as a pot, a variable resistor, or a twiddly dial thing if you like. Whatever you call it, it’s job is to basically let you adjust how much voltage goes to something by turning the knob on it. Light dimmer switches use them, and little versions of them are what let the thumbsticks on joypads do analog stuff in games rather than just what you get on the direction pad.

So, let’s do a quick project so you can see how they work!

Dimming Micro:Bit LEDs

Very simple this. What we’re going to do is connect a potentiometer to a Micro:Bit, then use it to make an icon on the LEDs brighter and dimmer.

What you will need

1 x Microbit

1 x 10K Ohm Potentiometer (You can get a bag of 5 of them here for less than £3, or pick them up for about 60p each)

3 wires with crocodile clip (This sort of thing)

Putting it together

Nice and easy this bit. Using your crocodile clip jumper wires, connect the

  • 3V terminal of your Micro:Bit to the left potentiometer pin
  • GND terminal of your Micro:Bit to the right potentiometer pin
  • the 0 terminal of your Micro:Bit to the center potentiometer pin

Make sure that none of the crocodile clips are touching eachother!

It’s traditional to use red wires for positive connedtions (3V in this case) and black for negative (GND), and it’s worth getting into the habit of sticking to that. Your should end up with something like this. (right-click an open it in another tab if you need a clearer view)

Making it do Stuff

Just a little bit of coding to do here, using the JavaScript Blocks Editor

First up, we need to set up one variable, called dial

Then put together the following bit of code (you can use whichever icon you like best)

(If you’re having trouble making the second bit of the Set Brightness block, what you need to do is drag a divide block from the maths section, drag that into the brightness block, drag dial from variables to the right position, then change the 2nd number to a 4.)

Once you’ve put that together, transfer it to your Micro:Bit, twist the knob on your potentiometer so it’s about halfway round, and once it powers up you’re ready to go!

You should now see the icon you chose on your Micro:Bit’s LED display. Now try turning the potentiometer left and right – as you do that you should notice the LED lights on your Micro:Bit get brighter and dimmer.

The explanation

So what’s going on then? Well, it’s pretty simple really. When you tell your Micro:Bit to show things on its LEDs it usually does it at maximum brightness, but you can actually set it to be any level of brightness between off and on. In the Block Editor we use the Set Brightness block found in the LED section to do this (it’s actually in the more… part of the LED blocks if you cant find it). To set the brightness you change its variable to anything from 0 (off) up to 255 (full brightness) – so 128 would be about half brightness for example.

Now, the way we’ve set things up, the Micro:Bit finds out what the potentiometer is set to by using Analog read pin P0, and it sets the dial variable to whatever that happens to be. If you remember from earlier posts an analog value for the pins on a Micro:Bit is usually a number between 0 and 1023. But that means that that if you twist the potentiometer all the way round, the dial variable will be 4 times bigger than the LEDs maximum brigtness value (255, remember?) So what we do is add in that little maths block to the set brightness block, which tells the Micro:bit to set the brightness to the dial value divided by 4.

Basically the Forever loop really says “Read an analog value from the potentiometer on Pin0, and call that number Dial. Then set the brightness of the LEDs to the Dial number divided by 4″

Wrapping Up

So there you have it – that’s how we can use potentiometers as an analog Input device. This means that instead of having buttons that are either on or off (like the ones on the face of the Micro:Bit) we can have much more control over things like lights, or motors. Or you could use potentiometers to make a musical instrument where they change the volume and the pitch of note being played. Loads of stuff really – this little project is just the very start of a bagful of new possibilities being opened up. I used two of them to make a remote control for a tank, for example.


April 6th, 2017 by