MCP4725 Generating a sine / triangle wave and controlling it

For other supported Arduino products from Adafruit: Shields, accessories, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
DanMx93
 
Posts: 2
Joined: Thu Nov 10, 2016 5:15 pm

MCP4725 Generating a sine / triangle wave and controlling it

Post by DanMx93 »

Hi, I hope someone can help me.
I'm using the DAC mcp4725 for generate triangle and sine waves and I have some questions.
How can I control the frequency of the wave I'm generating?
For example, I want a 10Khz triangle wave.
I follow the steps on this post
viewtopic.php?f=22&t=88259
But I can't get the frequency that I want.

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: MCP4725 Generating a sine / triangle wave and controllin

Post by adafruit_support_mike »

Post the code you have now (between CODE tags please, the </> button) and we'll take a look.

User avatar
DanMx93
 
Posts: 2
Joined: Thu Nov 10, 2016 5:15 pm

Re: MCP4725 Generating a sine / triangle wave and controllin

Post by DanMx93 »

This is the code:

Code: Select all

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

Adafruit_MCP4725 dac;

long freq = 10000;
long microSeconds = freq / 1000000;

const PROGMEM uint16_t up[8] = {
  0, 2, 4, 8, 16, 32, 64, 128
};

const PROGMEM uint16_t down[8] = {
  128, 64, 32, 16, 8, 4, 2, 0
};

int wavelenght = 8;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  dac.begin(0x62);

}

void loop() {
  // put your main code here, to run repeatedly:

  for(int i = 0; i < wavelenght; i++){
    dac.setVoltage(pgm_read_word(&(up[i])), false);
  }
  for(int i = 0; i < wavelenght; i++){
    dac.setVoltage(pgm_read_word(&(down[i])), false);
  }

  delayMicroseconds(microSeconds);
}

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: MCP4725 Generating a sine / triangle wave and controllin

Post by adafruit_support_mike »

You have an inversion error.

The delay between samples is called the sampling period, and in general, period is a length of time divided by how often something happens. To find the period of a 10kHz signal in microseconds, you'd start with 1,000,000 microseconds per second, and divide that by the number of cycles per second (10,000). The result would be the number of microseconds per cycle: 100uS.

To get the sampling rate, you need to factor in the number of samples per cycle.. for your code, there are 16.. 8 going up and another 8 coming down. If a whole cycle of the 10kHz wave lasts 100uS and you have 16 samples per cycle, each gap needs to last 6.25uS.

Locked
Please be positive and constructive with your questions and comments.

Return to “Other Arduino products from Adafruit”