geekcowboy.neta florea.xyz revival

Arduino and microcontrollers

MCP4725 12-bit I2C DAC with an Arduino in 2026: boards, wiring and a sketch with no library

An Arduino Uno has no DAC, and analogWrite() is PWM. The MCP4725 gives you a real voltage from 0 to its supply in 4096 steps over I2C, and remembers its power-up value. Which boards are on sale, how to wire them, what the command bytes mean, and one complete sketch that needs nothing but Wire.

By florea.xyz ·

analogWrite() on an Arduino Uno does not make a voltage. It switches a pin between 0 and 5 V, 490 times a second (980 on pins 5 and 6), and the value from 0 to 255 sets how long the pin stays high. An LED or a motor averages that by itself, and nobody can tell. A circuit that wants a steady control voltage, such as an amplifier’s bias, a comparator’s threshold or an analog synth’s pitch input, sees a square wave.

The MCP4725 fixes that for about five dollars: a 12-bit digital-to-analog converter (DAC) in a six-pin package, on I2C, with an EEPROM that sets the voltage it outputs the moment it powers up.

PWM and a filter, or a DAC?

You can turn PWM into a voltage with a resistor and a capacitor. Whether that is good enough depends on one trade-off. A filter slow enough to smooth 490 Hz to a small ripple is also slow to follow a change. Make it faster and more ripple gets through. On top of that, analogWrite() gives you 256 steps, about 20 mV each at 5 V.

PWM plus a filter is fine for:

  • LED brightness, motor speed and heaters: anything that averages by itself;
  • a voltage that rarely changes, where nothing minds a little ripple.

Use a DAC when:

  • the voltage feeds another circuit’s input and must be clean: a reference, a bias, a setpoint, a control voltage;
  • it has to change quickly and settle (the MCP4725 settles in 6 µs, typically);
  • you need finer steps: 4096 of them, 1.22 mV each at 5 V;
  • it must be correct at power-up, before your sketch has even started. That is what the EEPROM is for.

One note before you buy anything: if you have an UNO R4, Arduino’s own reference says it already has a true analog output on pin A0, through analogWrite(). This article is for the Uno R3 and every other board without one.

The MCP4725 in one table

From Microchip’s datasheet (DS20002039F, the current edition of the document once numbered DS22039; the command tables are unchanged from DS22039D):

What Value
Resolution 12 bits: codes 0 to 4095
Supply (VDD) 2.7 to 5.5 V; 210 µA typical, 400 µA at most
Reference VDD itself: there is no separate reference pin
Output VOUT = VDD × code / 4096, so 0 V up to VDD minus one step
One step (LSB) 1.22 mV at 5 V, 0.73 mV at 3 V
Settling time 6 µs typical
Load specified with 5 kΩ and 100 pF; drives up to 1000 pF
I2C 100 kHz, 400 kHz and 3.4 MHz; 7-bit address 1100 A2 A1 A0, i.e. 0x60 to 0x67
EEPROM 14 bits (the code and two power-down bits), loaded at every power-up
EEPROM write 25 ms typical, 50 ms at most; at least 1 million writes at 25 °C
Factory default code 0x800: half of VDD
Power-down 0.06 µA typical; the output is switched to a resistor to ground (below)

Two rows matter more than they look.

The reference is the supply. The output is a fraction of VDD, so it is exactly as good as VDD. A 5 V supply from USB or a regulator is never exactly 5.00 V, and the datasheet warns that any variation or noise on VDD goes straight to the output. Measure VDD at the DAC with a meter and give the sketch that number (VDD 4.93). For precise work, give the DAC a clean, regulated supply of its own. The datasheet also allows up to 2 % gain error and 0.75 % offset error at its limits (typical values are far smaller), so calibrate against your meter if the absolute voltage matters.

The address is only partly yours. A2 and A1 are set at the factory: Microchip sells the chip as MCP4725A0 (A2 A1 = 00, the default) through MCP4725A3. Only A0 is a pin. So a board carries two possible addresses, and which two depends on the chip it was built with.

Which board to buy

Checked on 27 November 2026 on the makers’ own shops. Prices change, so check before you order. These are plain links: the site earns nothing from them.

Board Price Stock Address Pull-ups Connectors
SparkFun I2C DAC Breakout, BOB-31305 $4.75 in stock 0x60, or 0x61 with a solder jumper 4.7 kΩ, jumper to disable header holes
SparkFun Qwiic 12-Bit DAC, BOB-31206 $4.95 in stock 0x60, or 0x61 with the ADDR jumper opened 4.7 kΩ, jumper to disable 2 × Qwiic, header holes
Adafruit MCP4725 Breakout, product 935 $4.95 in stock 0x62, or 0x63 with A0 high or the jumper closed 10 kΩ 2 × STEMMA QT, header included

The pull-up values for the SparkFun boards come from their schematics; the Adafruit value comes from its guide. SparkFun’s older board, BOB-12918, is retired, and its page now points to BOB-31305. Adafruit’s addresses (0x62 and 0x63) mean it uses the A1 version of the chip. The sketch below tries all eight addresses, so it finds any of these boards without changes.

Adafruit’s STEMMA QT connectors are compatible with SparkFun’s Qwiic. An Uno R3 has neither, so wire it to the header pins. Note that SparkFun’s Qwiic system carries 3.3 V. If you power the DAC through it, the output tops out at 3.3 V.

Wiring it to an Uno

DAC board Uno R3
VCC / VDD / VIN 5V
GND GND
SDA A4, or the SDA pin on the digital header
SCL A5, or the SCL pin on the digital header
VOUT your circuit, or a meter to GND
A0 / ADDR leave it alone, unless two DACs share the bus

On the Uno R3, A4/A5 and the SDA/SCL pins on the digital header are the same two signals: use whichever is handier.

Keep the load light. The datasheet specifies the output into 5 kΩ, and says the output voltage starts to sag once the load drops below about 3.5 kΩ. To drive anything heavier, put an op-amp buffer after it.

Pull-ups

SDA and SCL are open-drain: the chips on the bus can only pull the lines low, and resistors pull them back up. Microchip suggests 1 to 10 kΩ for standard and fast mode. All three boards above have pull-ups already, so a single board needs nothing extra.

The Uno’s Wire library also switches on the ATmega328P’s internal pull-ups (you can see it in twi.c). Those are 20 to 50 kΩ: too weak to rely on.

With several boards on one bus, their pull-ups end up in parallel: two 4.7 kΩ pairs make 2.35 kΩ, which is fine. With more than that, disable the pull-ups on all but one board. Both SparkFun boards have a jumper for exactly that.

3.3 V boards

On a board whose pins run at 3.3 V, power the DAC from 3.3 V. The output range is then 0 to 3.3 V, in steps of 0.8 mV.

Do not power the DAC from 5 V while the board runs at 3.3 V. The breakout’s pull-ups go to its own supply, so they would pull SDA and SCL up to 5 V, above the 3.3 V your board’s pins run at. And the MCP4725 needs at least 0.7 × VDD to see a “1”, which is 3.5 V on a 5 V supply: more than a 3.3 V pin puts out. If you need 0 to 5 V from a 3.3 V board, use an I2C level shifter between them, or amplify a 3.3 V DAC’s output.

The bytes on the wire

Every I2C message starts with the address byte: 1100 A2 A1 A0 and then a read/write bit. Arduino’s Wire library uses 7-bit addresses and adds that bit itself, so you write 0x60, not 0xC0. After the address come one of three write commands (datasheet, section 6.1):

Command Bytes after the address What it does
Fast mode (C2 C1 = 0 0) 0 0 PD1 PD0 D11 D10 D9 D8, D7 … D0 sets the output; the EEPROM is untouched
Write DAC register (C2 C1 C0 = 0 1 0) 0 1 0 X X PD1 PD0 X, D11 … D4, D3 … D0 X X X X the same as fast mode, one byte longer
Write DAC register and EEPROM (0 1 1) 0 1 1 X X PD1 PD0 X, D11 … D4, D3 … D0 X X X X sets the output and stores it as the power-up value

D11 … D0 is the 12-bit code, most significant bit first, and an X bit is ignored. Two worked examples, which the site’s test checks:

  • code 2048 (half of VDD), fast mode: 0x08 0x00;
  • code 2748 stored in the EEPROM: 0x60 0xAB 0xC0.

A read returns five bytes:

  • a status byte: RDY/BSY, POR and the two power-down bits;
  • the DAC register, in two bytes laid out like the 3-byte write;
  • the EEPROM, in two bytes: X PD1 PD0 X D11 D10 D9 D8, then D7 … D0.

RDY/BSY is 0 while the EEPROM is being written. During that time the chip ignores every new write. That is why the sketch polls it after saving.

Power-down

The two PD bits ride along in every write:

PD1 PD0 Output
0 0 normal
0 1 off, pulled to ground through 1 kΩ
1 0 off, pulled to ground through 100 kΩ
1 1 off, pulled to ground through 500 kΩ

In power-down, the chip draws about 60 nA and only the I2C interface stays awake. The next write in normal mode wakes it, or the general call wake-up (address 0x00, then 0x09). The PD bits can be stored in the EEPROM too, so a DAC can power up switched off, holding its output near ground until your code is ready.

The power-up value

At power-on, the chip copies the EEPROM into the DAC register and outputs it straight away. New chips come set to 0x800, half of VDD. Two details from the datasheet are worth knowing:

  • Slow supplies. If VDD rises slower than 1 V per millisecond, the chip may fail to load the EEPROM. The fix is a general call reset (address 0x00, then 0x06), which reloads it. A general call goes to every device on the bus, and any other chip that implements this reset will reset too. That is why the sketch below does not send one on its own.
  • Wear. At least a million writes, at 25 °C. That is plenty for a setting you save by hand, and not enough for a sketch that saves on every loop. In the sketch below, saving is a separate command for that reason.

The sketch

One file, no library besides Wire. Open the Serial Monitor at 115200 baud, with the line ending set to “Newline”, and type:

  • 2048: a raw code;
  • 1.8V (or 1.8): a voltage;
  • SAVE: store the present value as the power-up default;
  • WAVE: a slow triangle wave, from 0 V to VDD and back every 4 seconds;
  • OFF: power down, with 1 kΩ to ground;
  • VDD 4.93: tell the sketch what your meter says the supply is.

mcp4725_dac.ino

// mcp4725_dac.ino: drive an MCP4725 12-bit I2C DAC from the Serial Monitor, with no library but Wire.
// Serial Monitor at 115200 baud, line ending "Newline":
//   2048      a raw code, 0-4095: the output is VDD x code / 4096
//   2.5V      a voltage from 0 to VDD (a decimal point also means volts: 2.5)
//   SAVE      store the present code in the DAC's EEPROM, as the value it outputs at every power-up
//   WAVE      a triangle wave, 0 V to VDD and back every 4 seconds, until the next line
//   OFF       power down: the output is pulled to GND through 1 kohm; any new value wakes it up
//   VDD 4.93  the supply voltage you measured at the DAC, for the voltage conversions (default 5.000)
// Command bytes from Microchip's MCP4725 datasheet, DS20002039F (formerly DS22039). geekcowboy.net, 2026. MIT licence.

#include <Wire.h>

const unsigned long WAVE_MS = 4000;  // one period of the triangle wave

uint8_t dacAddr;    // the DAC's 7-bit I2C address, 0 until one answers
uint16_t dacCode;   // the code it is outputting
float vdd = 5.0;    // the MCP4725's reference is its own supply
bool waving;
char line[24];      // the command being typed
uint8_t lineLen;
bool lineTooLong;

// Fast mode write, datasheet figure 6-1: [0 0 PD1 PD0 D11 D10 D9 D8] [D7 ... D0]. pd 0 = normal output.
void packFast(uint16_t code, uint8_t pd, uint8_t *b) {
  b[0] = pd << 4 | code >> 8;
  b[1] = code & 0xFF;
}

// Write DAC register and EEPROM (C2 C1 C0 = 0 1 1), figure 6-2: [0 1 1 X X PD1 PD0 X] [D11 ... D4] [D3 ... D0 X X X X]
void packSave(uint16_t code, uint8_t pd, uint8_t *b) {
  b[0] = 0x60 | pd << 1;
  b[1] = code >> 4;
  b[2] = (code & 0x0F) << 4;
}

// A read returns five bytes, figure 6-3: [RDY/BSY POR X X X PD1 PD0 X] [D11 ... D4] [D3 ... D0 X X X X],
// then the EEPROM: [X PD1 PD0 X D11 D10 D9 D8] [D7 ... D0]
uint16_t registerCode(const uint8_t *r) { return r[1] << 4 | r[2] >> 4; }
uint16_t eepromCode(const uint8_t *r) { return (r[3] & 0x0F) << 8 | r[4]; }

// "4.93" or "4.93V", from lo to hi volts. Digits and dots only, so strtod() sees nothing exotic.
bool parseVolts(const char *s, float lo, float hi, float *v) {
  size_t n = strlen(s);
  if (n && s[n - 1] == 'V') n--;
  if (n == 0 || n > 8) return false;
  for (size_t i = 0; i < n; i++)
    if (!isdigit((unsigned char)s[i]) && s[i] != '.') return false;
  char *end;
  *v = strtod(s, &end);
  return end == s + n && *v >= lo && *v <= hi;
}

// A code ("2048") or a voltage ("2.5V", "2.5") into a code. False for anything else, or out of range.
bool parseLevel(const char *s, float ref, uint16_t *code) {
  size_t n = strlen(s);
  if (strchr(s, '.') || (n && s[n - 1] == 'V')) {
    float v;
    if (!parseVolts(s, 0, ref, &v)) return false;
    unsigned long c = (unsigned long)(v * 4096 / ref + 0.5);
    *code = c > 4095 ? 4095 : c;  // VDD itself is out of reach: the top code gives VDD - 1 LSB
    return true;
  }
  if (n == 0 || n > 4) return false;
  uint16_t c = 0;
  for (size_t i = 0; i < n; i++) {
    if (!isdigit((unsigned char)s[i])) return false;
    c = c * 10 + (s[i] - '0');
  }
  if (c > 4095) return false;
  *code = c;
  return true;
}

// Where the triangle wave is after ms milliseconds: 0 up to 4095 in half a period, then back down.
uint16_t triangleCode(unsigned long ms) {
  unsigned long t = ms % WAVE_MS, half = WAVE_MS / 2;
  return (t < half ? t : WAVE_MS - t) * 4095UL / half;
}

void printCode(uint16_t code) {
  Serial.print(code);
  Serial.print(F(" = "));
  Serial.print(code * vdd / 4096, 3);
  Serial.println(F(" V"));
}

// Wire.endTransmission(): 0 is success, 2 no answer to the address, 3 data not acknowledged, 5 timeout.
bool i2cOk(uint8_t err) {
  if (err == 0) return true;
  Serial.print(F("ERR I2C error "));
  Serial.print(err);
  Serial.println(F(" (2: no answer, 3: data refused, 5: bus timeout): check wiring, power and pull-ups"));
  dacAddr = 0;  // look for the DAC again before the next command
  return false;
}

bool send(const uint8_t *b, uint8_t n) {
  Wire.beginTransmission(dacAddr);
  Wire.write(b, n);
  return i2cOk(Wire.endTransmission());
}

bool readDac(uint8_t *r) {
  if (Wire.requestFrom(dacAddr, (uint8_t)5) != 5) {
    Serial.println(F("ERR I2C read failed: check wiring, power and pull-ups"));
    dacAddr = 0;
    return false;
  }
  for (uint8_t i = 0; i < 5; i++) r[i] = Wire.read();
  return true;
}

// The address is 1100 A2 A1 A0: A2 and A1 are set at the factory, A0 by a pin (datasheet 7.2). Try all eight.
bool findDac() {
  for (uint8_t a = 0x60; a <= 0x67; a++) {
    Wire.beginTransmission(a);
    if (Wire.endTransmission() == 0) {
      dacAddr = a;
      break;
    }
  }
  if (!dacAddr) {
    Serial.println(F("ERR no MCP4725 answers at 0x60-0x67: check SDA, SCL, power and pull-ups"));
    return false;
  }
  uint8_t r[5];
  if (!readDac(r)) return false;
  dacCode = registerCode(r);
  Serial.print(F("MCP4725 at 0x"));
  Serial.print(dacAddr, HEX);
  Serial.print(F(", power-up default "));
  Serial.print(eepromCode(r));
  Serial.print(r[0] & 0x06 ? F(", powered down, code ") : F(", output code "));
  printCode(dacCode);
  return true;
}

bool setCode(uint16_t code, uint8_t pd) {
  uint8_t b[2];
  packFast(code, pd, b);
  if (!send(b, 2)) return false;
  dacCode = code;
  return true;
}

// Writes the DAC register and the EEPROM, then polls RDY/BSY: 25 ms typical, 50 ms at most (datasheet, section 1.0).
void saveCode() {
  uint8_t b[3], r[5];
  packSave(dacCode, 0, b);
  if (!send(b, 3)) return;
  unsigned long start = millis();
  do {
    delay(2);
    if (!readDac(r)) return;
  } while (!(r[0] & 0x80) && millis() - start < 100);
  if (!(r[0] & 0x80)) {
    Serial.println(F("ERR the EEPROM write did not finish within 100 ms"));
  } else if (eepromCode(r) != dacCode) {
    Serial.print(F("ERR the EEPROM holds "));
    Serial.println(eepromCode(r));
  } else {
    Serial.print(F("OK power-up default saved: "));
    printCode(dacCode);
  }
}

void runCommand(char *cmd) {
  waving = false;  // any line stops the wave
  if (strncmp(cmd, "VDD ", 4) == 0) {
    float v;
    if (!parseVolts(cmd + 4, 2.7, 5.5, &v)) {
      Serial.println(F("ERR VDD is 2.7 to 5.5, the MCP4725's supply range"));
      return;
    }
    vdd = v;
    Serial.print(F("OK VDD = "));
    Serial.println(vdd, 3);
    return;
  }
  bool save = strcmp(cmd, "SAVE") == 0, wave = strcmp(cmd, "WAVE") == 0, off = strcmp(cmd, "OFF") == 0;
  uint16_t code = 0;
  if (!save && !wave && !off && !parseLevel(cmd, vdd, &code)) {
    Serial.println(F("ERR type a code 0-4095, a voltage from 0 to VDD like 2.5V, SAVE, WAVE, OFF or VDD 4.93"));
    return;
  }
  if (!dacAddr && !findDac()) return;
  if (save) {
    saveCode();
  } else if (wave) {
    waving = true;
    Serial.println(F("OK triangle wave, 0 V to VDD and back every 4 s: send any line to stop"));
  } else if (off) {
    if (setCode(dacCode, 1)) Serial.println(F("OK powered down: the output is pulled to GND through 1 kohm"));
  } else if (setCode(code, 0)) {
    Serial.print(F("OK "));
    printCode(code);
  }
}

void readSerial() {
  while (Serial.available()) {
    char c = Serial.read();
    if (c == '\n' || c == '\r') {
      line[lineLen] = '\0';
      if (lineTooLong) Serial.println(F("ERR the line is too long"));
      else if (lineLen) runCommand(line);
      lineLen = 0;
      lineTooLong = false;
    } else if (lineLen < sizeof(line) - 1) {
      line[lineLen++] = toupper((unsigned char)c);
    } else {
      lineTooLong = true;
    }
  }
}

void setup() {
  Serial.begin(115200);
  Wire.begin();
  Wire.setWireTimeout(25000, true);  // off by default on AVR; without it a stuck bus would hang the sketch
  Serial.println(F("mcp4725_dac: type a code 0-4095, a voltage like 2.5V, SAVE, WAVE, OFF or VDD 4.93"));
  findDac();
}

void loop() {
  readSerial();
  if (waving) {
    uint16_t code = triangleCode(millis());
    if (code != dacCode && !setCode(code, 0)) waving = false;
  }
}

With an Adafruit board on a USB supply that measures 4.93 V, a session looks like this (the commands you type are shown too):

mcp4725_dac: type a code 0-4095, a voltage like 2.5V, SAVE, WAVE, OFF or VDD 4.93
MCP4725 at 0x62, power-up default 2048, output code 2048 = 2.500 V
VDD 4.93
OK VDD = 4.930
1.8V
OK 1495 = 1.799 V
SAVE
OK power-up default saved: 1495 = 1.799 V
4096
ERR type a code 0-4095, a voltage from 0 to VDD like 2.5V, SAVE, WAVE, OFF or VDD 4.93

The 1.799 V is not a rounding bug: the sketch picks the nearest code, and with steps of 1.2 mV that is as close to 1.8 V as the chip gets. Unplug the Uno, plug it back in, and the first line now says power-up default 1495: the DAC was outputting 1.8 V before the sketch even started.

How the sketch works

  • Finding the DAC. At start-up, the sketch sends just an address to 0x60, 0x61 and so on up to 0x67, and stops at the first one that answers. Then it reads the five status bytes, so it knows what the DAC is already outputting. If you have another chip in that range on the same bus, replace the search with your DAC’s fixed address.
  • Setting a value is a fast-mode write: two bytes, built by packFast().
  • SAVE sends the 0 1 1 command, built by packSave(), then reads the status every 2 ms until RDY/BSY comes back to 1. It gives up after 100 ms, twice the datasheet’s maximum. Finally it compares the EEPROM’s contents with what it sent, and only then says OK.
  • WAVE works out where the triangle should be from millis(), and sends a new fast-mode write whenever the code changes. A fast-mode write is 27 clock pulses plus start and stop, about 0.3 ms at the Wire library’s default 100 kHz. So the Uno manages around 3,000 updates a second: plenty for a wave this slow, and the reason this is not how you play audio from an Uno. Wire.setClock(400000) makes the bus four times faster, if your pull-ups and wires allow it.
  • Errors are reported, not swallowed. Wire.endTransmission() returns 0 for success, 2 when nothing answered the address, 3 when the data was refused, 4 for other bus errors and 5 for a timeout. The sketch prints the number and goes looking for the DAC again before the next command. The AVR Wire library ships with its timeout switched off. The sketch switches it on, because a shorted or stuck SDA line would otherwise hang it forever.
  • Input is checked before anything goes on the bus. A code must be 0 to 4095, digits only. A voltage must be digits and at most one decimal point, from 0 to VDD. Lines longer than 23 characters are refused, not cut short.
  • RAM. An Uno has 2 KB of it. Every message sits in flash, inside F(), instead of being copied into RAM at start-up.

How this was tested

No MCP4725 was on the bench for this article, so the sketch was checked in two other ways:

  1. It compiles for an Arduino Uno with arduino-cli.
  2. A small C++ program runs the unchanged sketch on a computer, with a stand-in for Wire. The stand-in records every I2C transaction and answers reads the way an MCP4725 does. The program checks that:
  3. the bytes of every write, for codes from 0x001 to 0xFFF and all four power-down settings, and the decoding of reads, match the datasheet’s figures 6-1 to 6-3 bit for bit;
  4. the start-up search finds the board at 0x62 (Adafruit) and at 0x60 (SparkFun), and typed voltages turn into the right codes;
  5. SAVE polls until the EEPROM is ready, gives up after 100 ms, and notices an EEPROM that holds the wrong value;
  6. the triangle wave writes the right code every millisecond, and any line stops it;
  7. 21 kinds of bad input, from 4096 and -1 to 1e3, 2,5 and an overlong line, are refused without touching the bus, and I2C errors, a missing DAC and a failed read are reported.

To make sure the test can fail, eight bugs were planted in copies of the sketch, including shifted power-down bits, the wrong command code, an unmasked EEPROM byte and no polling. The test caught every one.

If you run it on a real board and something does not behave as described, the About page says where to write.

Sources