voltage sensor weird

i'm using Arduino Mega and 25v voltage sensor from MH Electronic.

i'm trying to measure (some) battery voltage but it always displaying 0v.
but when i put VCC (sensor in) into 5v or 3.3v on the Arduino board, it can measure the voltage normaly.

my cable connectors :
(sensor out) s -> (arduino) A1
(sensor out) + -> (arduino) 5v
(sensor out) - -> (arduino) GND

my code :

#include <Wire.h>
float val11;
float val2;

void setup()
{
  Serial.begin(9600);
}
void loop()
{
  float temp;
  val11=analogRead(1);
  temp=val11/4.092;
  val2=(temp/10);
  Serial.println(val2);     
   delay(500);
}

what's wrong with this setup?

What voltage are you trying to measure. Only DC with the right polarity will work.

Did you get the code from "Destructables".

Seems someone has pulled some random lines out of a hat.
"wire" is for I2C devices, "temp" is for a temperature sensor, and "4.096" seems to come from a 12-bit A/D.

Try this sketch.
25.0 is coming from the 1:4 voltage divider ratio (7k5 and 30k), multiplied with 5volt Aref (assuming you're using an Uno/Nano/Mega).
Leo..

void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.println(analogRead(A1)*25.0/1024);
  delay(500);
}

Wawa:
What voltage are you trying to measure. Only DC with the right polarity will work.

Did you get the code from "Destructables".
http://www.instructables.com/id/Arduino-Voltage-Sensor-0-25V/
Seems someone has pulled some random lines out of a hat.
"wire" is for I2C devices, "temp" is for a temperature sensor, and "4.096" seems to come from a 12-bit A/D.

Try this sketch.
25.0 is coming from the 1:4 voltage divider ratio (7k5 and 30k), multiplied with 5volt Aref (assuming you're using an Uno/Nano/Mega).
Leo..

void setup() {

Serial.begin(9600);
}

void loop() {
  Serial.println(analogRead(A1)*25.0/1024);
  delay(500);
}

thank you for your solution..
i have tried it, but still same..

image : Screenshot by Lightshot

when i pulled off the sensor cable (A1) it shows random value, so i think it's not a sensor failure..
but what..?

The thing is just two resistors in series making a voltage divider. If you have a DVM, digital volt/ohm meter, check the resistance values. If no meter, look very carefully at the soldering job on all the connections. This is too simple to fail!

Paul

sgun:
i have tried it, but still same..

when i pulled off the sensor cable (A1) it shows random value, so i think it's not a sensor failure..
but what..?

Tried what, and what are the results.
Again, what are you trying to measure (can't see what's connected to the screw terminal).

Random values from an unconnected pin is normal.

The purple wire (5volt) does nothing, and can be removed.
See this tutorial.
Leo..

Wawa:
Tried what, and what are the results.
Again, what are you trying to measure (can't see what's connected to the screw terminal).

Random values from an unconnected pin is normal.

The purple wire (5volt) does nothing, and can be removed.
See this tutorial.
Leo..

try the code embedded in your post.

yes, i already remove the + wire and still same result.

i'm trying to measure AA battery voltage (1,5v).
i connect sensor input :
VCC -> + battery
GND -> - battery
at first i think it's sensor failure, but i bought 10 pieces and i try it all, and the result is same : 0v.

but when i connect the VCC sensor input to 5v or 3.3v on arduino board, it's measure correctly WITHOUT connecting the GND sensor input to GND arduino board.

really confused with this..

It sounds like you're doing it right.

Have you tried measuring 3 or 4 AA batteries in series or perhaps a 9V battery? I wonder if 0.375V (1.5V/4) is below some threshold for your code. As Leo says try printing the actual result of the analogRead directly. Do you actually get literally all zeros?

Steve

slipstick:
It sounds like you're doing it right.

Have you tried measuring 3 or 4 AA batteries in series or perhaps a 9V battery? I wonder if 0.375V (1.5V/4) is below some threshold for your code. As Leo says try printing the actual result of the analogRead directly. Do you actually get literally all zeros?

Steve

i have tried 9v battery, but with same result.

at first, the result is not 0v, 0.xx volt. but it only last for few loops (3-5 loops with delay 500) and then back to 0v again. it happened everytime i connect the VCC input sensor to + battery without connecting the - battery.

i tried to print the actual analogRead value, same pattern but with different value, it's end with 0 value.

sgun:
i have tried 9v battery, but with same result.

at first, the result is not 0v, 0.xx volt. but it only last for few loops (3-5 loops with delay 500) and then back to 0v again. it happened everytime i connect the VCC input sensor to + battery without connecting the - battery.

You can't expect to read anything without connecting BOTH terminals of the battery. You have to complete the circuit. If you're just connecting the battery +ve then you are getting what you should expect.

And before you say it works with the 5V from the Arduino, that's because you have that voltage's circuit completed via the output ground

Steve

slipstick:
You can't expect to read anything without connecting BOTH terminals of the battery. You have to complete the circuit. If you're just connecting the battery +ve then you are getting what you should expect.

And before you say it works with the 5V from the Arduino, that's because you have that voltage's circuit completed via the output ground

Steve

sorry if i missed something..
but the result is 0v with or without GND sensor input connected to - battery..

This should be quite simple indeed.

Take your 9V battery, makes sure it actually outputs 9V (use your multimeter to confirm this - you do have one, no? If not - do something about that!), connect it to the screw terminals, then use your multimeter to measure the voltage between the output pin and the GND pin. Now you should measure 2.25V.

Then you can connect the sensor to your Arduino - and measure again to make sure you see the same 2.25V on your analog pin (use the one you have set in your software).

And if you really still can't see any voltage it's just about possible that the input ground on the board is not connected.

If you have a meter that measures resistance check that there is zero resistance between the input and output grounds. If you don't have a meter try connecting your battery +ve to the Vcc screw terminal with your battery -ve connected to the OUTPUT - pin.

Steve

thank you for all your replies..
i won't make this topics more complicated, so i will try to explain with this picture :

  1. i'm now using new AAA battery and i check with multimeter, it give me 1,55v img 1 .
  2. i check the first resistor on my sensor, it give me 30.0K img 2 and the second resistor 7.5K img 3 .
  3. this is how i measure the battery voltage img 4 and it give me 0v result on serial monitor.
  4. but when i did this img 5 it give me 3.25-3.3v. and this img 6 give me 5.0v.
  5. i'm using this code :
void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.println(analogRead(A1)*25.0/1024);
  delay(500);
}
  1. i have 10 sensors and all give me same results.

please help.. what should i do to measure the battery normally..

Please post images here instead of a bunch of external links that are not even clickable.

wvmarle:
Please post images here instead of a bunch of external links that are not even clickable.

sorry, edited.

Unfortunately you missed the first and main part of my message. At least they're clickable.

Img 4, 5 and 6 are useless without proper description. Can't see from the image how things are connected.

Did you try to measure the actual voltage on the output pins, as I asked for in #10?