Example Code for Arduino-Store the DAC Value in EEPROM
A guide for Arduino enthusiasts on how to store DAC values in EEPROM, includes hardware requirements, software setup, wiring instructions, and sample code to output and store analog voltage values using the MCP4725 library.
Hardware Preparation
- DFRduino UNO R3 + Gravity IO Expansion Shield * 1
- Gravity: MCP4725 12-Bit I2C DAC Module * 1
- Gravity 4P sensor wire (comes with Gravity 12-bit DAC Module) * 1
- Digital multimeter (optional) * 1
- Oscilloscope (optional) * 1
Software Preparation
- Arduino IDE: Click to Download Arduino IDE
- Search and install the 'DFRobot_MCP4725' library in the library manager of Arduino IDE.
- Or download the .zip library in via Github and install the .zip library in Arduino IDE. How to install .zip library?
Wiring Diagram
Other Preparation Work
- Set the I2C address to 0x60 with the ADDR switch. For I2C address 0x61, you need to modify the first parameter of the function DAC.init () in the code below.
- Open Arduino IDE, upload the following sample code to the Arduino UNO, and use a digital multimeter to measure the output voltage of VOUT.
- The user can change the value of OUTPUT_VOLTAGE to change the analog voltage output. REF_VOLTAGE can be set to the value used in the Calibration section. If not calibrated, simply set it to 5000 (for 5V controllers such as Arduino) or 3300 (for 3.3V controller such as Raspberry Pi, FireBeetle etc.) .
Sample Code
/*
* file OutputVoltageEEPROM.ino
*
* @ https://github.com/DFRobot/DFRobot_MCP4725
*
* connect MCP4725 I2C interface with your board (please reference board compatibility)
*
* Output a constant voltage value and write to the internal EEPROM.
*
* Copyright [DFRobot](https://www.dfrobot.com), 2016
* Copyright GNU Lesser General Public License
*
* version V1.0
* date 2018-1-15
*/
#include "DFRobot_MCP4725.h"
#define REF_VOLTAGE 5000
DFRobot_MCP4725 DAC;
uint16_t OUTPUT_VOLTAGE = 1000; // Input DAC output voltage (0~REF_VOLTAGE,unit: mV)
void setup(void) {
Serial.begin(115200);
/* MCP4725A0_address is 0x60 or 0x61
* MCP4725A0_IIC_Address0 -->0x60
* MCP4725A0_IIC_Address1 -->0x61
*/
DAC.init(MCP4725A0_IIC_Address0, REF_VOLTAGE);
}
void loop(void) {
Serial.print("DFRobot_MCP4725 write to EEPROM and output: ");
Serial.print(OUTPUT_VOLTAGE);
Serial.println(" mV");
DAC.outputVoltageEEPROM(OUTPUT_VOLTAGE);
delay(200);
}
Result
- A 1V voltage signal can be measured at VOUT. Disconnect the module from the Arduino. Repower the module, by connecting oinly the VCC and GND pins of the module to 5V and GND on the Arduino, respectively. The user can still read 1V at VOUT. The function DAC.outputVoltageEEPROM () stores the DAC value in the EEPROM while it outputs a specified analog voltage signal. The module recalls the DAC value in the EEPROM upon power-up and restores the analog output at VOUT.
Was this article helpful?
