Skip to content

Communicating with I2C module #129

@byunmlab

Description

@byunmlab

I am controlling an arduino uno with a windows pc to do some data collection. I am trying to connect an ADS1115 module to the I2C bus of the arduino in order to get higher resolution data but I am struggling to do this succesfully. Here is my code:

# %% imports
import numpy as numpy
from pyfirmata import Arduino, util, I2C_REPLY, I2C_REQUEST
import time
import inspect
from tqdm import tqdm

# Fix the deprecated part of the pyfirmata library
if not hasattr(inspect, 'getargspec'):
    inspect.getargspec = inspect.getfullargspec

# %% custom class

class MyBoard(Arduino):
    def __init__(self, port):
        super().__init__(port)
        self.add_cmd_handler(I2C_REPLY, self.handle_i2c)

    def send_i2c(self, data):
        self.send_sysex(I2C_REQUEST, data)

    def handle_i2c(self, *data):
        # TODO: handle i2c data
        print('16-bit ADC module: \t', data)

# %% setup
port = 'COM4'
board = MyBoard(port)

adc_address = 0x48

it = util.Iterator(board)
it.start()

board.analog[0].enable_reporting()

# setup i2c messaging
i2c_data = [
    I2C_REQUEST,
    0x48,
    0x00,
    I2C_REPLY
]


# %% Testing
count=15
loop = tqdm(total=count)

for i in range(count):
    # # TODO: read pot value 
    board.send_i2c(i2c_data)
    print('Arduino Built-in ADC: \t', board.analog[0].read())

    time.sleep(1)
    loop.update(1)

loop.close()

# %% exit
board.exit()

When I run it I get output that looks like this:

Arduino Built-in ADC: 0.3206
16-bit ADC module: (118, 0, 0, 0)
Arduino Built-in ADC: 0.3011
16-bit ADC module: (118, 0, 0, 0)
Arduino Built-in ADC: 0.2825
16-bit ADC module: (118, 0, 0, 0)
Arduino Built-in ADC: 0.2669
16-bit ADC module: (118, 0, 0, 0)
Arduino Built-in ADC: 0.2483
16-bit ADC module: (118, 0, 0, 0)
Arduino Built-in ADC: 0.2326
16-bit ADC module: (118, 0, 0, 0)
Arduino Built-in ADC: 0.2151
16-bit ADC module: (118, 0, 0, 0)
Arduino Built-in ADC: 0.1984
16-bit ADC module: (118, 0, 0, 0)
Arduino Built-in ADC: 0.1896
16-bit ADC module: (118, 0, 0, 0)
Arduino Built-in ADC: 0.1779
16-bit ADC module: (118, 0, 0, 0)
Arduino Built-in ADC: 0.1623
16-bit ADC module: (118, 0, 0, 0)
Arduino Built-in ADC: 0.1496
16-bit ADC module: (118, 0, 0, 0)
Arduino Built-in ADC: 0.1339
16-bit ADC module: (118, 0, 0, 0)
Arduino Built-in ADC: 0.1202
16-bit ADC module: (118, 0, 0, 0)
Arduino Built-in ADC: 0.1036
16-bit ADC module: (118, 0, 0, 0)

As you can see, as I twist the potentiometer that I have connected the value reading with the arduinos ADC is working correctly but from the I2C I am just getting the same tuple everytime, (118, 0, 0, 0). I have looked over issue #37 but I have not been able to solve my problems yet. Thanks for any help

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions