Voltage resetting with MCP4725

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.

Any help would be appreciated.

Hello aardvarken
What a nice name.

Welcome to the worldbest Arduino forum ever.

Post your sketch, well formated, with well-tempered comments and in so called code tags "< code >" and schematic to see how we can help.

Have a nice day and enjoy coding in C++.

Thank you.

Here is the sketch.

#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");
  }
}

Hello Erdferkel

Take a view here to gain the knowledge.

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).");
  }
}

Set the line-ending in serial monitor to No Line Ending.

Or start using a better approach based on Serial Input Basics - updated.

Thank you very much. That fixed the problem.

I am just learning how to code the arduino (out of necessity), so thank you for the solution and the advice.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.