ADS1015 ADC module program with Wire.h library

Hello,
I am trying to develop a program to read analog readings via the ADS1015 module with WEMOS D1 development board.
I have developed a program shown below, but the value doesn't change when the analog signal gives in to the module. Is there any way do this only using wire.h library.

#include <Wire.h>

const int sda = 4;
const int scl = 5;
void setup() {
  Serial.begin(115200);
  Wire.begin();
  //Wire.begin();
  Wire.beginTransmission(0x48);
  Wire.write(0x00);  
  Wire.write(0x00); 
  Wire.endTransmission();

  Wire.beginTransmission(0x48);
  Wire.write(0x01);  
  Wire.write(0x8583);   
  Wire.endTransmission();

  Wire.beginTransmission(0x48);
  Wire.write(0x02);   
  Wire.write(0x8000);
  Wire.endTransmission();  

  Wire.beginTransmission(0x48);
  Wire.write(0x03);   
  Wire.write(0x7FFF);
  Wire.endTransmission();        




}

void loop() {

  Wire.beginTransmission(0x48);
  Wire.write(0x4284);   
  Wire.endTransmission();
  Wire.requestFrom(0x48,1,true);


  delay(1000);
  while(Wire.available()){

    //int16_t c = (Wire.read()|Wire.read()<<8) & 0b0000111111111111;
    int16_t c = Wire.read();  
    Serial.print("Value1: ");
    Serial.println(c);
    Serial.println("");
  }



}

Welcome to the forum.

Could you try a I2C Scanner sketch ? That is needed to check if the I2C bus is working.

The Wire.write() function uses a single byte, or a pointer to data.
Looking into the datasheet, I think that the high byte comes first, for reading and writing.

// Wire.write(0x8583);
Wire.write(0x85);
Wire.write(0x83);

There is no need for a delay after the Wire.requestFrom() and there is no need for a while-statement for reading. Two bytes are requested and two bytes will be read.

int n = Wire.requestFrom(0x48, 2);   // request 2 bytes
if (n != 2)
  Serial.println("Error, ADS1015 did not give us two bytes");

byte hiByte = Wire.read();
byte loByte = Wire.read();
int16_t c = word(hiByte,loByte);
1 Like

Koepe,
Thanks for the reply. I have tried I2C scanner on this case. It gives 0x48 as the I2C address. I will go through the details provided.

Hello Koepel,
I have modified the code as shown below. But now the serial read shows 32767 that comes from hibyte and loByte values. When I gave input to may analog termianl in the ADS1015 module this value 32767 still shows on serial moniter.
Is the reason wrong Config Register Configuration?
How should I set ADS1015 Config Register Configuration?

#include <Wire.h>

const int sda = 4;
const int scl = 5;
void setup() {
  Serial.begin(115200);
  Wire.begin();

  Wire.beginTransmission(0x48);
  Wire.write(0x00);  
  Wire.write(0x00); 
  Wire.endTransmission();

  Wire.beginTransmission(0x48);
  Wire.write(0x01);  
  Wire.write(0x8583);   
  Wire.endTransmission();

  Wire.beginTransmission(0x48);
  Wire.write(0x02);   
  Wire.write(0x8000);
  Wire.endTransmission();  

  Wire.beginTransmission(0x48);
  Wire.write(0x03);   
  Wire.write(0x7FFF);
  Wire.endTransmission();        

}

void loop() {

  Wire.beginTransmission(0x48);
  Wire.write(0x85); 
//  Wire.write(0x83); 
  Wire.endTransmission();

  Wire.beginTransmission(0x48);
  Wire.write(0x83); 
  Wire.endTransmission();

  int n = Wire.requestFrom(0x48,2);
  //Serial.println("9");
  if (n !=2){
    Serial.println("Error, ADS1015 did not detected bytes");

  }
  
  byte hiByte = Wire.read();
  byte loByte = Wire.read();
  int16_t c = word(hiByte, loByte);
  
  Serial.println(c);

  delay(1000);

}
![image|364x500](upload://ruJm7Idrsyv6HAFFkrsNBzCAuR7.png)

I did not read the datasheet very well, but I think that the register address is 8 bits and the registers are 16 bits. I suggest to always read and write the full 16 bits.

It seems that you are reading 0xFF, 0xFF.

I have added comment

#include <Wire.h>

const int sda = 4;
const int scl = 5;

void setup() {
  Serial.begin(115200);
  Wire.begin();

  Wire.beginTransmission(0x48);   // okay, I2C address
  Wire.write(0x00);               // okay, register address
  Wire.write(0x00);               // high byte
                                  // ! where is the low byte ?
  Wire.endTransmission();

  Wire.beginTransmission(0x48);   // I2C address
  Wire.write(0x01);               // register address
  Wire.write(0x8583);             // ! Wire write uses a byte, not a word
  Wire.endTransmission();

  Wire.beginTransmission(0x48);
  Wire.write(0x02);   
  Wire.write(0x8000);             // !
  Wire.endTransmission();  

  Wire.beginTransmission(0x48);
  Wire.write(0x03);   
  Wire.write(0x7FFF);             // !
  Wire.endTransmission();        
}

void loop() {

  Wire.beginTransmission(0x48);
  Wire.write(0x85);               // select register 
//  Wire.write(0x83);             // ! no data ?
  Wire.endTransmission();

  Wire.beginTransmission(0x48);
  Wire.write(0x83);               // select register
  Wire.endTransmission();

  int n = Wire.requestFrom(0x48,2);
  if (n !=2){
    Serial.println("Error, ADS1015 did not detected bytes");
  }
  
  byte hiByte = Wire.read();
  byte loByte = Wire.read();
  int16_t c = word(hiByte, loByte);
  
  Serial.println(c);

  delay(1000);
}

Could you try a library ?
In the Arduino IDE is a library manager. When you search for "ADS1015" then there are four libraries and three from known good sources (Adafruit, Sparkfun, Rob Tillaart). When clicking on "More info" it brings you to the Github page with the source code. There you can see that they all use the same way to write and read the 16-bits registers.

1 Like

What I have done in the "void setup" part was reset the Conversion Register, Config Register, Lo_thresh register & Hi_thresh register.

What I wanted to do was read an analog reading only using "Wire.h" library. Early I have to use the Adafruit library for reading analog. But I am trying to read analog only using wire.h.

Then you have to write the equivalent of the existing libraries. Is there some reason one of the existing libraries won't work? If not pick one and get on with the rest of your project.

I have written my own support for some devices because existing libraries didn't do what I wanted, but that was an unusual case.

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.