Unused Analog Inputs Atmega328 - question

If I am deploying a stand alone projevt and only use 1 of the analog inputs, does it provide any benefit to enable the pull up resistors on the unused pins? I am thinking of stability more than anything else.

It probably won't hurt. The analog reading is actually done "on command" (ie. when you do an analogRead), it isn't just reading away for its amusement. But setting unused pins to a known value is probably going to help keep noise out of the processor.

Thank you Nick, So following what you said, I shouldnt add anything to the current draw either? The device is battery operated.

Well you could put in pull-down resistors to tie them low. That shouldn't consume much, if anything. Pull-ups will add to the current drawn (as you would expect) according to my measurements here:

http://www.gammon.com.au/forum/?id=11497

You can save substantial amounts of power by just running from 3.3V rather than 5V, BTW.

Wow, that is a great article, thank you.
I am using my own board in my device si it has no USB,

[quote author=Nick Gammon link=topic=94281.msg708088#msg708088 date=1330469893] Well you could put in pull-down resistors to tie them low. That shouldn't consume much, if anything. Pull-ups will add to the current drawn (as you would expect) according to my measurements here:

http://www.gammon.com.au/forum/?id=11497 [/quote]

Actually, I'm not sure I would expect pull-ups to add to the current drawn, if nothing else is connected to the pins. Where is the current flowing to?

In fact, in my experimenting with sleep modes, I make all the pins inputs and enable pull-ups before going into power-down mode, and supply current waffles around between 0.1µA and 0.2µA.

According to my measurements on the page quoted above, the difference is about 0.9 uA.

I presume where the current is "going" is making the input MOSFETs "work harder" by amplifying noise.

See page 43 of the datasheet:

If the input buffer is enabled and the input signal is left floating or have an analog signal level close to VCC/2, the input buffer will use excessive power.

and:

An analog signal level close to VCC/2 on an input pin can cause significant current even in active mode.

[quote author=Nick Gammon link=topic=94281.msg708209#msg708209 date=1330481416] According to my measurements on the page quoted above, the difference is about 0.9 uA.

I presume where the current is "going" is making the input MOSFETs "work harder" by amplifying noise.

See page 43 of the datasheet: [/quote]

Right, I saw the measurements, and hence the question as my results seem different. Shouldn't having the pullup turned on reduce noise? As opposed to a floating pin?

Datasheet section 14.2.6, p81, recommends enabling pull-ups to provide a well-defined level for unconnected pins.

Recently I've been thinking about starting each sketch with the following in setup():

PORTB = PORTC = PORTD = 0xFF;    //enable all pullups

an analog signal level close to VCC / 2

A pullup would put the signal level close to VCC not close to VCC divided by 2.

My results have been similar to Jack’s. For unconnected pins, I have not measured a difference between input with pullup, output low, or output high. But my Extech multimeter may not be up to the task.

Er, yes you both make good points. The pull-up on the face of it would result in not-floating pins.

However to check, I re-measured using the board shown on the page I linked above.

First with this sketch:

#include <avr/sleep.h>
void setup () 
{
  for (byte i = 0; i <= A5; i++)
    {
    pinMode (i, INPUT);    // changed as per below
    digitalWrite (i, LOW);  //     ditto
    }  
  // disable ADC
  ADCSRA = 0;   
  set_sleep_mode (SLEEP_MODE_PWR_DOWN);  
  sleep_enable();
  // turn off brown-out enable in software
  MCUCR = _BV (BODS) | _BV (BODSE);
  MCUCR = _BV (BODS); 
  sleep_cpu ();  
}  // end of setup

void loop () { }

Results:

Meter: 0.11 uA
Using Dave Jones' uCurrent device: 120 nA

So, around 110 to 120 nA with pins as input and no pull-ups.

Now with the revised sketch:

    digitalWrite (i, HIGH);  //    enable pull-ups

Results:

Meter: 75 uA
Using Dave Jones' uCurrent device: 75 uA

I admit that neither figure agrees with my previously-posted results (maybe the fuses were different).

I can’t explain why my figures are so different to yours, except that we must be using different hardware, and maybe different fuse settings.

However let’s assume you aren’t planning to sleep. Then the results are a bit different. This sketch:

void setup () 
{
  for (byte i = 0; i <= A5; i++)
    {
    pinMode (i, INPUT);    
    digitalWrite (i, LOW);  
    }
}  // end of setup

void loop () { }

Current measured: 15.3 mA.

Changing to set all pins to pull-ups gives 11.8 mA.

So in that case, we saved 3.5 mA by turning the pull-ups on.

[quote author=Nick Gammon link=topic=94281.msg708239#msg708239 date=1330486196] However let's assume you aren't planning to sleep. Then the results are a bit different. This sketch:

Current measured: 15.3 mA.

Changing to set all pins to pull-ups gives 11.8 mA.

So in that case, we saved 3.5 mA by turning the pull-ups on. [/quote]

Pretty sure I observed something similar recently, unfortunately did not record the results. But hence the idea of starting a sketch with all pullups on. That plus a couple of my projects which have been occasionally acting a bit funny, like they're responding to capacitive effects or maybe static electricity.

[quote author=Jack Christensen link=topic=94281.msg708249#msg708249 date=1330487072] [quote author=Nick Gammon link=topic=94281.msg708239#msg708239 date=1330486196] However let's assume you aren't planning to sleep. Then the results are a bit different. This sketch:

Current measured: 15.3 mA.

Changing to set all pins to pull-ups gives 11.8 mA.

So in that case, we saved 3.5 mA by turning the pull-ups on. [/quote]

Pretty sure I observed something similar recently, unfortunately did not record the results. But hence the idea of starting a sketch with all pullups on. That plus a couple of my projects which have been occasionally acting a bit funny, like they're responding to capacitive effects or maybe static electricity. [/quote]

On an unaltered Arduino (I have a dozen or so Pro Mini's lying around, some adulterated, so virginal ...) I see pins floating to the voltage of other pins in the same port. For digital pins, I see them signal close to whatever is nearby -- most often around line frequency. So, I'm guessing they are sensitive to capacitive effects (parasitic capacitance on the die, maybe?) as well as RF.

Good point. Probably to get defined behaviour you should have defined inputs into the pins. I would have thought that tying a pin low through a 10K resistor would do that at minimal cost, but maybe I'm wrong.

It should make no difference* but if you don't mind try this...

  // turn off brown-out enable in software
  cli();
  MCUCR = _BV (BODS) | _BV (BODSE);
  MCUCR = _BV (BODS); 
  sei();
  • Except in the very unlikely event that an interrupt occurs between the two MCUCR assignments.

[quote author=Nick Gammon link=topic=94281.msg708333#msg708333 date=1330495805] Good point. Probably to get defined behaviour you should have defined inputs into the pins. I would have thought that tying a pin low through a 10K resistor would do that at minimal cost, but maybe I'm wrong. [/quote]

That should work fine, although the internal pullups may win out on the cost angle ;)

[quote author=Coding Badly link=topic=94281.msg708341#msg708341 date=1330496732] * Except in the very unlikely event that an interrupt occurs between the two MCUCR assignments.

[/quote] Seems fairly unlikely given cli() and sei() ...

Same results.

[quote author=Jack Christensen link=topic=94281.msg708592#msg708592 date=1330520485]Seems fairly unlikely given cli() and sei() ...[/quote]

:P

  • Except in the very unlikely event that an interrupt occurs [u]had been occurring in Nick's original code[/u] between the two MCUCR assignments.