If you are new to electronics you might here a phrase like “Just add a pulldown to that”. Or “Cap the crap outta that.”. In Arduino-land you might also hear “I2C needs pullups on the SDA and SCL lines”.
And the advice from the wizards will be something like “Meh, just throw some components at it.”. At least that is how it will sound at first. So many options in the datasheets; so little actual guidance.
I came across an example with my ‘on the side’ project today. A floating input pin on the ESP 8266 wasn’t always reading as digital high or low. No real surprise, actually; that’s what floating pins do. So I thought I’d write this one up for reference, since the component values are general enough to be useful.
First off, realize that if something is going to be sent a signal as digital high (ie digitalRead(pin) == HIGH) then to give it stability you’re going to have to pull it down to near ground; at least below where the voltage will register as digital LOW. The reverse holds true also; if something is going to get a signal as 0V reference, it probably needs to be pulled up to Vcc or whatever digital HIGH is. I2C is like this, and Nick Gammon has some good advice and examples of why resistor values for pullups aren’t set in stone.
My example is the first type; I’m switching +Vcc but otherwise I have to keep the pin pulled to ground to stop spurious readings. The other bit of magic is to select minimal values that prevent excess current flow that either drain the power supply or damage the digital pins input circuits.
Now if you sketch this out you’ll realize it looks like a voltage divider… so now it’s just a matter of calculating the top and bottom values according to how badly it could misconfigured in software. Imagine that you plug in an ESP-01 which has a pin set HIGH and the circuit grounds that pin directly. Or +3.3V is applied directly to a pin with no current limiting resistor. Ouch.
The only real critical value is that I’d like to maintain is the output above about +2.6V, which is what the ESP-8266 uses for digital HIGH. The input is +3.3V, so the rest is simple:
I still need to deploy this in the real world for testing; the wiring for this circuit involves many meters of wire between the ESP and the two switches which might alter the effective value of R1. But as a start it’s ok. In terms of ratios I could also go for 3.3k and 15k; and in fact I might if the value of R1 turns out to be closer to 4k or 5k; I can use a 10k fixed value plus the middle of a 10k trimmer pot to dial in an optimal ratio around 15k.