User Tools

Site Tools


DIY Arduino DAC – 8 Bits for 0,85€

Digital to analog conversion with Arduino can be tricky. PWM has its limitations when used for applications like audio, and R2R ladders are interesting but have a too large pin cost : 8 pins gathers 60% of the Arduino Uno disgital IO budget. The cleanest solution relies on usage of dedicated chips like the MCP4921, which allows through SPI to handle a 12bits DAC for roughly 4€. High speed SPI output capability with a pin count limited to the 3 required SPI interface.

This taken into account, it is easy to build an 8 bit SPI DAC using a R2R network connected to a deserialiser like the 74HC595 chip. This 0,85€ DAC schematic is explained herebelow.


Arduino SPI hardware interface is connected to the deserializer (CLK/MOSI), including the CS gate connected to Arduino pin 10. This DAC has a 0/2,5V span, which allows the output amplifier to be powered with Arduino 5V Vcc without hitting the high output rail of the amp. Should the user want to use the full 0/5V range, the amplifier power supply should be raised above 8 volts to avoid hard clipping of the output. Usage of high precision resistors is mandatory to have a good linearity of the DAC, but this shall work properly with classical resistors, making this a cheap and easy way to have an 8 bits DAC.

Talking about lineraity, it’s not as good as a solid state converter, but the speed of this DAC thanks to the 74HC595 has been raised up to 332kHz.

By vertically connecting the resistors , one can get a reasonably small size on PCB, allowing design of simple shields like the one showed below: mixing a DAC, the output amplifier and a Midi receiver section with Optocoupler connected to Arduino serial Rx.

(Note : The schematic also shows a optocoupler used for Midi message reception, this has no direct link to the DAC purpose of this article)

Here is a picture of the prototype realised on veroboard. Concentrating the resistors on a veroboard allowed to leave more space on the breadboard for prototyping.


Upgrading the DIY R2R DAC

A word on the extension capability : by chaining 74HC595, it is very easy to get higher resolution DACs in theory. But in practice, taking into account that the accuracy of the resistors has to be at least equal to the DAC quantum (unity bit), a 16 bit DAC shall not provide the expected results, this is shown in the table below:

Bits 2 4 6 8 10 12 16
Accuracy 25% 6,25%1,56%0,39% 0,1% 0,02% 0,002%

6 bit DAC can be achieved accurately with 1% resistors. Above, you need 0,5% resistors for 8 bits DAC, and so on. Using 1% resistors for 8 bits is Ok anyhow, but increasing the number of bits leads to erratic behaviour.

So how does it work? Well, it’s doing well as you can see on the right. There’s still some issues linked to the 011111111 to 10000000 transition because of resistor inaccuracies, but this shall provide help to anyone of us Arduino users in the quest of getting a cheap but still efficient DAC!

Example : How to use the DAC as a DDS

To operate the DAC as a DDS, you just have to configure the SPI to communicate with the 74HC595 as follow.

insert this code in the setup() section of your Arduino sketch.

  // SPI Link Setup : Highest Speed Possible indeed !
  // Go Go Go !!
  byte clr;
  SPCR |= ( (1<<SPE) | (1<<MSTR) ); // enable SPI as master
  SPCR &= ~( (1<<SPR1) | (1<<SPR0) ); // clear prescaler bits
  clr=SPSR; // clear SPI status reg
  clr=SPDR; // clear SPI data reg
  SPSR |= (1<<SPI2X); // set prescaler bits

f you afterwards want to use it as a DDS to generate sine Waves, you need 3 things:

  • A 8 bit sine Look Up Table.
  • A counter to setup an interrupt
  • An interrupt handler to update the output sample.

Here we go, I used Timer2 configured as follow:

  // Configuration du Timer 2 pour la fréquence d'échantillonnage
  // No output (COM2A0 cleared, set = Toggle on Match)
  // Compteur en FAST PWM, Clear on Match OCR2A, Update at BOTTOM
  // Clock /8 (CS21 set, si set CS20 en plus /32)
  TCCR2A = _BV(COM2A0) | _BV(WGM21) | _BV(WGM20);
  TCCR2B = _BV(WGM22)  | _BV(CS21);
  TIMSK2 = _BV(TOIE2); // Enable timer overflow interrupt, once every 1/22039Hz = 45.375us.
  OCR2A = SMP_REG; // Use the right value here to define the sampling frequency

Then you create your Look Up Tables for the output, for example:

const unsigned char LUT_sin[]={
const unsigned char LUT_log[]={
const unsigned int LUT_freq[]={

We're almost there, since the Timer2 above has been configured to trigger an interrupt, here is the interrupt vector code:

// It
  acc += delta_acc; // update of phase accumulator
  sample = LUT_sin[acc>>8]; // >>8 bcz 'acc' is an int
  PORTB &= ~(4); // Fast RCK Toggling using direct port toggling
  PORTB |= 4;

There you are, you have a custom DDC with a DIY DAC for less than a Euro.


diy_spi_dac.txt · Last modified: 2018/08/26 11:36 (external edit)