MCP4725 Adafruit + Arduino Mega 2560 I²C endTransmission() hangs

Hello everyone,

I’m having trouble getting a single MCP4725 DAC to work with my Arduino Mega 2560. Here’s what I have and what I’ve tried:

Setup:

  • Arduino Mega 2560
  • Adafruit MCP4725 breakout board (or compatible, address = 0x62)
  • SDA → pin 20, SCL → pin 21 on Mega
  • VCC → 5V, GND → GND

Problem:
When I try to write to the DAC:

#include <Wire.h>

#define MCP4725_ADDR 0x62  
#define VCC 5    
#define STEP 0.033            
#define DELAY_MS 5000     
#define maximum 2.27

void setup() {
    Serial.begin(9600);
    pinMode(20, INPUT);
    pinMode(21, INPUT);
    delay(100);
    Wire.begin();
    Wire.setClock(50000);
}

void loop() {
   // Step through voltages from 0V to 5V for the digital to analog converter
   for (float voltage = 1.67; voltage <= maximum; voltage += STEP) {

    // Convert voltage to 12-bit value (0–4095)
    uint16_t dacValue = (uint16_t)((voltage / VCC) * 4095);
    Serial.print("DAC Output Voltage: ");
    Serial.print(voltage);
    //Send to MCP4725
    Wire.beginTransmission(MCP4725_ADDR);
    Wire.write(64);                     // Fast DAC update command
    Wire.write(dacValue >> 4);          // Upper 8 bits
    Wire.write((dacValue & 0x0F) << 4); // Lower 4 bits
    Serial.println(" 1");
    Wire.endTransmission();
    Serial.println(" 2");

    delay(DELAY_MS); 
} }

I tried this exact same code with my Arduino Uno and it worked just fine, but tried it with the Arduino Mega never worked. it prints the 1 and does not print the 2, meaning that it stops at the Wire.endTransmission();.

the main purpose of this code is to increase the output voltage of the mcp4725 every 5 seconds with an increment of 0.033V.

I even tried the Adafruit MCP4725 library examples, and it worked with the Uno and did not work with the Mega, could anybody help me solve this problem?
I would be very very grateful, I’m really stuck and would appreciate any guidance. Thanks!

Add 4k7 pullup resistors to both lines and see how it goes.

Noted, I will try it and tell you, I appreciate your response, but I have to inform you that the MCP4725 Adafruit already contains 10k pull up resistors.

Run the I2C scanner and let us know what it reports. If it does not find anything post your annotated schematic showing exactly how you wired it.

Your topic has been moved as you're not using an Arduino Due.

1 Like

50000 is an odd frequency, have you tried 10000?

Wire.write(64);  

That is not fast DAC update. The data should be 0 0 0 0 D11 D10 D9 D8 where Dn is the upper 4 bits of the data byte.
That command will put the DAC to sleep!

I have written a MCP4725 library too, you might give it a try on your MEGA.

It includes several samples to start with, please give it a try. (adjust address to your DAC).

Note: One example is specific made for the RP2040 so it wont run on a MEGA anyway.

Arduino mEGA board has built-in 2x10k pull-ups on the I2C bus.

Uplod the following modified sketch and report what message you get on Serial Monitor?

#include <Wire.h>

#define MCP4725_ADDR 0x62  
#define VCC 5    
#define STEP 0.033            
#define DELAY_MS 5000     
#define maximum 2.27

void setup() {
    Serial.begin(9600);
    pinMode(20, INPUT);
    pinMode(21, INPUT);
    delay(100);
    Wire.begin();
    Wire.setClock(50000);
}

void loop() {
   // Step through voltages from 0V to 5V for the digital to analog converter
   for (float voltage = 1.67; voltage <= maximum; voltage += STEP) {

    // Convert voltage to 12-bit value (0–4095)
    uint16_t dacValue = (uint16_t)((voltage / VCC) * 4095);
    Serial.print("DAC Output Voltage: ");
    Serial.print(voltage);
    //Send to MCP4725
    Wire.beginTransmission(MCP4725_ADDR);
    Wire.write(64);                     // Fast DAC update command
    Wire.write(dacValue >> 4);          // Upper 8 bits
    Wire.write((dacValue & 0x0F) << 4); // Lower 4 bits
    //Serial.println(" 1");
    byte busStatus = Wire.endTransmission();
   if(busStatus != 0)
   {
       Serial.print("I2C Bus error!");
       while(true);   //wait for ever
   }
    Serial.println('2');

    delay(DELAY_MS); 
} }

Sorry sir but could you demonstrate what you mean by running the I2C scanner? I am almost a beginner with Arduino, and thanks for your response and time.

I tried changing it to 10000, but it still did not work. It still worked with the Arduino uno in both cases. thanks for your time and response!

I tried it, but it only give the following output: "DAC Output Voltage: 1.67"

however, I tried it with the arduino uno and it worked, the "2" was printed in the serial monitor and it the code continued working, I am beginning to think that my Arduino Mega is faulted.

I tried it, did not work with the mega and worked with the uno

At the end of setup() put in Serial.print(SCL), print a space and then another line Serial.print(SDA) and see if the numbers match what you are connected to. This helps insure you and the mega are talking about the same connection. Be sure the MEGA is actually in slave mode.

I think there is a small misunderstanding, I am NOT connecting two Arduinos in the same time. In summary, I had a project where I used my Arduino Uno because I was waiting for the delivery of the Arduino Mega, and I did the wiring and finished everything with the Uno, then I got the Arduino Mega and just wanted to switch the connections from the Uno to the Mega.

however sir, I tried the Serial.print(SCL) and Serial.print(SDA), and it gave 21, and 20, respectively, and I also tried Serial.println(digitalRead(SCL)) and Serial.println(digitalRead(SDA)) and it gave 1, and 1.

Those are the pins you need to be using. I looked at the schematic in #8. Schematic shows SDA=20, SCL=21 so those connections are correct. You can try swapping them, it will not hurt anything. You can try slowing the I2C clock to see if that helps.

In the IDE PullDown menu under File

File>Examples>Wire>i2cScanner

The UNO and Mega both have a set of header pins specifically labeled SDA and SCL, you could use those to eliminate any possibility that the connections got switched between the UNO and Mega.

Presumably you are using the classic atmega328-based UNO.

Please confirm that the connections to the MCP4725 are soldered or otherwise electrically secure.

It does look that way. You will be the first with such an odd failure.
Do you have any other I2C device you could try with the Mega?