Reading digital pin - UI error or user error?

Issue #25 resolved
Dominic Kelly created an issue

Hi there, I am using MicroBlocks in our primary school code club to make a frustration game like this one - https://youtu.be/faTJQ-RaARY .

Attached is the code that I have used to test this out before working on it with the kids. All is working fine when I connect up the wires/headphones. However, I have one query/issue (which may well be user error:))...

I want a line to show up on the microbit LED when the digital pin 0 is off (when the circuit is incomplete). I want the sound and the hits to show when the digital pin 0 is on (when the circuit is complete). However, the only way I can get the code to work is if I set the 'line' when the pin 0 is green (assuming this means 1) and hits/sound when pin 0 is red (or off).

Have I misunderstood the digital pin block or do you think there is a bug?

I hope this makes some sense :)

Cheers,

Dom

Comments (6)

  1. John Maloney repo owner

    Hi, Dominic. This sounds like a fun project, and a great way to build understanding of electrical circuits! You're using the BBC micro:bit, right? When the circuit is completed, is pin 0 connected to ground or 3.3v?

    The value you read from a digital input pin is actually not well specified unless you connect it through a resistor to either ground or 3.3 volts (often called "pull down" or "pull up" resistors.)

    If you're used to make circuits with components like LED's or motors, this seems surprising since, if the motor or LED is not connected to a power source, it is always off. But that's because those devices quickly drain any electrical energy from their input. Unless given a continuous supply of current from a battery, their input quickly goes to zero. In contrast, a digital input pin is designed to sense a voltage level while drawing off as little charge as possible. Thus, if the input pin has enough electrical charge to read "high", that charge hangs around and the input keeps reading high. If the charge is drained away, then the input keeps reading low. If you think about static electricity and how you can rub a balloon (in dry weather) to put a charge on it and then that charge stays on the balloon, you'll have a good picture of what's happening with an input pin.

    If you want an input pin to be "off" by default, you can connect the pin through a resistor to ground. The exact resistance value can be anywhere from 330 ohms to 1 megaohm. I'd suggest 1k ohm. Then you'd connect your frustration "wand" to 3.3 volts to complete the circuit and trigger the sound.

    A more common arrangement is to connect the input pin through a resistor to 3.3 volts so that it's "on" by default. Then, make the frustration wand connect to ground to complete the circuit and pull the pin low. You have to invert the tests in you program, of course, but with this arrangement there is less risk of short circuiting the power supply. Since there are current limiters in both the on-board voltage regulator and the USB power supply, short circuiting the power supply won't do anything too dramatic like making the board smoke, but a momentary short circuit will reset the microcontroller, and a short circuit lasting many seconds might permanently damage the voltage regulator. Having the long wire of your frustration game connected to ground rather than 3.3 volts makes an accidental short circuit less likely to happen.

    I've crammed a lot of electronics ideas into a few paragraphs, so let me know if you still have questions.

  2. John Maloney repo owner

    P.S. Most microcontroller boards have internal pull-up resistors that can be turned on in software. The BBC micro:bit is a bit unusual in also having software-configurable pull-down resistors. There's currently no MicroBlocks command to configure these internal pull-up/down resistors, but your question makes me think that it would be useful to add one, since it would save you from having to connect an external resistor.

  3. Dominic Kelly reporter

    Hi John,

    Many thanks (again) for the quick and detailed reply. Definitely a case of lack of knowledge on my part!

    Thanks for taking the time to explain why I need a resistor in the circuit for my project. I have tested it out and it all works as you explained. I think I need to take a few steps back to the basics of electronic circuits as it is certainly not an area with which I am particularly familiar.

    I was connecting pin 0 to GND btw.

    Kind regards,

    Dom

  4. John Maloney repo owner

    Hi, Dominic.

    We recently added the ability to have a digital input use an internal pull-up resistor, which will simplify the construction of things like your Frustration game. You can use it by expanding the block and setting the pullup option to “true”:

    Thanks for highlighting the need for that feature.

  5. Dominic Kelly reporter

    That's great - thx John!

    BTW, the servo and radio functions are both working well for me.

    Many thanks again for continuing to develop MicroBlocks.

    Cheers,

    Dom

  6. Log in to comment