An engineer told me that all unused pins on a MCU (Uno, esp32 etc) should be set to a value. I don't remember if it was HIGH or LOW but my guess would be LOW.
Part two is do you do the pinmode(pin, OUTPUT) first, or the digitalWrite(pin, LOW) first.
-
That depends.
-
See this.
- However, don’t leave inputs floating.
“pinmode(pin, OUTPUT) first, or the digitalWrite(pin, LOW) first.“
- If you want a LOW at power up, digitalWrite(pin, LOW) first, then pinmode(pin, OUTPUT).
I know it's the common assumption that pins should not be left floating (i.e. - in INPUT mode). But the tests I've done actually measuring power-down sleep current on the 328P don't show any difference between INPUT, OUTPUT Low, and OUTPUT High if nothing is connected to the pins. I guess to be accurate I should say that any difference that may exist is some fraction of 1uA.
According to Gammon here are the details when in SLEEP_MODE_PWR_DOWN
I haven't found info for normal mode but my assumption is that the OUTPUT LOW current will be the same, 0.35UA. I can see I will have to get one of those Current Ranger's.
I don’t think it matters I just set them as outputs. However, if you plan to use special modes, be sure to consult the datasheet for details.
If you never read the pin, why care if it floats?
- Atmel datasheet:
13.2.6 Unconnected Pins
If some pins are unused, it is recommended to ensure that these pins have a defined level. Even though most of the digital inputs are disabled in the deep sleep modes as described above, floating inputs should be avoided to reduce current consumption in all other modes where the digital inputs are enabled (Reset, Active mode and Idle mode).
The simplest method to ensure a defined level of an unused pin, is to enable the internal pull-up. In this case, the pull-up will be disabled during reset. If low power consumption during reset is important, it is recommended to use an external pull-up or pull-down. Connecting unused pins directly to VCC or GND is not recommended, since this may cause excessive currents if the pin is accidentally configured as an output.
Pins can float to intermediate levels ("illegal" as digital levels) causing both output mosfets to be partially biased into their active regions, significantly increasing current consumption.
Detail: here's the schematic for a CMOS inverter:
If Vin is at Vdd, the bottom N-channel MOSFET should be on (Vgs = Vdd) and the P-channel MOSFET should be off. If Vin is at GND potential, the opposite happens. If Vin is, say, halfway between VDD and GND, it's possible that both mosfets are "slightly on", giving a direct path between Vdd and GND; if "slightly" is small, you get more current consumption than you would otherwise. If it's larger, you can potentially burn out one or both mosfets.
Since I don't connect anything to unused pins,... I guess making them INPUT_PULLUP won't hurt.
If you use battery supply and are concerned about current consumption, you will probably use a sleep sate. In the ATmega328 a hardware solution is built in. Other MCUs may or may not have similar solutions. Anyway, if you supply is sufficient to go without sleep states, the power consumption will be negligible.
Furthermore:
-
If the burn out of post #8 could occur, the chip would be unreliable and would be taken out of production.
-
The input circuitry of the ATmega328 contains a Schmitt trigger.
-
digitalMode
will set the bit in the output register, to 0 for INPUT and to 1 for INPUT_PULLUP, no need for adigitalWrite
.
Does not… (assuming you meant pinMode)
In fact, you can avoid short LOW output by doing a digitalWrite(pin, HIGH) before you do the pinMode()
Hmm. Perhaps pinMode(pin, INPUT) writes 0 to the output register, since otherwise a previous HIGH would result in pull-up mode instead.
Correct! pinMode
will set the bit in the output register, to 0 for INPUT and to 1 for INPUT_PULLUP, no need for a digitalWrite
. At least, for the ATmega328 and similar MCUs.
Not all MCUs use the output register to switch the pullup resistor, in which case the digitalWrite
is needed, and best put before the pinMode
.
A RESET will set the output register to 0. (All registers actually.)
So are you only concerned what happens when you are in sleep mode?
See post #7. Power consumption.
So that is the circuit inside every micro controller? Remember we are talking about pins with nothing attached and what happens if left floating.
Power is OFF, nothing attached to pin. What amt of current is used in each pinmode and digitalwrite combo see post#4.
No, all modes. Need to reduce battery use as much as possible.
If the pin is not connected to anything there can't be much consumption.
On power-up all IO pins default to INPUT LOW.
To turn that HIGH, write 1 to the matching PINx bit cause otherwise the pin might float and cause duelllng mosfets, I am informed.
For years now I just let unused IO pins stay INPUT LOW. Now I know how bad that was and how lucky I have been. I thought that the pin only took in a sample to evaluate during the read process and that sample is 1 uAmp but now it's going on all the time, 2 mosfets deciding if PINx0-7 is HIGH or LOW does match with direct port PINx register read if the sampling doesn't happen as part of reading that register.
See post #4. If you want to disagree with Gammon, go for it, me I take his word as gospel. https://www.gammon.com.au/power
The note about sleep mode? How did I disagree with that?
from the 328P datasheet --- note that we don't read in Sleep Mode
18.2.4. Reading the Pin Value
Independent of the setting of Data Direction bit DDxn, the port pin can be read through the PINxn
Register bit. As shown in Ports as General Digital I/O, the PINxn Register bit and the preceding latch
constitute a synchronizer. This is needed to avoid metastability if the physical pin changes value near the
edge of the internal clock, but it also introduces a delay. The following figure shows a timing diagram of
the synchronization when reading an externally applied pin value. The maximum and minimum
propagation delays are denoted tpd,max and tpd,min respectively.
Figure 18-3. Synchronization when Reading an Externally Applied Pin value.
--and then there's a timing diagram showing synch and read.
But the important part is that the sample comes from the pin and what is connected or not to it, not chip power which that chart gave you. When I filled a jumper connected to a pin from a BJT, the jumper held the current that set the pin HIGH. There was no pulldown. I counted reads to drain the jumper to LOW noting that a read takes a sample from the pin. The same works with using a led as a light detector.
Nick Gammon is one of the members who taught me to read datasheets carefully. I was here when he was active.