ADS1015 the ADC with I2C InterfacesteemCreated with Sketch.

in #electronics7 years ago (edited)

In this post I will explain how we can use the ADC with arduino without using libraries that greatly reduce sampling speed. I am doing a project that involves reading analog data but the microcontroller I am using does not have a digital analog converter (ADC) so I decided to order a module based on ADS1015 an ADC with I2C interface.

My initial idea was to use the library that Adafruit developed for ADS1015 and ADS1115 however after using it and studying the code I realized that the library needed a lot of work but it was still my first choice to test the ADC.
After adding Adafruit_ADS1X15 to Arduino and trying out the sample code I was happy to be working fine and getting data quite accurately. I used the circuit below.
Untitled Sketch_bb.png
For my project I needed the highest sampling speed possible and after studying the code Adafruit knew that it had to take at least 1ms for each sample because it was included in the conversion function a "delay (1)" to wait for the conversion was finished. To my surprise the sampling time was 4ms which was unacceptable to me.
I decided to discard the Adafruit library and decided to use the Soligen2010 fork which seemed to me to be a more developed library. And after studying the code, I realized that, despite being a fork in the Adafruit library, the code had been more worked on. Unfortunately after installing the library I could not get the ADC to work. However I did not fold my arms, I discarded all the libraries and created my own code based on the mentioned libraries and the Datasheet of the ADS1015. I created a code that makes 10 readings of two different channels (A0 and A1) and shows the result of the readings of the ADC and the time that was executed. The code is set out below.

#include <Wire.h>
int16_t i2caddress=0x48;
int8_t inter=0;
uint16_t leitura0, leitura1, leitura2, leitura3,
 leitura4, leitura5, leitura6, leitura7, leitura8, leitura9;
uint16_t mimi0, mimi1, mimi2, mimi3, 
mimi4, mimi5, mimi6, mimi7,mimi8, mimi9;

uint16_t config0 = 0x0003  | // NOT Assert ALERT/RDY after one conversions
                   0x0000  | // NOT Latching Comparator
                   0x0000  | // Alert/Rdy active low   (default val)
                   0x0000  | // Traditional comparator (default val)
                   0x00C0  | // 3300 samples per second (default)
                   0x0100  | // Single-shot mode (default)
                   0x0200  | // +/-4.096V range = Gain 1
                   0x4000  | // Single-ended AIN0
                   0x8000;   // Set 'start single-conversion' bit
uint16_t config1 = 0x0003  | // NOT Assert ALERT/RDY after one conversions
                   0x0000  | // NOT Latching Comparator
                   0x0000  | // Alert/Rdy active low   (default val)
                   0x0000  | // Traditional comparator (default val)
                   0x00C0  | // 3300 samples per second (default)
                   0x0100  | // Single-shot mode (default)
                   0x0200  | // +/-4.096V range = Gain 1
                   0x5000  | // Single-ended AIN1
                   0x8000;   // Set 'start single-conversion' bit

  
void setup(){
  Wire.begin();
  Wire.setClock(400000);
  Serial.begin(115200);
    while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  delay(5000);
  Serial.println("START"); Serial.print("\n");
  
    leitura0=fazer_leitura_ADC(config0);
    mimi0=micros();
    leitura1=fazer_leitura_ADC(config1);
    mimi1=micros();
    leitura2=fazer_leitura_ADC(config0);
    mimi2=micros();
    leitura3=fazer_leitura_ADC(config1);
    mimi3=micros();
    leitura4=fazer_leitura_ADC(config0);
    mimi4=micros();
    leitura5=fazer_leitura_ADC(config1);
    mimi5=micros();
    leitura6=fazer_leitura_ADC(config0);
    mimi6=micros();
    leitura7=fazer_leitura_ADC(config1);
    mimi7=micros();
    leitura8=fazer_leitura_ADC(config0);
    mimi8=micros();
    leitura9=fazer_leitura_ADC(config1);
    mimi9=micros();
    Serial.print("ADC0: "); Serial.print(leitura0); Serial.print (" at microsegundo "); 
    Serial.println(mimi0); Serial.print("\n");
    Serial.print("ADC1: "); Serial.print(leitura1); Serial.print (" at microsegundo ");
    Serial.println(mimi1); Serial.print("\n");
    Serial.print("ADC0: "); Serial.print(leitura2); Serial.print (" at microsegundo ");
    Serial.println(mimi2); Serial.print("\n");
    Serial.print("ADC1: "); Serial.print(leitura3); Serial.print (" at microsegundo "); 
    Serial.println(mimi3); Serial.print("\n");
    Serial.print("ADC0: "); Serial.print(leitura4); Serial.print (" at microsegundo "); 
    Serial.println(mimi4); Serial.print("\n");
    Serial.print("ADC1: "); Serial.print(leitura5); Serial.print (" at microsegundo ");
    Serial.println(mimi5); Serial.print("\n");
    Serial.print("ADC0: "); Serial.print(leitura6); Serial.print (" at microsegundo ");
    Serial.println(mimi6); Serial.print("\n");
    Serial.print("ADC1: "); Serial.print(leitura7); Serial.print (" at microsegundo "); 
    Serial.println(mimi7); Serial.print("\n");
    Serial.print("ADC0: "); Serial.print(leitura8); Serial.print (" at microsegundo ");
 Serial.println(mimi8); Serial.print("\n");
    Serial.print("ADC1: "); Serial.print(leitura9); Serial.print (" at microsegundo ");
 Serial.println(mimi9); Serial.print("\n");
   
}

void escrever_registo_ADC(uint8_t address, uint16_t registo) { // Write ADC
  Wire.beginTransmission(address);
  Wire.write(0x01);
  Wire.write((uint8_t)(registo>>8));
  Wire.write((uint8_t)(registo & 0xFF));
  Wire.endTransmission();
}
uint16_t ler_ADC(uint8_t address, uint8_t reg) { // Read ADC
  Wire.beginTransmission(address);
  Wire.write(reg);                       // ADS1015_REG_POINTER_CONVERT
  Wire.endTransmission();
  Wire.requestFrom(address, (uint8_t)2);
  return ((Wire.read() << 8) | Wire.read());
}

// Make a reading
uint16_t fazer_leitura_ADC(uint16_t config) { 
   //Write config register to the ADC - endereço com ADDR=GND ,
  // ADS1015_REG_POINTER_CONFIG
  escrever_registo_ADC(i2caddress, config); 
  delayMicroseconds(20);
  // Stop when the config register OS bit changes to 1 
  while ((ler_ADC(i2caddress, 0x01) & 0x80) == 0);  
  // Read the conversion results - endereço com ADDR=GND
  return (ler_ADC(i2caddress, 0x00) >> 4); 
}

void loop() {
  }

The result was the following:

Capturar.PNG
With this code I was able to reduce the sampling time of 4ms with the Adafruit library to just over 0.5ms. With this time I can work.

Sort:  

Congratulations! This post has been upvoted by SteemMakers. We are a community based project that aims to support makers and DIYers on the blockchain in every way possible. Find out more about us on our website: www.steemmakers.com

If you like our work, please consider upvoting this comment to support the growth of our community. Thank you.

Congratulations @rpsreal! You have received a personal award!

1 Year on Steemit
Click on the badge to view your Board of Honor.

Do not miss the last post from @steemitboard:

SteemFest3 and SteemitBoard - Meet the Steemians Contest

Support SteemitBoard's project! Vote for its witness and get one more award!

Congratulations @rpsreal! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Do not miss the last post from @steemitboard:

SteemFest Meet The Stemians Contest - The mysterious rule revealed
Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.22
TRX 0.21
JST 0.035
BTC 98789.27
ETH 3346.59
USDT 1.00
SBD 3.08