Analoge Read Values

Hello,

I am using this code

#include <Wire.h>

int val11; 
int val2; 

void setup() 
{ 
Serial.begin(9600); 
Serial.println("Emartee.Com"); 
Serial.println("Voltage: "); 
Serial.print("V"); 
} 
void loop() 
{ 
float temp; 
val11=analogRead(1); 
temp=val11/4.092; 
val11=(int)temp;// 
val2=((val11%100)/10); 
Serial.println(val2); 
delay(1000); 
}

With a module that I got from the ebay. The problem is that the output is not stable cause of the analoge read values. Even if the pin is not pluged the value goes to 300…How to solve this?

Even if the pin is not pluged the value goes to 300...How to solve this?

Plug the pin to something like ground or the 5 V pin.

With a module that I got from the ebay.

What module exactly ?

The problem is that the output is not stable cause of the analoge read values

What range of values are you seeing ?

Even if the pin is not pluged the value goes to 300.

If the pin is floating then random values are to be expected.

How to solve this?

Maybe only print when the values changes significantly

Thats the module.

http://www.ebay.com/itm/380773553422

I am trying to read 9v from a battery.

I cant read only the big changes as this is going to be used for a motorcycle battery so I ll need to have a real time update of the volts.

I also tryied to read the Volt without a module using 100k and 10k resistors but no luck again.

I think we need a schematic

I cant read only the big changes as this is going to be used for a motorcycle battery so I ll need to have a real time update of the volts.

The changes do not have to be big, just significant which is not the same thing. What would you regard as a significant change of voltage and, as I asked previously what range of values are you seeing ?

Moderator edit: tags correction

eldonate:

void loop() 

{
float temp;
val11=analogRead(1);
temp=val11/4.092;
val11=(int)temp;//
val2=((val11%100)/10);
Serial.println(val2);
delay(1000);
}

A note on style - don’t use variables without a reason, don’t use global variables without a good reason,
use meaningful names and constant values and avoid clutter and use plenty of whitespace:

// no global variables needed
void loop() 
{
  // convert to nearest tenth of a volt (input is 1/5th scaled so 25V is full scale)
  int tenths = (int) (analogRead(1) * 250.0 / 1024 ;

  Serial.println (tenths / 10 % 10);  // display the volt units only

  delay (1000); 
}

You can assume normal operator precedence for +,-,*,/ etc, parentheses are used
when necessary or when the precedence isn’t mathematically obvious (such as when mixing &,+,<< etc)

Making code clear and simple is important, because you or someone else will come back to it
and some point and have to figure out what it means - names like ‘val11’ and constants like 4.092
aren’t self-documenting (or even correct!)

The code from post#0 is some random code from the page of the resistor divider. http://www.ebay.com/itm/380773553422 I think OP just want to see how much is left in a 9volt battery. Leo..

Wawa: The code from post#0 is some random code from the page of the resistor divider.

Which he says that he is using, but is getting unstable values. He says that he is currently measuring the voltage of a 9V battery but the electrical environment of a motorcycle will be more hostile.

He has not provided a schematic as requested nor answers to questions so we are working in the dark. For instance, is the Arduino powered by the 9V battery that he is trying to measure ? The typical 9V (PP3) battery will not power an Arduino for long.

UKHeliBob: Which he says that he is using, but is getting unstable values. He says that he is currently measuring the voltage of a 9V battery but the electrical environment of a motorcycle will be more hostile.

He has not provided a schematic as requested nor answers to questions so we are working in the dark. For instance, is the Arduino powered by the 9V battery that he is trying to measure ? The typical 9V (PP3) battery will not power an Arduino for long.

The voltage of the motorcycle will be really unstable. But before I install it i ll have to be sure that it is "working" at an early stage cause troubleshooting afterinstallation wont be that easy so I ll have to avoid the main issues.

So I tried to test it with a 9V battery but the values are really far from what I am supposed to get. (I got like 15-28V on the serial while I plug the 9v bat to test it).

My first attemp was this guide but no luck....

http://www.electroschematics.com/9351/arduino-digital-voltmeter/

by the way....the guide that I posted on my latest reply wotn "consume" the battery??

It has been posted in the past that analog reads may be more stable when the analog pin is read twice anf the second read is used. This possibly allows the analog read circuit to stabilize some before the reading to be used is taken.

float temp;
val11=analogRead(1);
val11=analogRead(1);  // reading to be used
temp=val11/4.092;
val11=(int)temp;//
val2=((val11%100)/10);
Serial.println(val2);
delay(1000);

So I tried to test it with a 9V battery but the values are really far from what I am supposed to get. (I got like 15-28V on the serial while I plug the 9v bat to test it).

I am confused as to whether the values you are seeing are wrong or unstable. Can you please clarify ?

Have you tried using the code from the link you provided ?

The circuit shown in the link you provided appears to be incomplete. Can you please post a diagram of the full circuit that you are using ?

What are the raw values that you get from analogRead(1) ?

UKHeliBob: I am confused as to whether the values you are seeing are wrong or unstable. Can you please clarify ?

Have you tried using the code from the link you provided ?

The circuit shown in the link you provided appears to be incomplete. Can you please post a diagram of the full circuit that you are using ?

What are the raw values that you get from analogRead(1) ?

I focused on getting 0V for now....even without battery on my A0 I get like 14V....thats not good no matter circuit....the raw value from A0 is 300-400

I'm confused as to what you are doing, but connecting any voltage greater than 5v to an arduino analog in pin may ruin your arduino. Do you have the grounds connected between the arduino and the battery grounds you are testing?

That voltage divider board is 1:5. Good to drop 25volt to 5volt, or 16.5volt to 3.3volt.
It is better to measure a battery against Arduino’s internal reference voltage.
In case the battery drops below the minimum input voltage of the onboard regulator.
Use the 10k/100k resistors you used before, and try this sketch.
Leo…

// displays the voltage of a 9V battery
// works with 3.3volt and 5volt Arduinos
// change (INTERNAL) to (INTERNAL1V1) for a Mega
// uses the internal 1.1volt reference
// 100k resistor from A1 to +batt
// 10k resistor from A1 to ground
// optional 100n capacitor from A1 to ground
//
unsigned int total;
//
void setup() {
  analogReference(INTERNAL); // use the internal ~1.1volt reference,
  Serial.begin(9600);
}
//
void loop() {
  analogRead(1); // one unused read
  for (int x = 0; x < 10; x++) { // 10 readings
    total = total + analogRead(1); // add each value
  }
  Serial.print("The battery is ");
  Serial.print(total * 0.001142); // value to volts, adjust last digits to calibrate
  //Serial.print(total * 0.0010742, 1); // displays with one decimal place
  Serial.println(" volt");
  total = 0; // reset value
  delay(1000); // delay, so humans can read it
}

I dont plug anything....raw values of A0 are 300-400 without anything pluged....

eldonate: I dont plug anything....raw values of A0 are 300-400 without anything pluged....

Search the forum or google for "floating pin" and you may find an answer to this reading.

Wawa:
That voltage divider board is 1:5. Good to drop 25volt to 5volt, or 16.5volt to 3.3volt.
It is better to measure a battery against Arduino’s internal reference voltage.
In case the battery drops below the minimum input voltage of the onboard regulator.
Use the 10k/100k resistors you used before, and try this sketch.
Leo…

// displays the voltage of a 9V battery

// works with 3.3volt and 5volt Arduinos
// change (INTERNAL) to (INTERNAL1V1) for a Mega
// uses the internal 1.1volt reference
// 100k resistor from A1 to +batt
// 10k resistor from A1 to ground
// optional 100n capacitor from A1 to ground
//
unsigned int total;
//
void setup() {
  analogReference(INTERNAL); // use the internal ~1.1volt reference,
  Serial.begin(9600);
}
//
void loop() {
  for (int x = 0; x < 10; x++) { // 10 readings
    total = total + analogRead(1); // add each value
  }
  Serial.print(“The battery is “);
  Serial.print(total * 0.001142); // value to volts, adjust last digits to calibrate
  //Serial.print(total * 0.0010742, 1); // displays with one decimal place
  Serial.println(” volt”);
  total = 0; // reset value
  delay(1000); // delay, so humans can read it
}

I used this. Look at my output.

Baterry unplaged

The battery is 11.68 volt
The battery is 11.68 volt
The battery is 11.68 volt
The battery is 11.68 volt
The battery is 11.38 volt

Baterry plaged

The battery is 1.54 volt
The battery is 0.27 volt
The battery is 1.95 volt
The battery is 0.00 volt
The battery is 0.95 volt
The battery is 0.15 volt
The battery is 0.00 volt
The battery is 1.81 volt
The battery is 0.00 volt
The battery is 0.02 volt
The battery is 1.39 volt
The battery is 0.00 volt
The battery is 0.01 volt
The battery is 1.46 volt
The battery is 0.00 volt
The battery is 1.20 volt

Did you use the right value resistors, and connected them to A1, as explained in the code.
11.68 means overflow. More than 11.68volt, or the wrong resistors.
A random reading without battery connected could mean that the resistor to ground is missing.

I have tested this sketch with a 9volt battery.
Works 100%.
Leo…

9Vbatt.jpg