jajberni / MCP342x_LoPy

MicroPython driver for the MCP342x ADC.
MIT License
3 stars 3 forks source link

RPi pico and MCP3424 #1

Closed kazemiprince closed 1 year ago

kazemiprince commented 1 year ago

I have an issue when using RPi pico (microPython and MCP3424) when it's trying to convert_and_read, it gave an error as below: object with buffer protocol required

below is my code:

from machine import Pin, I2C
from MCP342x import MCP342x
import time

sensor_type = "None"

try:
    i2c = I2C(0 , sda=Pin(4), scl=Pin(5), freq=100000)
    #
    #addr68_ch0 = MCP342x(i2c, 0x68, channel=0, resolution=18, gain=8,
    #                     scale_factor=1000.0)
    addr68_ch0 = MCP342x(i2c, address=0x68,
           device='MCP3424',
           channel=0,
           gain=1,
           resolution=18,
           continuous_mode=False,
           scale_factor=1.0,
           offset=0.0)

    time.sleep(1)
    print('Ready to read ADC')
except Exception as error:
    print("Err in init",error)
    pass

while True:
    adc_values = [-1.0, -1.0, -1.0, -1.0]
    try:
        adc_values[0] = addr68_ch0.convert_and_read()

    except Exception as error:
        print("err in reading",error)

    print("ADC:{ch0},{ch1},{ch2},{ch3}".format(
        sensor_type=sensor_type, ch0=adc_values[0], ch1=adc_values[1],
        ch2=adc_values[2], ch3=adc_values[3]))

    time.sleep(1.0)
kazemiprince commented 1 year ago

Solved

convert function should change as below:

    def convert(self):
        """Initiate one-shot conversion.
        The current settings are used, with the exception of continuous mode."""
        c = self.config
        c &= (~MCP342x._continuous_mode_mask & 0x7f)  # Force one-shot
        c |= MCP342x._not_ready_mask                  # Convert
        #print('Convert ' + hex(self.address) + ' config: ' + bin(c))
        data_ = bytearray(1)
        data_[0] = c
        self.i2c.writeto(self.address, data_)
Bambi1222 commented 1 year ago

Does this library work with https://www.digikey.pl/en/products/detail/microchip-technology/MCP3424-E-SL/1680890 ADC? Because i cant find anything related to thie PyCom board mentioned in the description.