9 years, 8 months ago.

Pulldown or current limiting resistor needed?

Hello, this extremely simple mBuino project uses only one extra component: a switch between P0.4 and GND. Using a falling interrupt, triggering of the switch is used to switch from Demo to Roll mode and to roll the dice. I tested this using both a simple push-button as well as a tilt-switch, which is of course way more fun.

However, this being my first mBuino project, I'm not sure whether the just a switch is sufficient. Is it safe to use without a resistor? Should a current limiting pull down resistor be included in the circuit, or is the internal pull-up resistor used by default and sufficient?

Also as this is my first mbed program, I'm not entirely sure I used the mbed API correctly. I you have time to review my code, all comments to improve the quality are appreciated. I now that the code is still lacking some comments, and as the mBuino API is not available yet, I choose a compatible platform to compile the code.

Thanks in advance,

Marcel

Question relating to:

Electronic dice application for the mBuino platform. Notes: The mBuino starts in Demo mode. In Demo mode the LEDs are lighted, showing sweeps and demo rolls. Connect a button or … BUTTON, interrupt, light show, mBuino, tilt-switch

One minor tweak to your program. Using an analog in to seed the random number generator is a good start but is going to give you similar values most of the time, only the few least significant bits will be changing. It's probably good enough that you won't spot a pattern but why not be sure. Rather than using a single analog in reading as the seed use the least significant bits of 32 reads joined together.

Or even better you have a truly random factor available to you, the person pressing the button. While sitting idle waiting for a key press keep generating random numbers and throwing them away. Or run a timer, take the time between presses in us modulo 100, generate that number of random numbers before displaying the result. This way even if your seed value was exactly the same every time your results will still be unpredictable.

posted by Andy A 02 Sep 2014

Great ideas, thank you! I'm working on an updated version now, which uses the microseconds of the current time (which moment depends on the user action) in addition to the analog-in noise to re-seed the generator prior to each throw of the dice.

posted by Maxint R&D 08 Sep 2014

1 Answer

9 years, 8 months ago.

There is no need for any current limitting (actually pull down resistor would break it). If the switch is not pressed, there is no closed connection and no current will run. The pull-up resistor (not an actual resistor, but looks like one) will pull the pin to the power supply. If the switch is closed it will short circuit that pin to the ground, but since the pull-up resistor is in series, only little current will flow.

The only possible cause of damage is that the pin has a tiny capacitance, and that is rapidly discharged through the switch, giving possibly very tiny sparks inside. A resistor in series with the switch could decrease this effect, but it is not worth worrying about for something like this.

Regarding your code:

Instead of DigitalOut you could also consider BusOut, and then store the patterns as chars. Internally BusOut does exactly what you are doing here, so doesn't really matter.

I didn't even know you could make C++ objects like that, but for example the regular way for AnalogIn is:

AnalogIn RandomIn(P0_14);

I don't know if your InterruptIn actually does more than a DigitalIn would do. For sure make variables which change in an interrupt routine volatile.

Accepted Answer

Hi Erik, thanks for your swift reply! Thank you also for your elaborate answer and explanation on the resistors. As written, the mbed environment is new to me, so I greatly appreciate your code comments too. I found the InterruptIn class when searching for how to attach an interrupt routine. I see I still have plenty API documentation to study...

posted by Maxint R&D 01 Sep 2014

Thats indeed what InterruptIn does, and thats very useful in situations. But from a glance at your code (I might be wrong) it doesn't seem you actually need an interrupt, just checking the value of your input pin would work too.

posted by Erik - 01 Sep 2014

It's my impression that using an interrupt makes it more responsive, since the interrupt will also catch button state changes during a wait or activity loop. Furthermore I noticed that the interrupt can be used to revive from deep sleep, which I added in my updated version.

posted by Maxint R&D 08 Sep 2014