#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!
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.
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!
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 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.
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.
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.