DAC without PWM


For some reason, I wanted to check the current transfer ratio (CTR) of some of my optocouplers. To do this, I had to vary the current applied to the diode in small steps. And I did not want to use PWM with a low-pass filter.
I found an Arduino clone named Massduino that offers one DAC output without PWM. (Actually, there is also a second DAC output. But this one is not connected to any of the Arduino pins.)

Unfortunately, the DAC output of the Massduino has an extremly high impedance compared to the digital outputs offered by the ATmega328. So I had to add a non-inverting opamp to feed my optocoupler.

The results were quite satisfying. The scope shows the linear triangle output ranging from zero to 2.56 volts and the voltage drop at the resistor R2 ranging from zero to 5 volts.

Generating a triangle waveform is not very complicated:

/*
  Massduino 328D
  DACO output ==> D4 on board
*/

void setup() {
  Serial.begin(9600);
  Serial.println(__FILE__);
  analogReference(INTERNAL2V56);
  pinMode(DAC0, ANALOG);
  noInterrupts();
}

byte out = 0;
char inc = 1;

void loop() {
  analogWrite(DAC0, out);
  out = out + inc;
  switch (out) {
    case 255: inc = -1; break;
    case   0: inc =  1;
  }
}

Note that you have to install the Massduino hardware files provided by INHAOS.
After doing this, the lines

  analogReference(INTERNAL2V56);
  pinMode(DAC0, ANALOG);
will be accepted by the Arduino IDE pretty well.


contact: nji(at)gmx.de