hideakitai MCP4728 DAC microchip with Arduino Mega

Hi :slight_smile: ,
I'm using the hideakitai arduino library for MCP4728 DAC microchip and controlling the different outgoing voltages with this library. I don't understand the code of the library so well, but I've managed to make it work. Is there anyone who can help me adjust the code so I can use an Arduino Mega's capability to control two of these chips (because it has two SDA and SCL ports) separately? Right now it's working with the Mega but both chips are getting the same reading, so instead of 8 channels I have double of each of the four channels. Hope that makes sense! GitHub - hideakitai/MCP4728: Arduino library for MCP4728 quad channel, 12-bit voltage output Digital-to-Analog Convertor with non-volatile memory and I2C compatible Serial Interface <- library

my code (that for now just sends a voltage to one channel):

#include <Wire.h>
#include "MCP4728.h"

MCP4728 dac;
int minutes = 60;
int analogPin = A1;

void setup()
{
Serial.begin(115200); // initialize serial interface for print()

Wire.begin();
dac.attatch(Wire, 14);
dac.readRegisters();

dac.selectVref(MCP4728::VREF::VDD, MCP4728::VREF::VDD, MCP4728::VREF::INTERNAL_2_8V, MCP4728::VREF::INTERNAL_2_8V);
dac.selectPowerDown(MCP4728::PWR_DOWN::GND_100KOHM, MCP4728::PWR_DOWN::GND_100KOHM, MCP4728::PWR_DOWN::GND_500KOHM, MCP4728::PWR_DOWN::GND_500KOHM);
dac.selectGain(MCP4728::GAIN::X1, MCP4728::GAIN::X1, MCP4728::GAIN::X2, MCP4728::GAIN::X2);
dac.analogWrite(MCP4728::DAC_CH::A, 0);
dac.analogWrite(MCP4728::DAC_CH::B, 0);
dac.analogWrite(MCP4728::DAC_CH::C, 0);
dac.analogWrite(MCP4728::DAC_CH::D, 0);

dac.enable(true);

dac.readRegisters();
delay(5000);
dac.analogWrite(MCP4728::DAC_CH::B, 3500);
delay(10000);

}

int cur = 3500;
void loop()
{
dac.analogWrite(MCP4728::DAC_CH::B,(cur));
int measurements = 100;
float average = 0.0;
while (measurements > 0){
float value = analogRead(A1)* 0.0049 / 0.2;
average += value;
delay(100);
measurements --;
}
Serial.println(average / 100.0);

}

That library only supports I2C/TWI/Wire address 0x60, the default address for that chip. If you want to use more than one chip you will need to set the addresses differently and use a library that supports multiple addresses.

The Arduino Mega only has 1 i2c bus. You can access it on pins 20/21 or the other breakout pins by AREF/GND, but they are connected to the same internal pins of the 2560 chip.

What you need to do is change the i2c address of your MCP4728 (default lower 3 bits are '000'). Read the datasheet about how to program one of them to respond to '001'. Do this while only that chip is on the i2c bus.

After that is done, you will have one device at address '1100 0000' and one device at '1100 0001'

Now you connect them both to you i2c bus and talk to each at their own address.

blh64:
Now you connect them both to you i2c bus and talk to each at their own address.

Except that the library only supports address 0x60, as I mentioned above.

johnwasser:
Except that the library only supports address 0x60, as I mentioned above.

I took a closer look at the library and there is a setId() function which changes the i2c address so you should be able to create two instances and set their address

You can write an example to have 8 DACS assign the address 61 to a second MCP4728 while a first MCP4728 is assigned the address 60?
thank you
Giovanni

I think it would start something like this:

#include <Wire.h>
#include "MCP4728.h"

MCP4728 dac60, dac61;
int minutes = 60;
int analogPin = A1;

void setup()
{
    Serial.begin(115200);  // initialize serial interface for print()

    dac61.setId(0x61);  // Tell dac61 to use address 0x61 instead of the default.

    Wire.begin();
    dac60.attatch(Wire, 14);
    dac60.readRegisters();

    dac61.attatch(Wire, 14);
    dac61.readRegisters();

Thanks for the support,
these days I do some tests and then I send you the news.
Giovanni