B4J Question Using Raspberry Pi I2C with ADS1115 (Adafruit)

M4RC3L0

New Member
Licensed User
Longtime User
Hello,
I'm trying to communicate a raspberry pi 3 model B with ADS1115 through i2c communication,
but it is not working.

When I run an adafruit example in python, its works
perfectly.

Using and arduino Mega i can read what python example sends to ADS1115 and I tried to send the same commands with no success.

my B4J program always read a random value.
Can someone tell me where is the problem??

Thanks in advance.



Here is my actual code:
B4X:
Sub Button1_MouseClicked (EventData As MouseEvent)
    Dim bus As JavaObject = GetBus(1)
    Dim device As JavaObject = GetDevice(bus, 0x48)
  
    Write(device, 0x90)
    Write(device, 0x01)
    Write(device, 0x84)
    Write(device, 0x83)
    Write(device, 0x90)
    Write(device, 0x00)
    Write(device, 0x91)
  
    Sleep(100)  
    Dim buffer(2) As Byte
    Dim len As Int = Read2(device, buffer, 0, buffer.Length)
    Label1.Text = ""
    For i = 0 To (len - 1)
        Label1.Text = Label1.Text & buffer(i)
    Next
End Sub
 
Last edited:

M4RC3L0

New Member
Licensed User
Longtime User
Hello Erel, indirectly you helped me through other topics. I was able to do the communication and I would like to share a sample code for those who need it:

B4X:
#Region Project Attributes 
    #MainFormWidth: 1366
    #MainFormHeight: 768 
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private Button1 As Button
    Private Button2 As Button
    Private Label1 As Label
    Private Label2 As Label
   
    Dim Tini As Long
    Dim Contador As Long
   
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Principal") 'Load the layout file.
    MainForm.Show
   
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub



'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
'                                                               '
'                COMANDOS I2C PARA COMUNICACAO COM ADS1115      '
'                                                               '
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Sub GetBus(BusNumber As Int) As JavaObject
    Dim factory As JavaObject
    Return factory.InitializeStatic("com.pi4j.io.i2c.I2CFactory").RunMethodJO("getInstance", Array As Object(BusNumber))
End Sub
'Bus methods
Sub GetDevice(Bus As JavaObject, Address As Int) As JavaObject
    Return Bus.RunMethodJO("getDevice", Array As Object(Address))
End Sub

Sub CloseBus (Bus As JavaObject)
    Bus.RunMethod("close", Null)
End Sub

'Device methods
Sub Read(Device As JavaObject) As Int
    Return Device.RunMethod("read", Null)
End Sub

Sub Read2 (Device As JavaObject, Buffer() As Byte, Offset As Int, Size As Int) As Int
    Return Device.RunMethod("read", Array As Object(Buffer, Offset, Size))
End Sub

Sub Write(Device As JavaObject, b As Byte)
    Device.RunMethod("write", Array As Object(b))
End Sub

Sub Write2(Device As JavaObject, Buffer() As Byte, Offset As Int, Size As Int)
    Device.RunMethod("write", Array As Object(Buffer, Offset, Size))
End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''



Sub Button1_MouseClicked (EventData As MouseEvent) 'Read button

    Dim Timer1 As Timer
    Timer1.Initialize("Timer1", 1)
    Timer1.Enabled = True
    Tini = DateTime.Now
    Contador = 0
End Sub

Sub Timer1_Tick
    Dim bus As JavaObject = GetBus(1)
    Dim device As JavaObject = GetDevice(bus, 0x48) 'change this address if you have more than one ADS1115 connected.
   
    Dim rcvbuffer(2) As Byte
    Dim len As Int = Read2(device, rcvbuffer, 0, rcvbuffer.Length)
    Label1.Text = ""
    For i = 0 To (len - 1)
        rcvbuffer(i) = Bit.And(0xff, rcvbuffer(i)) 'Convert Signed(-128 to 127) byte to Unsigned (0 to 255)
    Next
    Dim Resultado As Int
    Resultado = (255* rcvbuffer(0)) + rcvbuffer(1) 'here you can treat the data as you need it.
    Label1.Text = Resultado

'''''' Measure samples per second '''''''''
    If DateTime.Now - Tini > 1000 Then
        Tini = DateTime.Now
        Label2.text = Contador
        Contador = 0
    Else
        Contador = Contador + 1   
    End If
''''''''''''''''''''''''''''''''''''''''''   
End Sub

Sub Button2_MouseClicked (EventData As MouseEvent) 'Config Button
    Dim bus As JavaObject = GetBus(1)
    Dim device As JavaObject = GetDevice(bus, 0x48)

    'The following settings should be made according to your needs by following the datasheet
    Write2(device, Array As Byte(0x01,0xC4,0xA3),0,3) '0x01 = Change to Config Register, 0xC4 = MSB of the Config register to be written, 0xA3 = LSB of the Config register to be written
    Sleep(1000) 
    Write2(device,Array As Byte(0x00),0,1) ' 0x00 = Change to Conversion Register.
End Sub


Thank you.


Marcelo S.
 
Upvote 0
Top