Pullups and Pulldowns

I’ve written a simple program for the imp that reads the state of an attached reed sensor:

At first I was using DIGITAL_IN on line 11 which wasn’t working. I saw most of the time other programs were using DIGITAL_IN_PULLUP, which did work, but I don’t know why it works.

Can someone explain (or point me towards information) about the difference between DIGITAL_IN, DIGITAL_IN_PULLUP, and DIGITAL_IN_PULLDOWN?

Thanks!

I’m guessing that your reed switch is grounding the input. If this is the case when the reed is open the input is floating and depending on the processor the result is erratic or undefined. Setting the input as DIGITAL_IN_PULLUP or _PULLDOWN will connect a weak pullup to VDD or pulldown to VSS. This will set the value read when your reed is open to 1 or 0 (depending on which one you pick)

As Bruce says, these are just internal resistors (saving you from adding one externally). If you have a plain switch to ground the input will float when the switch is open hence you need a pull up - either a physical resistor or by using _PULLUP.

Note that for an output you probably want push-pull (ie, DIGITAL_OUT), not open drain with a pullup as you are using. Open drain outputs are used in various places, eg shared interrupt or comms lines, but if you’re trying to turn something on or off you want push pull.

Hi Hugo,

Would this mean you couldn’t use a plain switch on pin 1 without a physical pull-up resistor; if you wanted to use the DIGITAL_IN_WAKEUP function?

Best wishes

Benjamin:-)

Wakeup is always a rising edge, so you need the switch between 3.3v and pin 1.

When booted, you want to configure it as DIGITAL_IN_PULLDOWN - when the imp is asleep, there is an automatic pulldown on this pin as I remember.