W E L C O M E

Namaste and Welcome to the "Nuts About Electronics" Blog.

I am an electronics hobbyist based in India. Recently, I rediscovered my childhood passion in tinkering with electronic circuits. I created this blog to share some of my projects, as well as provide some useful information to other hobbyists in India, where many electronic components are hard to find.

Please note that the projects as well as code in this blog are provided "as is", with no guarantees. Working with electricity, electronic components, soldering irons, etc. requires safety precautions - please use your common sense.

Good luck with your projects, and above all, have FUN!

MV

Monday, August 3, 2009

Making a 12-bit Serial DAC



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...]

Saturday, August 1, 2009

The Karplus-Strong Algorithm

As part of my ongoing audio projects with Arduino, I was looking for a programmatic way of generating complex sounds. It is simple enough to create a pure tone with a sine wave. But real sounds are complex, with mixtures of overtones or frequencies that rise and fall over time. I recently found an interesting algorithm called "Karplus-Strong string synthesis" which simulates the sound of a plucked string. I wrote a program in Python to explore this algorithm, and I think the sounds produced by this algorithm are just amazing.

First, listen to the 2 samples below, produced by the algorithm:

1. simple.wav
2. ks.wav

Now for the code that produced these:

Here is a simple Python program that generates the sound from a single plucked string, and writes it out to a WAV file.


############################################################
#
# karplus-simple.py
#
# Author: MV (http://electro-nut.blogspot.com/)
#
# Generates a plucked string sound WAV file using the
# Karplus-Strong algorithm. (Simple version.)
#
############################################################
from math import sin, pi
from array import array
from random import random

import wave

# KS params
SR = 44100
f = 220
N = SR/f

# WAV params
NCHANNELS = 1
SWIDTH = 2
FRAME_RATE = 44100
NFRAMES = 44100
NSAMPLES = 44100
# max - 128 for 8-bit, 32767 for 16-bit
MAX_VAL= 32767

# pluck
buf = [random() - 0.5 for i in range(N)]

#init samples
samples = []

# KS - ring buffer
bufSize = len(buf)
for i in range(NSAMPLES):
samples.append(buf[0])
avg = 0.996*0.5*(buf[0] + buf[1])
buf.append(avg)
buf.pop(0)

# samples to 16-bit to string
tmpBuf = [int(x*MAX_VAL) for x in samples]
data = array('h', tmpBuf).tostring()

# write out WAV file
file = wave.open('simple.wav', 'wb')
file.setparams((NCHANNELS, SWIDTH, FRAME_RATE, NFRAMES,
'NONE', 'noncompressed'))
file.writeframes(data)
file.close()


Here is a more object oriented version of the code above, which lets you mix sounds from multiple string plucks.


############################################################
#
# karplus.py
#
# Author: MV (http://electro-nut.blogspot.com/)
#
# Generates a plucked string sound WAV file using the
# Karplus-Strong algorithm.
#
############################################################
from math import sin, pi
from array import array
from random import random
import wave

# KS params
SR = 44100
f = 220

# WAV params
NCHANNELS = 1
SWIDTH = 2
FRAME_RATE = 44100
NFRAMES = 44100
NSAMPLES = 44100*2
# max - 128 for 8-bit, 32767 for 16-bit
MAX_VAL= 32767

class String:
def __init__(self, freq, SR):
self.freq = freq
self.N = SR/freq
# 'pluck' string
def pluck(self):
self.buf = [random() - 0.5 for i in range(self.N)]
# return current sample, increment step
def sample(self):
val = self.buf[0]
avg = 0.996*0.5*(self.buf[0] + self.buf[1])
self.buf.append(avg)
self.buf.pop(0)
return val

str1, str2 = String(196, SR), String(440, SR)

str1.pluck()
str2.pluck()

samples = []

for i in range(NSAMPLES):
sample = str1.sample()
if(i > NSAMPLES/8):
sample += str2.sample()
samples.append(sample)

# samples to 16-bit to string
tmpBuf = [int(x*MAX_VAL) for x in samples]
data = array('h', tmpBuf).tostring()

# write out WAV file
file = wave.open('ks.wav', 'wb')
file.setparams((NCHANNELS, SWIDTH, FRAME_RATE, NFRAMES,
'NONE', 'noncompressed'))
file.writeframes(data)
file.close()



It remains to be seen how I can adapt this to the Arduino. But it has been a fun project!

References

1. You can read details about the algorithm here:

http://en.wikipedia.org/wiki/Karplus-Strong_string_synthesis

2. Here is a programming assignment from Princeton which started me off on this project:

http://www.cs.princeton.edu/courses/archive/spring09/cos126/assignments/guitar.html



[...CLICK HERE to read the full post...]