JakeQuinn
Posts: 17
Joined: Thu Oct 24, 2019 11:31 pm

Get Reading from ADS1115

Fri Oct 25, 2019 12:57 am

Hello I am trying to compare the speed of how many data samples can i get from ADS1115 between python and C++. So far i have tested the python using Adafruits library. Now i am looking for a sample code in C++ to take a reading but i could not find a very basic one. I have seen that the wiringPi library has a ads1115.h, but i could not find documentation of a sample code.

salvato
Posts: 97
Joined: Tue Jan 15, 2013 9:21 pm

Re: Get Reading from ADS1115

Fri Oct 25, 2019 9:36 am

Hi,

I have written a class (it is written for a Qt program but not too difficult to modify) for the ADS1115.
give a look at:
https://github.com/salvato/TControllerR ... ds1115.cpp
and
https://github.com/salvato/TControllerR ... /ads1115.h

You can instantiate an ADS1115 object like this:

Code: Select all

pADC = new ADS1115(QString("/dev/i2c-1"), 0x48);
and ... to read a value:

Code: Select all

double newValue = pADC->ReadData();
Hope this help.

dsyleixa123
Posts: 809
Joined: Mon Jun 11, 2018 11:22 am

Re: Get Reading from ADS1115

Fri Oct 25, 2019 12:49 pm

heya,
@salvato: thanks for your contributions!

2 questions:
what does newValue = pADC.ReadData() do?
And why is it double? actually it's a 16-bit ADC...?!

I actually expected functions like

Code: Select all

int16_t  adc0, adc1, adc2, adc3;

  adc0 = pADC.readADC(0);
  adc1 = pADC.readADC(1);
  adc2 = pADC.readADC(2);
  adc3 = pADC.readADC(3);
Last edited by dsyleixa123 on Fri Oct 25, 2019 1:00 pm, edited 2 times in total.

dsyleixa123
Posts: 809
Joined: Mon Jun 11, 2018 11:22 am

Re: Get Reading from ADS1115

Fri Oct 25, 2019 12:54 pm

I double the OP's question:

where are example source codes for the wiringPi ads1115.h lib? ("unofficially" forked at https://github.com/WiringPi/WiringPi/bl ... /ads1115.c)

LdB
Posts: 1549
Joined: Wed Dec 07, 2016 2:29 pm

Re: Get Reading from ADS1115

Fri Oct 25, 2019 1:40 pm

I2C is pretty straight forward on the linux device API

run Raspi_Config and turn on the I2C, reboot and then you can access it via "/dev/i2c-1"

Code: Select all

int fd = open("/dev/i2c-1", O_RDWR));

I just started from amaork's code and its pretty basic C/C++
https://github.com/amaork/libi2c/blob/master/src/i2c.c

If you have basic C/C++ ability and know the device address layout it shouldn't be hard.

dsyleixa123
Posts: 809
Joined: Mon Jun 11, 2018 11:22 am

Re: Get Reading from ADS1115

Fri Oct 25, 2019 1:59 pm

and where are example source codes for the wiringPi ads1115.h lib?

LdB
Posts: 1549
Joined: Wed Dec 07, 2016 2:29 pm

Re: Get Reading from ADS1115

Fri Oct 25, 2019 2:22 pm

There is no requirement for wiringPi done that way.

It comes down to do you know basic C/C++ coding.

If you are a novice and need wiringPi then probably drop Gordon a message on his site, I am not sure how often he calls thru here.

dsyleixa123
Posts: 809
Joined: Mon Jun 11, 2018 11:22 am

Re: Get Reading from ADS1115

Fri Oct 25, 2019 3:10 pm

I do not want to hijack the OP's topic but I personally would prefer to know how to use the wiringPi ads1115.h lib
On Gordon's website I can find just examples about MCP and PCF ICs.

User avatar
DougieLawson
Posts: 38798
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Get Reading from ADS1115

Fri Oct 25, 2019 4:28 pm

LdB wrote:
Fri Oct 25, 2019 2:22 pm
If you are a novice and need wiringPi then probably drop Gordon a message on his site, I am not sure how often he calls thru here.
That won't work and I suspect he's given up with the forum.
http://wiringpi.com/wiringpi-deprecated/
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

LdB
Posts: 1549
Joined: Wed Dec 07, 2016 2:29 pm

Re: Get Reading from ADS1115

Fri Oct 25, 2019 5:17 pm

Thanks for that Dougie and I sympathize some of Gordon's issues ... I baulked at telling them how to do it for exactly some of those reasons :-)

I was feeding into his problems by trying to send him more newbies.

To any moderate skill level programmer It's obvious what to do and how it works. But yeah problems with newbies and they abuse the situation (the old why should I read and learn when you know).

dsyleixa123
Posts: 809
Joined: Mon Jun 11, 2018 11:22 am

Re: Get Reading from ADS1115

Sat Oct 26, 2019 8:07 am

The objective of "Wiring" was to make it easy for artists, designers, and beginners to work with electronics, by abstracting away the often complicated details of electronics so they can focus on their own objectives
https://arduinohistory.github.io/
http://people.interactionivrea.org/h.ba ... ow_res.pdf
So what is wrong about asking how to use the ads1115 lib of WiringPi? And as it already exists: about where to find example codes how to use it?

User avatar
DougieLawson
Posts: 38798
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Get Reading from ADS1115

Sat Oct 26, 2019 8:53 am

dsyleixa123 wrote:
Sat Oct 26, 2019 8:07 am
So what is wrong about asking how to use the ads1115 lib of WringPi [sic]? And as it already exists: about where to find example codes how to use it?
Because it's now, effectively, unsupported and unsupportable.

If you use WiringPi, you're on your own. When it breaks you get the bugs to add to your collection.

Code: Select all

#include <wiringPi.h>
#include <ads1115.h>
#define PINBASE 120

void main(int argc, char* argv[])
{

 ads1115Setup (PINBASE, 0x48);  
 int analog1 = analogRead(2);
}
 
This code is 100% untested (I don't have the hardware), based on reading the instructions at http://wiringpi.com/extensions/
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

dsyleixa123
Posts: 809
Joined: Mon Jun 11, 2018 11:22 am

Re: Get Reading from ADS1115

Sat Oct 26, 2019 10:22 am

perhaps my ads1115 IC is meanwhile damaged, but I always get readings =0, at either ads1115 ADC pin:

Code: Select all

// code still not working yet, deleted not to confuse people...
PS, edit:
wiringPi is also forked on github, so if one once had a working example then one might submit a pull request! 8-)
Last edited by dsyleixa123 on Sun Oct 27, 2019 9:20 am, edited 1 time in total.

User avatar
DougieLawson
Posts: 38798
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Get Reading from ADS1115

Sat Oct 26, 2019 11:07 am

dsyleixa123 wrote:
Sat Oct 26, 2019 10:22 am
wiringPi is also forked on github, so if one once had a working example then one might submit a pull request! 8-)
It's never been on Github, so that's clearly been done by someone who has helped to contribute to the demise of wiringPi. It is bound to be an old version that won't be supported by the person who stuffed it on Github and likely won't work on an RPi4.

Choose any other library or write your own code using linux/i2c-dev.h and /dev/i2c0-1.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

dsyleixa123
Posts: 809
Joined: Mon Jun 11, 2018 11:22 am

Re: Get Reading from ADS1115

Sat Oct 26, 2019 1:30 pm

DougieLawson wrote:
Sat Oct 26, 2019 11:07 am
dsyleixa123 wrote:
Sat Oct 26, 2019 10:22 am
wiringPi is also forked on github, so if one once had a working example then one might submit a pull request! 8-)
It's never been on Github, so that's clearly been done by someone who has helped to contribute to the demise of wiringPi. It is bound to be an old version that won't be supported by the person who stuffed it on Github and likely won't work on an RPi4.

Choose any other library or write your own code using linux/i2c-dev.h and /dev/i2c0-1.
[offtopic]
it's been forked by a 3rd party, yes, but quite up-to-date though.
OTOH, all the linux/i2c-dev.h and /dev/i2c0-1 stuff and even pigpio is far to complicated for non-professionals.
So IMO WiringPi should be updated at github further on though - unless someone once creates a new "Wiring" based, Arduino-like C++ GPIO lib, but that's another topic
[/offtopic]

anyway, perhaps someone else could provide or link to a tested, working ads1115-wiringPi example.
Last edited by dsyleixa123 on Sat Oct 26, 2019 1:36 pm, edited 2 times in total.

dsyleixa123
Posts: 809
Joined: Mon Jun 11, 2018 11:22 am

Re: Get Reading from ADS1115

Sat Oct 26, 2019 1:34 pm

PS,
why is in your code

analog1 = analogRead(2);

and not

analog1 = analogRead(1);

:?:

User avatar
DougieLawson
Posts: 38798
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Get Reading from ADS1115

Sat Oct 26, 2019 1:50 pm

dsyleixa123 wrote:
Sat Oct 26, 2019 1:34 pm
PS,
why is in your code

analog1 = analogRead(2);

and not

analog1 = analogRead(1);

:?:
Because I was testing that I'd read Gordon's code correctly. I don't have the hardware so had to read ads1115.h and ads1115.c to see how it worked (my example does compile OK).
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

dsyleixa123
Posts: 809
Joined: Mon Jun 11, 2018 11:22 am

Re: Get Reading from ADS1115

Sat Oct 26, 2019 1:53 pm

yes, my example in which I reworked your example also compiles fine, but nonetheless, as already stated, I just get repetitive readings = 0 at either ADS1115 ADC pin though.

dsyleixa123
Posts: 809
Joined: Mon Jun 11, 2018 11:22 am

Re: Get Reading from ADS1115

Sat Oct 26, 2019 1:57 pm

GOT IT!

it must be
analog1 = analogRead(PINBASE+1); // connected to ads1115 A1

dsyleixa123
Posts: 809
Joined: Mon Jun 11, 2018 11:22 am

Re: Get Reading from ADS1115

Sat Oct 26, 2019 2:00 pm

so this is the tested, functioning ads1115 code using wiringPi:

Code: Select all

// ads1115 example wiringPi
 

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <inttypes.h>
#include <stdbool.h>

#include <wiringPi.h>
#include <wiringPiI2C.h>
#include <ads1115.h>


#define  PINBASE  120    // choose  PINBASE  value arbitrarily

#define  ADS_ADDR 0x48



int main() {
   int analog1; 
  
   int ioerr = wiringPiSetupGpio();   
     if( ioerr == -1 ) return 1;  
    
   ads1115Setup (PINBASE, ADS_ADDR);    
    
   while(true) {        
       analog1 = analogRead(PINBASE+1);    // connected to ads1115 A1
       printf("analog1 = %d \n", analog1);
       
       delay(250);
   }
    
     
  
  return 0;
  }

  
thanks a lot for your contributions! 8-)

aminka
Posts: 8
Joined: Tue Jun 30, 2020 6:34 pm

Re: Get Reading from ADS1115

Tue Jun 30, 2020 6:53 pm

Could some one please write a simple example for ads1115.h (wiringPi) to read measures from 4 sensors in a while loop with set a sample rate and other configs ?
Thank you very much .

dsyleixa123
Posts: 809
Joined: Mon Jun 11, 2018 11:22 am

Re: Get Reading from ADS1115

Wed Jul 01, 2020 7:26 am

Actually it's basically my code from above!
So for all 4 ADS1115 sensors:

Code: Select all

// ads1115 example wiringPi
 
// my common #includes
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <inttypes.h>
#include <stdbool.h>

#include <wiringPi.h>
#include <wiringPiI2C.h>
#include <ads1115.h>


#define  PINBASE  120    // choose  PINBASE  value arbitrarily

#define  ADS_ADDR 0x48



int main() {
   int analog0,analog1,analog2,analog3; 
  
   int ioerr = wiringPiSetupGpio();   
     if( ioerr == -1 ) return 1;  
    
   ads1115Setup (PINBASE, ADS_ADDR);    
    
   while(true) {        
        analog0 = analogRead(PINBASE+0);      // connected to ads1115 A0
          printf("analog0 = %d \n", analog0);
	analog1 = analogRead(PINBASE+1);    // connected to ads1115 A1
          printf("analog1 = %d \n", analog1);
        analog2 = analogRead(PINBASE+2);    // connected to ads1115 A2
          printf("analog2 = %d \n", analog2);
        analog3 = analogRead(PINBASE+3);    // connected to ads1115 A3
          printf("analog3 = %d \n\n", analog3);
       
       delay(250);  // sample rate 250ms
   } 
  
  return 0;
  }

aminka
Posts: 8
Joined: Tue Jun 30, 2020 6:34 pm

Re: Get Reading from ADS1115

Thu Jul 02, 2020 4:53 pm

dsyleixa123 wrote:
Wed Jul 01, 2020 7:26 am
Actually it's basically my code from above!
So for all 4 ADS1115 sensors:

Code: Select all

// ads1115 example wiringPi
 
// my common #includes
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <inttypes.h>
#include <stdbool.h>

#include <wiringPi.h>
#include <wiringPiI2C.h>
#include <ads1115.h>


#define  PINBASE  120    // choose  PINBASE  value arbitrarily

#define  ADS_ADDR 0x48



int main() {
   int analog0,analog1,analog2,analog3; 
  
   int ioerr = wiringPiSetupGpio();   
     if( ioerr == -1 ) return 1;  
    
   ads1115Setup (PINBASE, ADS_ADDR);    
    
   while(true) {        
        analog0 = analogRead(PINBASE+0);      // connected to ads1115 A0
          printf("analog0 = %d \n", analog0);
	analog1 = analogRead(PINBASE+1);    // connected to ads1115 A1
          printf("analog1 = %d \n", analog1);
        analog2 = analogRead(PINBASE+2);    // connected to ads1115 A2
          printf("analog2 = %d \n", analog2);
        analog3 = analogRead(PINBASE+3);    // connected to ads1115 A3
          printf("analog3 = %d \n\n", analog3);
       
       delay(250);  // sample rate 250ms
   } 
  
  return 0;
  }
Thank you very much bro,did you test above code ? what about sample rate and gain configuring and how to use ALRT/READY pin ? And why we start PIN-BASE at 120 ?
Other question is how to purchase or order onboard memory raspberry pi for save our code privet ?
Very very thanks .

dsyleixa123
Posts: 809
Joined: Mon Jun 11, 2018 11:22 am

Re: Get Reading from ADS1115

Thu Jul 02, 2020 6:34 pm

start pin 120 is arbitrary, wiringPi is pin-based and needs just a unique start pin.
Of course I tested the code, it works like a charm and even identical to the console program above when I use it in qt5.
About the rest of your questions I cannot give a statement, because I won't ever need to use it.

aminka
Posts: 8
Joined: Tue Jun 30, 2020 6:34 pm

Re: Get Reading from ADS1115

Fri Jul 03, 2020 12:12 pm

dsyleixa123 wrote:
Thu Jul 02, 2020 6:34 pm
start pin 120 is arbitrary, wiringPi is pin-based and needs just a unique start pin.
Of course I tested the code, it works like a charm and even identical to the console program above when I use it in qt5.
About the rest of your questions I cannot give a statement, because I won't ever need to use it.
Ok thanks, but how to configure sample rate ? And for this code (your code) what is the sample rate? 860 sps ?

Return to “C/C++”