I recently built a 12-bit serial DAC, using a pair of 74HC595 shift registers and a R/2R ladder. I know that the entire circuit can be replaced with a single IC. But I did for 2 reasons (1) To learn about R/2R ladder DACs and (2) I could not find 12-bit DAC ICs here. The end result might be wobbly, but it sure is cheap! (Rs. 25 vs Rs. 300).
Note
Since posting this, I have found that this method is not particularly accurate. See for instance the post here.
The Circuit
The main components here are the pair of 74HC595 shift registers, the R/2R resistor ladder and the LM358 Op Amp.
The shift registers are used to shift out the serial 12-bit data. Since we are sending 12 bits, we can't use the built in shiftOut() method, and have to resort to some direct port manipulation code for the same. The shift registers are connected to the Arduino as shown in the schematic on the Arduino ShiftOut tutorial.
The R/2R ladder is the core of the DAC. I used a set of single 100 K Ohm 1% resistors. You can read details about the R/2R ladders here.
The LM358 is configured as a voltage follower, since it isolates the R/2R network with a high input impedance.
The power supply to the Op Amp is separate 9V battery. I tried powering it with just the 5V from the Arduino, but it does not operate very well at inputs close to the supply voltage.
The Code
/*
* ShiftOutTest
*
* MV (http://electro-nut.blogspot.com/)
*
* This tests a 2 x 74HC595 + R/2R Ladder 12-bit DAC
*
*/
// 74HC595 pins
int pin_SH_CP = 6;
int pin_ST_CP = 7;
int pin_DS = 5;
// values
int readings[] = {
0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200,
1300,1400, 1500, 1600, 1700, 1800, 1900, 2000, 2100, 2200, 2300,
2400, 2500, 2600, 2700,
2800, 2900, 3000, 3100, 3200, 3300, 3400, 3500, 3600, 3700, 3800,
3900, 4000, 4096};
int N = sizeof(readings)/sizeof(int);
void setup()
{
pinMode(pin_SH_CP, OUTPUT);
pinMode(pin_ST_CP, OUTPUT);
pinMode(pin_DS, OUTPUT);
}
void loop()
{
for(int i = 0; i < N; i++) {
// ST_CP low
digitalWrite(pin_ST_CP, LOW);
// shift 12 LSB-first bits of data to shift register
shiftOut12(readings[i]);
// ST_CP high
digitalWrite(pin_ST_CP, HIGH);
// change this delay to suit your reading needs
delay(1000);
}
}
// shift out 12 bits of data
void shiftOut12(int data)
{
for(int i = 0; i < 12; i++) {
// SH_CP low
//PORTD &= ~(1<<6);
digitalWrite(pin_SH_CP, LOW);
// DS
// is i-th bit of data high?
if(data & (1<<i)) {
PORTD |= (1<<5);
}
else {
PORTD &= ~(1<<5);
}
// SH_CP high
//PORTD |= (1<<6);
digitalWrite(pin_SH_CP, HIGH);
}
// SH_CP low
//PORTD &= ~(1<<6);
digitalWrite(pin_SH_CP, LOW);
}
Results
I took 42 readings using a multimeter (enlisted the help of my wonderful wife - thanks, dear!), and the response does look very linear:
In a real application, I'd definitely got for a serial DAC IC - but it was nice to discover that it can be done this way!
Acknowledgments
Thanks to the excellent Arduino Forum and its members for helpful posts and discussions.
References
1. The Arduino ShiftOut Tutorial
http://www.arduino.cc/en/Tutorial/ShiftOut
2. Practical Electronics for Inventors by Paul Scherz
[...CLICK HERE to read the full post...]