MCP4725 Rect pulses code

#include <Wire.h>
#include <Adafruit_MCP4725.h>

Adafruit_MCP4725 dac;
#define DAC_RESOLUTION      (12)
#define DAC_ARRAY_INDICES (2)

const PROGMEM uint16_t RectLookup_5bits[DAC_ARRAY_INDICES] =
{
  4095,
  0
};

void setup() {
  dac.begin(0x60);
}

void loop() {
  for (int i = 0; i < DAC_ARRAY_INDICES; i++) {
    dac.setVoltage(pgm_read_word(&(RectLookup_5bits[i])), false);
    delayMicroseconds(5000);
  }
}

I have this code. MCP4725 DAC Code.
I am taking rectangular pulse output with frequency of 3.2 kHz maximum. Whatever I increase or decrease the delay at the end, this frequency does not increase. I want it to be 20 kHz.
Here the output.

For 20Khz then you should have a delay of no more than 25us. This would be 25us at voltage and 25us at 0 voltage; however this assumes that

dac.setVoltage(pgm_read_word(&(RectLookup_5bits[i])), false);

executes in 0 us! It turns out that if you look at the setVoltage() method prototype it sets the i2c frequency to 400000. 3 bytes are transmitted at 400Khz over i2c which takes approximately 60us. Therefore there is no way to to reach 20Khz with this code.

What type of Arduino are you using? You should be able to easily generate a 20Khz signal using a time library. I have used TimerOne library to generate square waves. You can also set the duty cycle as well.

A DAC is not the best way to generate a square wave.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.