I am using an MCP4725 module with a Mega board. I just need to have a set voltage output.
When I input the desired voltage, I can measure the correct voltage for about a second, and then in the serial monitor I see a command that sets the voltage at 0, and then the measured voltage goes to 0. I have tried everything, swapping all components, but the problem persists.
#include <Wire.h>
#include <Adafruit_MCP4725.h>
Adafruit_MCP4725 dac;
void setup() {
Serial.begin(9600);
Wire.begin();
dac.begin(0x62); // Use the I2C address 0x62 for the MCP4725 module
}
void loop() {
if (Serial.available()) {
float voltageValue = Serial.parseFloat(); // Read the voltage value from the Serial Monitor
// Ensure the voltage value is within the valid range (0 to 5)
voltageValue = constrain(voltageValue, 0.0, 5.0);
// Convert the voltage to the corresponding DAC value (0 to 4095)
int dacValue = voltageValue * 4095.0 / 5.0;
// Set the DAC output value and update EEPROM
dac.setVoltage(dacValue, true); // 'true' means update the EEPROM
Serial.print("Output voltage set to: ");
Serial.print(voltageValue, 2);
Serial.println(" V");
}
}
I tried a different variation of the code without that command, and I get the same problem. It accepts my input voltage, and then after about a second it changes it to 0 v.
#include <Wire.h>
#include <Adafruit_MCP4725.h>
Adafruit_MCP4725 dac;
void setup() {
Serial.begin(9600);
dac.begin(0x62); // Change this to your I2C address if needed
}
void loop() {
Serial.println("Enter desired voltage (0-50 for 0.0-5.0V): ");
while (!Serial.available()) {
// Wait for user input
}
int targetVoltageRaw = Serial.parseInt();
if (targetVoltageRaw >= 0 && targetVoltageRaw <= 50) {
float targetVoltage = targetVoltageRaw / 10.0; // Convert to 0.0-5.0V range
dac.setVoltage(targetVoltage * 4095.0 / 5.0, false);
Serial.print("Set Voltage: ");
Serial.print(targetVoltage, 1);
Serial.println(" V");
} else {
Serial.println("Invalid input or voltage out of range. Please enter a valid voltage (0-50 for 0.0-5.0V).");
}
}