Analog Read from empty pins display bigger than 0

Hi there,

I had a electronic sensor which had 0-5v linear output. My aim read this value from arduino and send to pc via USB.

I try to read via analog read and failed.

After some trial error I found my empty analog pins shows values. Also A4 and A5 displays constant 1023 value.

I though pins have to show 0.

My Board is olimex-328

void setup() {
  Serial.begin(9600);
  analogReference(INTERNAL); //analog in can only go up to 1.1v!  
}

void loop() {
  
  int sensorValue = analogRead(4);
  
  Serial.print("sensor = " );                       
  Serial.print(sensorValue);      
  Serial.print(" Voltage=");
  Serial.println(voltage(sensorValue));
  delay(100); // wait 1/10 of a second  
}

double voltage(int analogIn){
  double voltage = (analogIn/1023.0) * readVcc();
  return voltage;
}

long readVcc() {
  // Read 1.1V reference against AVcc
  // set the reference to Vcc and the measurement to the internal 1.1V reference
  #if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
    ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  #elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
    ADMUX = _BV(MUX5) | _BV(MUX0);
  #elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
    ADMUX = _BV(MUX3) | _BV(MUX2);
  #else
    ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  #endif  
 
  delay(2); // Wait for Vref to settle
  ADCSRA |= _BV(ADSC); // Start conversion
  while (bit_is_set(ADCSRA,ADSC)); // measuring
 
  uint8_t low  = ADCL; // must read ADCL first - it then locks ADCH  
  uint8_t high = ADCH; // unlocks both
 
  long result = (high<<8) | low;
 
  result = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000
  return result; // Vcc in millivolts
}

I found some posts about arduino ground may affect analog read values.

What is my problem.

Also is there any way to detect internal voltage. (3.3 or 5v)

My Best Regards

I found my empty analog pins shows values.

Floating pin's potential is influenced by its environment.

That's one reliable to generate truly random numbers.

I though pins have to show 0.

Only if the pin(s) are wired to something that is presenting a 0 voltage to the analog input pin. A unconnected pin is not 'seeing' a 0 voltage, but rather just responding to circuit noise and is called a 'floating input pin condition'. If it's important to you that unused pins read 0 then wire the pin to ground through a pull-down resistor. Note that this also applies to digital input pins, unconnected (floating) digital input pins will return random HIGH or LOW values as noise determines.

That’s one reliable to generate truly random numbers.

to certain extend as it depends on all kind of electrical properties of the Arduino and connected hardware (to other pins).

that said, most of the time this trick will work well enough. use it to get the seed for the built in random generator as 32 analogreads take quite some time.

unsigned log getSeed()
{
  uint32_t seed = 0;
  for (int i=0; i< 32; i++)
  {
     seed = (seed << 1) & (analogRead(0) & 0x01);
  }
  return seed;
}

use it

Some revision to it is probably needed.

Oh no. Never ending random generator debate detected !!! :astonished:

Anyway

To read 0 from analogRead I have to connect analog pin to ground with pull down resistor.

Is it effect my sensor output ? or shall I do something extra for my sensor ?

Show us the circuit you used?

oldnoob: Oh no. Never ending random generator debate detected !!! :astonished:

Anyway

To read 0 from analogRead I have to connect analog pin to ground with pull down resistor.

Is it effect my sensor output ? or shall I do something extra for my sensor ?

My advice to add a pull-down was in reference to analog input pins that have nothing wired to them, if it bothers you that reading from unconnected pins don't return 0. However that has no effect or requirement on a analog input pin that you are using and that you have wired to a sensor, that should not require any pull-up or pull-down resistor. So show us the sensor and your wiring drawing and we shall see if there is some electrical problem or not.

Lefty

Actually it was not a plain sensor. I'im trying to read my Wideband controller (a commercial one) linear output form arduino analog pin.

Sensivity is important. My readings are not lienar. It sends values like waves which made me nervous and I want to make sure nothing was intefere the signal.

My Best regards