download

Raspberry Pi DAC – MCP4725 with wiringPi

The Raspberry Pi lacks a DAC but using the I2C bus, one can easily add a device like the 12bit MCP4725. The GPIO library wiringPi provides support for I2C devices, however, getting the MCP4725 working with it isn’t a simple as one might hope. The device is 12bit but the I2C protocol works on bytes (8bits). To send 12bit data, the Microchip designed the message transfer like this:

The MC4725 expects the 12bit data to be broken into two bytes and sent directly after each other.
The MC4725 expects the 12bit data to be broken into two bytes and sent directly after each other.

Trying to send this 12bit data using the standard commands to send bytes won’t work because the opening messages will be resent between the bytes. The solution is to write the register to be written (0x40 for standard or 0x60 for saving the output state to EEPROM) followed by the two bytes in a row – one is bodged in place of the register. One could write a new i2c_smbus function to do this but it’s useful to incorporate it into the existing wiringPi library.

I started by writing a basic function for using wiringPi:

With this working, I integrated it with the wiringPi methods. I’ve hosted my fork of the library on github:

https://github.com/tuna-f1sh/wiringPi-mcp4725

Installing this library, one can then use the chip like any other wiringPi chip, here the example I’ve put in ‘/examples’:

build and run in the examples directory with:

make dac
./dac 4095 #output Vdd
./dac #output repeating sine wave

Using my driver for wiringPi, a sine wave being produced by the MCP4725
Using my driver for wiringPi, a sine wave being produced by the MCP4725
cropped-JBRLogo.png

One thought on “Raspberry Pi DAC – MCP4725 with wiringPi”

Leave a Reply