I'm trying to figure out if it is possible to use 2 MCP4728 at the same time in order to have a 8 DAC with an Arduino UNO R3.
I've understood that is a i2c problem but can't figure it out, please help!
This is the working code that I'm using for one of them:
// Basic demo for configuring the MCP4728 4-Channel 12-bit I2C DAC
#include <Adafruit_MCP4728.h>
#include <Wire.h>
Adafruit_MCP4728 mcp;
int inByte = 0;
char buffer[40];
int index = 0;
int value = 0;
int CV1 = 0;
int CV2 = 0;
int CV3 = 0;
int CV4 = 0;
void setup(void) {
Serial.begin(115200);
while (!Serial)
delay(10); // will pause Zero, Leonardo, etc until serial console opens
Serial.println("Adafruit MCP4728 test!");
// Try to initialize!
if (!mcp.begin(0x64)) {
Serial.println("Failed to find MCP4728 chip");
while (1) {
delay(10);
}
}
}
void loop() {
index = 0;
do
{
buffer[index] = Serial.read();
if(buffer[index] !=-1) index = index+1;
}while(buffer[index-1]!=32);
value = atoi(buffer);
if(value >= 0 && value <= 4095){ //0 to 4095
CV1 = value;
mcp.setChannelValue(MCP4728_CHANNEL_A, CV1);
}
if(value >= 4096 && value <= 8191){ //0 to 4095
CV2 = value - 4096;
mcp.setChannelValue(MCP4728_CHANNEL_B, CV2);
}
if(value >= 8192 && value <= 12287){ //0 to 4095
CV3 = value - 8192;
mcp.setChannelValue(MCP4728_CHANNEL_C, CV3);
}
if(value >= 12288 && value <= 16383){ //0 to 4095
CV4 = value - 1288;
mcp.setChannelValue(MCP4728_CHANNEL_D, CV4);
}
delay(5);
}
I've found this on the forum:
"According to the data sheet for the MCP4728, you can configure these devices with different I2C addresses. Standard is 0b1100000 (seven bit address) but you can set the last 3 bits.
That means you could have up to 8 of these devices on the same I2C bus"
You need to have a different I2C address for each module. It's set in the internal EEPROM of the IC.
You can reprogram the I2C address of one module by connecting to it and sending some commands. It's explained in the datasheet of the IC.
In the adafruit modules the default I2C address, in some versions at least, it's printed in the back.
As of Aug 1, 2022 - we may make this board with the MCP4728 (default I2C address 0x60) or MCP4728A4 (default address 0x64).
If you get two with different addresses it should work. Otherwise you can study the datasheet and try to reprogram one of them.
Edit: The link in the post #6 could be the solution.
Thanks everyone for the help, really appreciate it!
So I've tried what is suggested by cedarlakeinstruments in the post #6 but with no tangible results.
I'm quite new and still developing my knowledge and this seems quite complicate for me at the moment.
Can anyone please help me to understand how to combine my code with the suggested one?
Thanks again!
I haven't used that project, but my understanding is that they are not combined.
First, you run the address changer to set a different address on the MCP4728. Now that you've done that, your original program can use two of them on the same I2C bus since you can now access them separately by address. The address changer program isn't needed again unless you want to make further addressing updates.
Thanks you all for the support!
I've finally succeed to change the address of one of the two MCP4728 with the code suggested in post#6.
So now I've two MCP4728 with different address running correctly on the same i2c bus!