At least from the zillions of request for a DAC Driver implementation example that I received in the latest weeks, this is the long awaited topic: ESP8266 Modules and Voltage Output Digital-to-Analog Converter control!

For our project we will use Microchip MCP4726, a Voltage Output Digital-to-Analog Converter (DAC) with EEPROM and I2C™ Interface.

As you will see, despite the fact that is a tiny one, it’s a very versatile chip with a great price/capabilities ratio. If you take a look on Digikey for example it can be found at around 1.15$!


• Output Voltage Resolutions:
    – 12-bit: MCP4726
– 10-bit: MCP4716
– 8-bit: MCP4706
• Rail-to-Rail Output

• Fast Settling Time of 6 μs (typical)
• DAC Voltage Reference Options:
– VREF Pin

The VREF pin or the device VDD can be selected as the DAC’s reference voltage. When VDD is selected, VDD is connected internally to the DAC reference circuit.  When the VREF pin is used, the user can select the output buffer’s gain to 1 or 2. When the gain is 2, the VREF pin voltage should be limited to a maximum of VDD/2.

That means that it has also an internal reference and if you don’t need very high accuracy or you have a very stable power supply for MCP4726 DAC, you don’t need to use an external expensive Voltage Reference to make it working decent! If you are looking in the basement after boson Higgs cousin then this 1.15$ DAC is not what are you looking for 🙂

• Output Gain Options:
– Unity (1x)
– 2x, only when VREF pin is used as voltage source
• Nonvolatile Memory (EEPROM):
– Auto Recall of Saved DAC register setting
– Auto Recall of Saved Device Configuration (Voltage Reference, Gain, Power-Down)
• Power-Down modes:
– Disconnects output buffer
– Selection of VOUT pull-down resistors (640 kΩ, 125 kΩ, or 1 kΩ)
• Low-Power Consumption:

– Normal Operation: 210 μA typical
– Power-Down Operation: 60 nA typical (PD1:PD0 = 11)
• Single-Supply Operation: 2.7V to 5.5V
• I2C™ Interface:
– Eight Available Addresses – Factory hardcoded !! – if you need more than one to run in your project be carefully when ordering!!
– Standard (100 kbps), Fast (400 kbps), and
High-Speed (3.4 Mbps) modes
• Small 6-lead SOT-23 and DFN (2×2) Packages
• Extended Temperature Range: -40°C to +125°C

For more details, please see MCP4726 Datasheet

Don’t be scared by the SOT-23 package used, with a small SOT-23 to DIP adapter will fit great in our CBDB extension slots so it will not matter at all. Actually is even more manageable than the MCP9808 Temperature sensor MSOP-8 package used before

What we will need:
  • CBDB Board
  • USB adapter (take a look on Part 1 for details how to connect them together)
  • MCP4726 Module from above
  • LED module (if you want maybe a LED ticker)
CBDB with MCP4726 DAC and MCP9808 Temperature Modules
    Connection between the modules is pretty simple as both are I2C chip modules. Each of them is connected at +3V, GND and toghether on I2C bus using SDA and SCL lines. I will suggest also to do proper decoupling for MCP4726 Power and Vout pins (see picture above).
MCP4726 DAC Module connections

For programming and uploading the driver and the software we will continue to use the LuaUploader as before.

Driver implementation.

As MCP4726 has a I2C compatible compatible interface, building a driver for it it’s a pretty straigh forward process:

1. Init I2C bus/interface

dev_addr = 0x60,
init = function (self, sda, scl) = 0
i2c.setup(, sda, scl, i2c.SLOW)

2. Write Volatile DAC Register Function.

write_dac = function (self, voltage)  
 self.dev_addr = 0x60 
 self.reg_addr = 0x40

 volt=(4096*voltage)/3.27 -- tweak the value for your VREF value
 print("Voltage Steps:" .. string.format("%d",volt))  --debug
 msb = bit.rshift(volt, 8)
 print("MSB:" .. string.format("%d",msb))              --debug

 lsb = volt-bit.lshift(msb,8)
 print("LSB:" .. string.format("%d",lsb))              --debug

 i2c.address(, self.dev_addr ,i2c.TRANSMITTER)

For testing, just save the code on ESP as ‘mcp4726.lua‘, restart ESP and run:

sda=2 --GPIO4
scl=1 --GPIO5
mcp4726:init(sda, scl)

Uploading Driver on ESP module

 If all OK, if you measure the DAC output (see picture above for connections) you will find the programmed voltage.

                                                     MCP4726 DAC output voltage set

Youtube MCP4726 DAC Driver Test Video :

That’s all for today, thank you for your great feedback and looking forward for your suggestions!

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

battery monitor system

Thingspeak – SmartMon Ext Board P4

This is Part 4 of the SmartMon Extension Board series. SmartMon v2.7ex Board  is a extension board fully compatible with Arduino, ESP8266, ARM, PIC & other available MCu’s out there.  As long as your MCU/Dev Read more…

battery monitor system

Smart Mon Ext Board – Software example – P3

This is Part 3 of the SmartMon Extension Board series. SmartMon v2.7ex Board  is a extension board fully compatible with Arduino, ESP8266, ARM, PIC & other available MCu’s out there.  As long as your MCU/Dev Read more…


MCP4728 – 12 Bit I2C DAC Driver – AN1

From today we will move on the Analog interface part of the nEXT EVO Board AN-1 and we will start talking about the Digital to Analog conversion, Analog Autoscaling input and Analog to Digital conversion Read more…