0% found this document useful (0 votes)
160 views

mcp4725 Use Code

This document discusses using an MCP4725 DAC IC with an Arduino board to generate analog voltages from digital inputs. It includes the pin connections between the Arduino, MCP4725 breakout board, and an overview of the example code to set the DAC output voltage and read it with the Arduino analog pin. Key aspects covered are installing the MCP4725 library, initializing communication over I2C, using a for loop to set the DAC value and read the corresponding analog voltage in a loop.

Uploaded by

Shakirullah Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
160 views

mcp4725 Use Code

This document discusses using an MCP4725 DAC IC with an Arduino board to generate analog voltages from digital inputs. It includes the pin connections between the Arduino, MCP4725 breakout board, and an overview of the example code to set the DAC output voltage and read it with the Arduino analog pin. Key aspects covered are installing the MCP4725 library, initializing communication over I2C, using a for loop to set the DAC value and read the corresponding analog voltage in a loop.

Uploaded by

Shakirullah Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

#include <Wire.

h>//Include the Wire library to talk I2C

//This is the I2C Address of the MCP4725, by default (A0 pulled to GND).
//Please note that this breakout is for the MCP4725A0. 
#define MCP4725_ADDR 0x60   
//For devices with A0 pulled HIGH, use 0x61

//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 sintab2[512] = 
{
  2048, 2073, 2098, 2123, 2148, 2174, 2199, 2224  };

void setup()
{
  Wire.begin();

  // 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
  Wire.write(sintab2[lookup] >> 4); // the 8 most significant bits...
  Wire.write((sintab2[lookup] & 15) << 4); // the 4 least significant bits...
  Wire.endTransmission();
  lookup = (lookup + 1) & 511;
}
To give more power to Arduino board DAC (Digital to Analog
Converter) interfaced through breakout board and detailed Arduino
DAC Tutorial given in this article for the best understanding. Here
DAC IC MCP4725 based breakout board is used because it is 12-Bit
Digital-to-Analog Converter with EEPROM Memory, this IC utilize low
power and gives high accuracy output. This IC comes in sot package
hence better to go with breakout board in this tutorial we used
Sparkfun I2C Breakout MCP4725 board. By using this board we can
obtain Analog voltage from Arduino board depends on the digital input
and it accepts input in I2C format.
 MCP4725 DAC IC 

Features and Applications of MCP4725. This IC provides 12 bit


resolution and on board Board Non-Volatile Memory (EEPROM). It
can be addressed by external A0 address pin, and operates in Normal
or Power-Down Mode. It takes wide range of input supply voltage as
2.7V to 5.5V from single supply source. This IC provides eight address
through I2C and have Extended Temperature Range: -40°C to
+125°C.

Applications
Sensor Calibration
PC Peripherals
Data Acquisition Systems
Low Power Portable Instrumentation
 MCP4725 Arduino connection 

Construction & Working. Connect 5V pin and GND pin of Arduino


board to Breakout board VCC and GND pin then connect A4 (SDA),
A5 (SCL) pins with corresponding I2C pin of MCP 4725 brakout board.
If you using other Arduino board then refer and use the corresponding
I2C pins. Output pin of MCP4725 is connected with Analog pin A0 by
the way we can measure and display the Analog voltage obtained
from MCP4725 in serial monitor, you can also measure by using
digital voltmeter or multimeter.
 Arduino MCP4725 Code 

Here is an example code for the MCP4725 with the Arduino. You will
need to install the Adafruit_MCP4725.h library so downlaod it from
below. It will be a.zip file. In Arduino IDE, go to include library as .zip
file and select the downlaoded file. Now the library shoudl work.
Compile, make the connections and uplaod the code.

Download Adafruit_MCP4725.h

#include <Wire.h> //wire library


#include <Adafruit_MCP4725.h> // MCP4725 library from adafruit
#define analogVin A0 // Analog voltage input to A0

Adafruit_MCP4725 MCP4725;

void setup(void) {
Serial.begin(9600);
MCP4725.begin(0x60); // Default I2C Address of MCP4725 breakout board (sparkfun)
If not try 0x61 or 0x62

void loop(void) {

uint32_t MCP4725_value;
int adcValueRead = 0;
float voltageRead = 0;
float MCP4725_expected_output;

for (MCP4725_value = 0; MCP4725_value < 4096; MCP4725_value = MCP4725_value + 15)


{
MCP4725_expected_output = (5.0/4096.0) * MCP4725_value;
MCP4725.setVoltage(MCP4725_value, false);
delay(250);
adcValueRead = analogRead(analogVin);
voltageRead = (adcValueRead * 5.0 )/ 1024.0;

Serial.print("MCP4725 Value: ");


Serial.print(MCP4725_value);

Serial.print("\tExpected Voltage: ");


Serial.print(MCP4725_expected_output,3);

Serial.print("\tArduino ADC Value: ");


Serial.print(adcValueRead);

Serial.print("\tArduino Voltage: ");


Serial.println(voltageRead,3);
}
}

You might also like