Problems with my I2C ADC

Hey everyone,

I got this 16-bit ADC to more accurately measure the voltage coming off a sensor that I have. The sensor outputs 0-5VDC.

This is how I have it hooked up:


and this is my code:

#include  <Wire.h>
int address = 0x14;
 
void setup()
{
  Serial.begin(9600);
  Wire.begin();
}
 
void loop()
{
 
  Wire.requestFrom(address, 2);
 
  while(Wire.available() == 0);
 
  double data = Wire.read() << 8;
  data += Wire.read();
 
  Serial.println(data); //Number coming off the ADC from 0-2^16)
 
  double power = data/13.1072 ; //to convert to mV
 
  Serial.println(power); //print number in mV
  Serial.println("========="); //Spacer to make reading the output easier
 
  delay(1500);
}

and this is the output I am getting at 1V on my bench PSU

-27677.00
-2111.59
=========
-27593.00
-2105.18
=========
-27730.00
-2115.63
=========
-27564.00
-2102.97
=========
-27649.00
-2109.45
=========
-27570.00
-2103.42
=========
-27634.00
-2108.31
=========
-27503.00
-2098.31
=========

I'm trying to make it so that the second number is in millivolts. Can anyone help me with this? I don't know where I'm going wrong. (I'm a noob so please me patient with me if this is a stupid question :))

Wire.requestFrom(address, 2);
 
  while(Wire.available() == 0);
 
  double data = Wire.read() << 8;
  data += Wire.read();

Wire.requestFrom returns the number of bytes it obtained. Doing the "while" on the second line is futile, and would only send it into a loop. Better would be:

  if (Wire.requestFrom(address, 2) != 2)
     {
     // error!   run away!
     }
 
  double data = Wire.read() << 8;
  data += Wire.read();

I don't really know what that did, but thanks! It didn't change the output though, I'm still getting seemingly random numbers :frowning:

double data = Wire.read() << 8;
  data += Wire.read();

If the value from the ADC is an integer (and it is), then why not treat it as one?