B4R Question Wishing library for ADS1115 (16-bit 4channel ADC)...

rbghongade

Active Member
Licensed User
Longtime User
Dear friends,
I request someone to create a library for ADS1115 , a 4 channel 16-bit I2C ADC. I have tested it with WeMos in Arduino IDE. Attaching herewith the sketch and master from github.
 

Attachments

  • Adafruit_ADS1X15-master.zip
    12.4 KB · Views: 429
  • ADS1115.zip
    867 bytes · Views: 474
Last edited:

rbghongade

Active Member
Licensed User
Longtime User
Tried this code but did not succeed.
B4X:
Sub Process_Globals
   Public Serial1 As Serial
   Private wire As WireMaster
 
   Private raf As RandomAccessFile
   Private const MPU As Int = 0x48
   Private timer1 As Timer
End Sub

Private Sub AppStart
   Serial1.Initialize(115200)
   Log("AppStart")
 
   wire.Initialize
   timer1.Initialize("timer1_Tick",1000)
   timer1.Enabled=True
 
 
End Sub

Sub timer1_tick
    wire.WriteTo2(MPU,True, Array As Byte(0x01))
       wire.WriteTo2(MPU,True, Array As Byte(0xc2))'continuous mode , single ended input
       wire.WriteTo2(MPU,True, Array As Byte(0xe3))
    
 
  Dim t As Byte
   t=0
  Do While t=0  'checking for conversion completion
    
    wire.WriteTo2(MPU,True, Array As Byte(0x01))
   Dim c() As Byte = wire.RequestFrom(MPU, 2)
   t=Bit.Get(c(0),7)
   Log("conv status:",t)

  Loop
 

   wire.WriteTo(MPU, Array As Byte(0x00))
   Dim b() As Byte = wire.RequestFrom(MPU, 2)
   If b.Length = 2 Then
     raf.Initialize(b, False)
    Dim ch1 As Long = raf.Readuint16(raf.CurrentPosition)
'   
     Log("byte0 ",b(0))
     Log("byte1 ",b(1))
  
     Log(ch1)
   Else
     Log("Data Not Available...")
   End If
 
 
End Sub

Interestingly , after I had tested it with Adruino IDE and the sketch attached in the earlier post , the last value is retrieved by the above code! But thereafter it remains same. If I power down the ADS1115 , I get a zero value! However it indicates that the ESP8266 is communicating with the I2C slave ADS1115!
 
Upvote 0

rbghongade

Active Member
Licensed User
Longtime User
SOLVED:

B4X:
Sub Process_Globals
   Public Serial1 As Serial
   Dim adc_ch0 As Long
   Private timer1 As Timer
   Public voltage As Double
End Sub

Private Sub AppStart
   Serial1.Initialize(115200)
   Log("AppStart")
  
   timer1.Initialize("timer1_Tick",1000)
   timer1.Enabled=True
   RunNative("setup",Null)
    Delay(100)
  
End Sub

Sub timer1_tick
   
    RunNative("adc_0",0)
    Delay(100)
   
   voltage=adc_ch0*0.1875/1000
   Log(voltage)
End Sub

#if C
#include <Wire.h>
#include <Adafruit_ADS1015.h>
Adafruit_ADS1115 ads;
void setup(B4R::Object* o){
ads.begin();
}
void adc_0 (B4R::Object* o) {
   b4r_main::_adc_ch0 = ads.readADC_SingleEnded(o->toULong());
}

#End if
 
Last edited:
Upvote 0

JMB

Active Member
Licensed User
Longtime User
Ok, I managed to get the ADS1115 working with a Wemos D1 Mini board using the Arduino sketch kindly supplied by rbghongade.

I am not sure if I put things in the right place, but I coped the Adafruit_ADS1015.h and the Adafruit_ADS1015.h files into the folder containing the ADS1115 sketch, and just had to change one line to get it to work:

Line 2 of the ADS1115 sketch was this:
B4X:
#include <Adafruit_ADS1015.h>

which gave a compile error.

Changing it to this:

B4X:
#include "Adafruit_ADS1015.h"

got it working, with ADDR tied to Ground so the address of the ADS1115 is 0x48h

Just working on getting it going with B4R now.

JMB

Edited:

I realise now that is because I should have put the adafruit files into arduino/libraries...
 
Last edited:
Upvote 0

JMB

Active Member
Licensed User
Longtime User
I am a little bit confused. I put the Adafruit files into a sub folder in arduino/libraries, changed my code back to using <> and now when I compile using the library, I get lots of "multiple definition" errors.

When I use "" then I don't get the problem, but this seems like cheating a bit - I want to do things the standard way, but I clearly haven't set it up correctly.

I did manage to get the inline C code provided by rbghongade running which is very nice!

I will look at how to write a wrapper round that as an experiment, but if anyone can help me out with the <> "" dilemma that would be great.

JMB
 
Upvote 0

JMB

Active Member
Licensed User
Longtime User
Hi Erel,

I recreated the project, and the problem disappeared.

Thanks.

JMB
 
Upvote 0
Top