Arduino library · Sensors

Adafruit MCP4725: a 12-bit analog output over I2C

Yes. You can use the Adafruit MCP4725 library online in Mokxi with nothing to install, and the sketch runs in your browser against the simulated part (DAC, MCP4725). The example below, "Set a voltage with the MCP4725 and read it back on A0", runs on the Arduino Uno R3.

Built in on Mokxi: #include <Adafruit_MCP4725.h> and it builds, with nothing to install. Free, and no account needed.

What Adafruit MCP4725 does

An Uno’s analogWrite() is PWM, a square wave that only becomes a voltage after a filter. The MCP4725 is a real digital-to-analog converter on a small I2C board. Send it a number from 0 to 4095 and its VOUT pin holds that fraction of the supply voltage: on a 5 V supply, 1024 gives 1.25 V and each step is about 1.2 mV.

The library has two calls. begin() checks the chip acknowledges, and setVoltage(code, writeEEPROM) sends a value. Despite the name, setVoltage() takes the 12-bit code, not volts. Passing true as the second argument also stores the code in the chip’s EEPROM, so VOUT starts at that level after the next power-up; the chip is then busy for about 25 ms and ignores further writes, so keep EEPROM writes out of loops.

The address catches most people. The library defaults to 0x62, the address of Adafruit’s board, while the generic purple boards answer at 0x60, so call begin(0x60) for those, as the example does. Mokxi’s mcp4725 part follows the same rule, or set its model to MCP4725A1 to put it at 0x62. The example wires VOUT back to A0 so that analogRead() confirms each step of the voltage.

Library Manager nameAdafruit MCP4725
AuthorAdafruit
Include#include <Adafruit_MCP4725.h>
Boardsevery board Mokxi compiles for but ATtiny85
In the simulatorSimulated: it runs against the part the way it does on the bench.
Parts it drivesDAC, MCP4725

Install it

It builds on every board Mokxi compiles for but ATtiny85.

In Mokxi it is built in: #include <Adafruit_MCP4725.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "Adafruit MCP4725" by Adafruit from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add Adafruit MCP4725 to lib_deps in PlatformIO. Mokxi follows the calls of release 2.0.2, so a sketch written here builds there too.

What to know in the simulator: The library defaults to 0x62, Adafruit’s board (the MCP4725A1). The generic boards are 0x60: call begin(0x60), or set the part’s model to MCP4725A1.

The calls you will use

In the order a sketch meets them.

Adafruit_MCP4725 dac

Creates the driver; nothing is sent until begin().

dac.begin(0x60)

Checks that a chip acknowledges at 0x60 and returns false otherwise. With no argument it uses 0x62.

dac.setVoltage(2048, false)

Sets VOUT to 2048/4096 of the supply, half of it, without touching the EEPROM. Returns whether the chip acknowledged.

for (uint16_t code = 0; code <= 4095; code += 1024) {
  dac.setVoltage(code, false);
  delay(10);
  Serial.println(analogRead(A0));
}

dac.setVoltage(1024, true)

Sets the output and also stores 1024 as the power-up value. The chip ignores writes for about 25 ms afterward.

dac.setVoltage(code, false, 100000)

The third argument is the I2C clock for the transfer, 400000 by default. Lower it for long wires.

Example: Set a voltage with the MCP4725 and read it back on A0

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino · Arduino Uno R3
#include <Wire.h>
#include <Adafruit_MCP4725.h>

// The generic purple board is at 0x60; Adafruit's is 0x62.
Adafruit_MCP4725 dac;

void setup() {
  Serial.begin(9600);
  if (!dac.begin(0x60)) {
    Serial.println("MCP4725 not found");
    while (1) delay(10);
  }
  Serial.println("MCP4725 found");
  // Up the scale in quarters: VOUT is 5 V x code / 4096.
  for (uint16_t code = 0; code <= 4095; code += 1024) {
    dac.setVoltage(code, false);
    delay(10);
    int a0 = analogRead(A0);
    Serial.print("code ");
    Serial.print(code);
    Serial.print(" -> A0 ");
    Serial.println(a0);
  }
  dac.setVoltage(1024, false);
  delay(10);
  int a0 = analogRead(A0);
  Serial.println(abs(a0 - 256) <= 3 ? "1.25 V on A0" : "VOUT is off");
}

void loop() {}

Wiring

  • Arduino Uno R3 A0 to DAC, MCP4725 VOUT
  • Arduino Uno R3 A4 to DAC, MCP4725 SDA
  • Arduino Uno R3 A5 to DAC, MCP4725 SCL
  • Arduino Uno R3 5V to DAC, MCP4725 VCC
  • Arduino Uno R3 GND to DAC, MCP4725 GND

Open it in the editor, wired and ready to run

Common errors and how to fix them

MCP4725 not found

begin() got no acknowledge at the address tried. Generic boards are at 0x60 and Adafruit’s at 0x62, which is the default; run an I2C scanner and pass whatever it finds to begin().

VOUT stays near 0 V after setVoltage(3.3, false)

setVoltage() takes a code from 0 to 4095, not volts, so 3.3 becomes 3. For a voltage V on supply Vs, send V / Vs x 4095, rounded.

Updates stop working for a moment after storing a value

Writing the EEPROM keeps the chip busy for about 25 ms, during which it ignores writes, as the real one does. Wait before the next setVoltage(), and store to EEPROM only when the startup value must change.

Questions and answers

Why does the MCP4725 example read A0?

VOUT is wired to A0, so analogRead() shows each code arriving: 1024 out of 4096 should read about 256 out of 1024 on the Uno’s converter.

Can the MCP4725 play a waveform?

Yes, by calling setVoltage() from a table in a loop, but each step is a whole I2C transfer, so the rate is limited by the bus clock rather than the chip.

Projects that use Adafruit MCP4725

Built-in projects that include it. Each page has the full sketch and opens in the editor, already wired.

Try Adafruit MCP4725 without installing anything

Open the example, change the sketch, and keep your version in a free account.