Cannot communicate with MCP4725 with smbus or Adafruit_MCP4725 library

I tried the exact sample code below from a sparkfruit tutorial and still no results.

# i2ctest.py
# A brief demonstration of the Raspberry Pi I2C interface, using the Sparkfun
# Pi Wedge breakout board and a SparkFun MCP4725 breakout board:
# https://www.sparkfun.com/products/8736

import smbus

# I2C channel 1 is connected to the GPIO pins
channel = 1

#  MCP4725 defaults to address 0x60
address = 0x60

# Register addresses (with "normal mode" power-down bits)
reg_write_dac = 0x40

# Initialize I2C (SMBus)
bus = smbus.SMBus(channel)

# Create a sawtooth wave 16 times
for i in range(0x10000):

    # Create our 12-bit number representing relative voltage
    voltage = i & 0xfff

    # Shift everything left by 4 bits and separate bytes
    msg = (voltage & 0xff0) >> 4
    msg = [msg, (msg & 0xf) << 4]

    # Write out I2C command: address, reg_write_dac, msg[0], msg[1]
    bus.write_i2c_block_data(address, reg_write_dac, msg)

I still see nothing but around 5V. I also replaced the line " bus.write_i2c_block_data(address, reg_write_dac, msg)" with “bus.write_byte_data(0x61, 0x00, 0x00)” and the output voltage doesn’t change at all.

I would believe 0v, that chip is a bit strange as it uses a “fast write” which doesn’t have a command register. Basically you want to send two bytes in a 12-bit MSB LSB. So it should just look like byte_array(0x61, 0x00, 0x00) as the only bytes going to the board.

I would think Python code that works with one MCP4725 would work with another. We don’t do anything strange, just expose it to the I2C bus.

It should set the voltage to 0v.

I can’t say I’ve ever used class attributes so I’m not 100% how the syntax/function in Python.

I don’t have one on my desk to test against.

What about sending bus.write_i2c_block_data(0x61, 0x00, bytearray(0x00, 0x00))

Can you link me the source code you’re looking at from Adafruit?

I will try your above suggestions. But here is the adafruit library

When I do the bus.write_i2c_block_data(0x61, 0x00, bytearray(0x00, 0x00)) I get an error.

TypeError: bytearray() argument 2 must be str, not int

Alright it looks like the Fast Write doesn’t work with Python. So we’ll have to use smbus.write_i2c_block_data(0x61, 0x60, bytearray([0x00, 0x00])

I forgot to add array brackets to the bytearray.

Here is an post where Scroix outlines this: Raspberry Pi 0-10V Board - libraries and code samples

Okay

I tried that command and I am getting a type error that says Third argument must be a list of at least one, but not more than 32 integers

Also is the second argument supposed to be 0x60 or 0x00?

I will take a look at that post.

I looked at what Scriox wrote. I used the call bus.write_i2c_block_data(0x61, 0x61, [0x00, 0x00]) which is what he did I just replaced the raw value stuff he had with 0 to change the DAC output to 0 volts. I was unsuccessful. It did however, get rid of the TypeError

while using this code


what did you set your raw_value ?

Thanks

I just used that exact code. I set my raw value to 0 and my output was 0. I then set my raw value to 2048 and still got 0. My DMM still says 5 V.

Could it possibly be my board?

set the value to 1024 and see whats the output value.

the output of the file is 0 for the raw ADC and 0 for the voltage when I set the voltage to 1024

did you measure the voltage using multimeter?

yes it is a constant 6.31 VDC since I gave power to the DAC.

I had the script print the data it is reading when I send it 1024 and it is [192, 0]

Not sure what i should expect it to be

looks like everything is working as it should. may be your read DAC value function is not working.
if i were you i will run the dac values from 0 to 4000 and read the output on meter.

My DMM still reads 6.33 when I iterate through.

I changed the lines set_voltage(1024) and time.sleep(DELAY)

to
i = 0
while i < 4001:
set_voltage(i)
time.sleep(DELAY)
i = i + 1

set the value to 2096 and see if it changes.

The reading the on the DMM did not change. It still reads 6.3 VDC. however, the reading from the dac changed. the data is [192, 48] causing a voltage of 0.02.

Hi,
i will recommending sending it back for further inspection.

Thanks

This library is really easy to use. The one you linked to is deprecated.
https://github.com/adafruit/Adafruit_CircuitPython_MCP4725/blob/master/examples/mcp4725_simpletest.py