Smart Home Automation Forum
[arduino code examples for A32 Pro]-09 How to output DC0-10v from GP8403 DAC port - Printable Version

+- Smart Home Automation Forum (https://www.kincony.com/forum)
+-- Forum: Technical Support (https://www.kincony.com/forum/forumdisplay.php?fid=20)
+--- Forum: KC868-A32/A32 Pro (https://www.kincony.com/forum/forumdisplay.php?fid=27)
+--- Thread: [arduino code examples for A32 Pro]-09 How to output DC0-10v from GP8403 DAC port (/showthread.php?tid=6688)



[arduino code examples for A32 Pro]-09 How to output DC0-10v from GP8403 DAC port - admin - 09-14-2024

Code:
/*
* Made by KinCony IoT: https://www.kincony.com
*
* This Arduino program controls the GP8403 DAC I2C chip using an ESP32-S3 board.
* The program sets the DAC outputs for two channels:
* - Channel 0 (CH0) outputs 5V.
* - Channel 1 (CH1) outputs 10V.
*
* The I2C communication is handled using the Wire library, and the SDA and SCL pins
* are custom defined as GPIO11 (SDA) and GPIO10 (SCL).
*
* GP8403 I2C address: 0x58
*/

#include "DFRobot_GP8403.h"

// Initialize the GP8403 DAC object with the I2C address 0x58
DFRobot_GP8403 dac(&Wire, 0x58);

void setup() {
  // Start serial communication for debugging purposes
  Serial.begin(115200);

  // Initialize I2C communication with custom SDA and SCL pins
  Wire.begin(11, 10);  // SDA = GPIO11, SCL = GPIO10

  // Try to initialize the DAC, retry if initialization fails
  while(dac.begin() != 0) {
    Serial.println("Initialization failed, retrying...");
    delay(1000);  // Wait for 1 second before retrying
  }

  // If initialization succeeds
  Serial.println("Initialization successful!");

  // Set the DAC output range to 10V (this setting applies to both channels)
  dac.setDACOutRange(dac.eOutputRange10V);

  // Set the output for DAC channel 0 to 5V
  // Range for 10V mode is 0-10000, so 5000 represents 5V
  dac.setDACOutVoltage(5000, 0);  // Set CH0 to 5V
  Serial.println("Channel 0 set to 5V");

  // Set the output for DAC channel 1 to 10V
  dac.setDACOutVoltage(10000, 1);  // Set CH1 to 10V
  Serial.println("Channel 1 set to 10V");

  // Store the data in the chip's non-volatile memory so it persists after power-off
  dac.store();
  Serial.println("DAC values stored in memory");
}

void loop() {
  // No need to continuously write the values, the DAC holds the last set value
}
arduino ino file download: 
.zip   9-dac-GP8403.zip (Size: 975 bytes / Downloads: 37)
BIN file (you can use esp32 download tool download to ESP32-S3 with address 0x0 then directly to use) download: 
.zip   9-dac-GP8403.ino.merged.zip (Size: 187.92 KB / Downloads: 41)