GENERATING SINEWAVE WITH MCP4725 DAC AND ARDUINO UNO:

Component list:
1. MCP4725 - I2C 12 Bit DAC module for Arduino
2. Arduino UNO board

Connection for Normal Operation:


But in our test setup, for simplicity, board can be powered with two address lines A2 & A3 as bellow.
This is possible, because MCP4725 draw few milliamps.

Sample Arduino Code:

/****************************************************************************** 
Ref: https://github.com/sparkfun/MCP4725_Breakout

This sketch takes data from a lookup tables to provide 
waveforms to be generated by the MCP4725 DAC. 
This code builds off the sketch written by Mark VandeWettering, which can be found here:
http://brainwagon.org/2011/02/24/arduino-mcp4725-breakout-board/
Using only the basic library "Wire.h"
MCP4725 - 12Bit DAC 
Multi resolution selectable.
Modified by AMP Technology - amptech.co.nz
*/

#include <Wire.h> //Include the Wire library to talk I2C

//This is the I2C Address of the MCP4725, by default (A0 pulled to GND).
//If you pulled A0 HIGH on the pcb, use 0x61
#define MCP4725_ADDR 0x60   

// Set this value to 7, 6 or 5 to adjust the resolution
#define DAC_RESOLUTION    (7)

//Sinewave Tables were generated using this calculator:
//http://www.daycounter.com/Calculators/Sine-Generator-Calculator.phtml

int lookup = 0;  //varaible for navigating through the tables

int sintabl_7Bit[128] =
{
  2048, 2148, 2248, 2348, 2447, 2545, 2642, 2737,
  2831, 2923, 3013, 3100, 3185, 3267, 3346, 3423,
  3495, 3565, 3630, 3692, 3750, 3804, 3853, 3898,
  3939, 3975, 4007, 4034, 4056, 4073, 4085, 4093,
  4095, 4093, 4085, 4073, 4056, 4034, 4007, 3975,
  3939, 3898, 3853, 3804, 3750, 3692, 3630, 3565,
  3495, 3423, 3346, 3267, 3185, 3100, 3013, 2923,
  2831, 2737, 2642, 2545, 2447, 2348, 2248, 2148,
  2048, 1947, 1847, 1747, 1648, 1550, 1453, 1358,
  1264, 1172, 1082,  995,  910,  828,  749,  672,
   600,  530,  465,  403,  345,  291,  242,  197,
   156,  120,   88,   61,   39,   22,   10,    2,
     0,    2,   10,   22,   39,   61,   88,  120,
   156,  197,  242,  291,  345,  403,  465,  530,
   600,  672,  749,  828,  910,  995, 1082, 1172,
  1264, 1358, 1453, 1550, 1648, 1747, 1847, 1947
};

int sintabl_6Bit[64] =
{
  2048, 2248, 2447, 2642, 2831, 3013, 3185, 3346,
  3495, 3630, 3750, 3853, 3939, 4007, 4056, 4085,
  4095, 4085, 4056, 4007, 3939, 3853, 3750, 3630,
  3495, 3346, 3185, 3013, 2831, 2642, 2447, 2248,
  2048, 1847, 1648, 1453, 1264, 1082,  910,  749,
   600,  465,  345,  242,  156,   88,   39,   10,
     0,   10,   39,   88,  156,  242,  345,  465,
   600,  749,  910, 1082, 1264, 1453, 1648, 1847
};

int sintabl_5Bit[32] =
{
  2048, 2447, 2831, 3185, 3495, 3750, 3939, 4056,
  4095, 4056, 3939, 3750, 3495, 3185, 2831, 2447,
  2048, 1648, 1264,  910,  600,  345,  156,   39,
     0,   39,  156,  345,  600,  910, 1264, 1648
};


void setup()
{
  Wire.begin();
//--------------------------------------------------------
  //For test setup A2- as GND and A3 as +VCC for the DAC board.
  //Otherwise remove this 4 lines of cord.
  // Set A2 and A3 as Outputs to make them our GND and Vcc,
  //which will power the MCP4725
  pinMode(A2, OUTPUT);
  pinMode(A3, OUTPUT);

  digitalWrite(A2, LOW);//Set A2 as GND
  digitalWrite(A3, HIGH);//Set A3 as Vcc
//---------------------------------------------------
}

void loop()
{
  Wire.beginTransmission(MCP4725_ADDR);
  Wire.write(64);                     // cmd to update the DAC
  //around 73Hz Sine Wave 
  if (DAC_RESOLUTION == 5) {  
    Wire.write(sintabl_5Bit[lookup] >> 4);        // the 8 most significant bits of 12bits
    Wire.write((sintabl_5Bit[lookup] & 15) << 4); // the 4 least significant bits of 12bits
    Wire.endTransmission();
    lookup = (lookup + 1) & 31;
  }
  //around 36Hz Sine Wave 
  else if (DAC_RESOLUTION == 6) {
    Wire.write(sintabl_6Bit[lookup] >> 4);        // the 8 most significant bits of 12bits
    Wire.write((sintabl_6Bit[lookup] & 15) << 4); // the 4 least significant bits of 12bits
    Wire.endTransmission();
    lookup = (lookup + 1) & 63;
  }
  //around 18Hz Sine Wave 
  else if (DAC_RESOLUTION == 7) {
    Wire.write(sintabl_7Bit[lookup] >> 4);        // the 8 most significant bits of 12bits
    Wire.write((sintabl_7Bit[lookup] & 15) << 4); // the 4 least significant bits of 12bits
    Wire.endTransmission();
    lookup = (lookup + 1) & 127;
  }
}
Download complete code.





0Comments

For Comment you need to Login


Related Product

MCP4725 I2C 12 Bit DAC module for Arduino

MCP4725 I2C 12 Bit DAC module for Arduino

MCP4725 IIC I2C DAC Breakout module for Arduino:MCP4725 is Microchip I2C 12-Bit Digital to Analog Co..

$5.25