#include <Wire.h>
#define DAC_ADDRESS_1 0x60 // MCP4725 address for DAC1
#define DAC_ADDRESS_2 0x61 // MCP4725 address for DAC2
#define DAC_ADDRESS_3 0x62 // MCP4725 address for DAC3
void setup() {
Wire.begin(); // initialize I2C bus
}
void loop() {
// send values to DACs
writeDAC(DAC_ADDRESS_1, 1023); // set DAC1 to maximum value (1023)
writeDAC(DAC_ADDRESS_2, 512); // set DAC2 to half of maximum value
writeDAC(DAC_ADDRESS_3, 0); // set DAC3 to minimum value (0)
delay(1000); // wait for 1 second before changing values
}
void writeDAC(byte address, int value) {
byte msb = value >> 8; // get the most significant byte of the value
byte lsb = value & 0xFF; // get the least significant byte of the value
// write the value to the DAC
Wire.beginTransmission(address);
Wire.write(64); // send control byte
Wire.write(msb); // send most significant byte of value
Wire.write(lsb); // send least significant byte of value
Wire.endTransmission();
}
All three DAC will have the same address. This is not allowed on the i2c bus. The only way they can have different addresses is if you buy the chips direct from the manufacturer. You would need to buy probably 10,000 units at least!
However, you can change the address by connecting the A0 pin to Vcc or GND. Unfortunately this will give you 2 different addresses and you need 3 different addresses.
There is a trick you can use to get around this problem. You can connect each of the 3 A0 pins of the DAC to 3 Arduino digital output pins.
Then, in your sketch, you can set the Arduino pins so that one DAC has a different address to the other 2 DACs. Then the Arduino can talk to that one DAC alone with no interference from the other 2 DACs.
The Arduino can select which DAC to talk to by changing the 3 output pins.
#include <Wire.h>
#define DAC_ADDRESS 0x60 // MCP4725 address for DAC 1, 2, 3
const byte dacAdrsPins[3] = {2, 3, 4}; //pins connected to the A0 pins of the DACs
void setup() {
Wire.begin(); // initialize I2C bus
}
void loop() {
// send values to DACs
writeDAC(0, 1023); // set DAC1 to maximum value (1023)
writeDAC(1, 512); // set DAC2 to half of maximum value
writeDAC(2, 0); // set DAC3 to minimum value (0)
delay(1000); // wait for 1 second before changing values
}
void writeDAC(byte dacNumber, int value) {
for (byte d=0; d<3; d++)
if (d==dacNumber) digitalWrite(dacAdrsPins[d], HIGH); else digitalWrite(dacAdrsPins[d], LOW);
// write the value to the DAC
Wire.beginTransmission(DAC_ADDRESS);
Wire.write(64); // send control byte
Wire.write(highByte(value)); // send most significant byte of value
Wire.write(lowByte(value)); // send least significant byte of value
Wire.endTransmission();
}
This will not work correctly. The array does not have indexes 3 or 4. You are comparing a number to an array pointer, it will never be equal. Why did you change the code I suggested?
Also I forgot you have to set these pins as OUTPUT:
void setup() {
Wire.begin(); // initialize I2C bus
for (byte d=0; d<3; d++)
pinMode(dacAdrsPins[d], OUTPUT);
}
Your schematic looks the same as before. I don't see the A0 pins connected to the arduino and I don't see any pot.
Did you check the arrangement of the essential termination resistors for a proper I²C communication needed?
I can´t see any of these in your schematic.
" If you're going to have more than one MCP4725 on a bus, you'll also want to disable the pull-up resistors on all but one breakout. To make this easier, we've added another jumper on the back of the board. The pull-ups are enabled by default. If you need to disable them, you'll need to cut the traces on the jumper pad with an Xacto knife. If you ever need to re-enable the pull-ups, simply solder the three pads back together"
THANK YOU! this code is ok. but DAC 1 and 3 work, constant 2.5 v as output value provide only second DAC chip it not change with POT, can you check it problem is code or my circuit