Write a code in arduino but I am not getting correct DAC result at Vout pin , constantly i read wrong values , just check my code and let me know
#include <Wire.h>
#define devId 0x50 // DAC device ID
#define regDAC 0x40 // DAC register address
#define VREF 3 // DAC reference voltage
#define DAC_RESOLUTION 1024 // DAC resolution (8-bit)
// Function to reset the DAC
void dac_reset() {
Wire.beginTransmission(devId);
Wire.write(regDAC);
Wire.write(0x06);
Wire.endTransmission();
}
// Function to power on the DAC
void dac_power_on() {
Wire.beginTransmission(devId);
Wire.write(regDAC);
Wire.write(0x0F);
Wire.endTransmission();
}
// Function to power off the DAC
void dac_power_off() {
Wire.beginTransmission(devId);
Wire.write(regDAC);
Wire.write(0x00);
Wire.endTransmission();
}
// Function to lock the DAC
void dac_lock() {
Wire.beginTransmission(devId);
Wire.write(0xD3);
Wire.write(0x50);
Wire.endTransmission();
}
// Function to set the DAC's output voltage
void set_dac_output_voltage(double voltage) {
// Calculate the DAC code based on the desired voltage
int dacCode = (int)(voltage / VREF * DAC_RESOLUTION);
// Write the DAC code to the register
Wire.beginTransmission(devId);
Wire.write(0x21);
Wire.write(dacCode);
Wire.endTransmission();
}
void setup() {
Wire.begin();
Serial.begin(9600);
// Reset the DAC
// dac_reset();
// Power on the DAC
dac_power_on();
dac_lock();
// Perform other initialization tasks...
}
void loop() {
// Measure the DAC value continuously
// Set the DAC output voltage to 2.5V
set_dac_output_voltage(3.0);
// Read the DAC value
Wire.requestFrom(devId, 1); // Request 1 byte from the DAC
if (Wire.available()) {
int dacCode = Wire.read();
double voltage = (double)dacCode / DAC_RESOLUTION * VREF;
// Print the measured voltage
Serial.print("DAC Value: ");
Serial.print(dacCode);
Serial.print(" Measured Voltage: ");
Serial.print(voltage, 2);
Serial.println(" V");
}
// Add a delay if needed
delay(1000);
}