MCP4922/4902 SPI DAC

DAC (Digital to Analog Converter)

I use these little chips for a lot of applications, they’re quite cheap and really easy to use.

There’s a few variations of these chips, single channel, dual channel, bit resolution etc.

The ones in this article I’m going to talk about are:
MCP4902 – Dual  8 bit, giving 0 to 255 range
MCP4922 – Dual 12 bit giving 0 to 4095 range (more steps, better resolution)

There is a dual 10 bit DAC 4912 available.

Setting Up

Regardless of which dual DAC you choose, the wiring will be as follows:

Connections

Left
1: +5V
3: Chip Select pin (of your choice, set this in code)
4: MOSI (Pin 11 on Arduino Uno)
5: SCK (Pin 13 on Arduino Uno)

Right
8: GND
9: +5V
10: Output B
11: +5V
12: GND
13: +5V
14 Output A

Programming

The DAC is programmed via SPI, we need to send 2 8 bit values to the chip which differ depending on the output resolution.

Above: Registers on MCP4922 DAC
When we send SPI we send the MSB First, so the upper half will be sent first, then the lower half.
The lower half is easy, we just need to mask our 12 bit value to knock off the D8-D11 bits and just leave us with an 8 bit number, we mask with 0xFF or 255 or B11111111.

The upper half has the rest of the data, so to get this value we shift it up by 8 (>> 8) this will knock off the bits D0 to D7 and leave us with a 4 bit number.
Then we need to add bits to this number by using the |= command.
In my code you will see dacSPI0 |= 48 – – This is setting 2 bits, 48 is 00110000, if you look at the register above this corresponds to the SHDN and GA bit, and if you look at the bit description it says what this is doing.

The next bit is the (1<<7), this shifts a 1 to the bit location 7 which is the DAC selection A / B

Then we send this value first to the DAC , then the lower half value. That’s our voltage now on the output pin.

The 8 bit is similar, we just have to shift the data slightly differently.

Downloads

Code for both MCP4922 and MCP4902 in seperate sketches, just remmeber to choose the CS pin and connect up to your SPI.



Other notes

Teensy use
The Teensy is 3.3V, you can power the DAC with 5 volts, it’s happy with the SPI bus at 3.3 volts and you will get a 0 to 5 volt output.
If you power the DAC from 3.3 volts, this is the max output you will get from it.

Calculations

Voltage / Resolution = volts per value (value being 0 to 255/4095)
5000 / 256 = 19.53 mV per value
5000 / 4096 =1.22mV per value (higher resolution)