Electronic porject by Lewis Loflin


Arduino with MCP4725 12-bit Digital-to-Analog Converter Demo

by Lewis Loflin

In this demo Arduino reads the value of a potentiometer connected to ADC0 which is a 10-bit value. This is multiplied by 4 to a 12-bit values then written through an I2C connection produces an out voltage from 0-5V based on the pot value.

Arduino sketch for this project: mcp4725_demo.ino


/*

 By Lewis Loflin lewis@bvu.net
 http://www.sullivan-county.com/main.htm
 Electronics website:
 http://www.bristolwatch.com/index.htm

 Reads pot on Arduino ADC0 (10-bit) then multiplies by 4.
 Transmits via I2C numeric values controls
 the output voltage on a MCP4725 DAC
 measured with a voltmeter.

 Configuration bytes:
 // 12-bit device values from 0-4095
 // page 18-19 spec sheet
 buffer[0] = 0b01000000; // control byte
 // bits 7-5; 010 write DAC; 011 write DAC and EEPROM
 // bits 4-3 unused
 // bits 2-1 PD1, PD, PWR down P19 00 normal.
 // bit 0 unused

 buffer[1] = 0b00000000; //HIGH data
 // bits 7-0 D11-D4

 buffer[2] = 0b00000000; // LOW data
 // bits 7-4 D3-D0
 // bits 3-0 unused
*/

#include <Wire.h> // specify use of Wire.h library
#define MCP4725 0x62 // MCP4725 base address

unsigned int val;
byte buffer[3];

void setup()   {

  Wire.begin(); // begin I2C

}  // end setup

void loop() {

  buffer[0] = 0b01000000; // control byte
  val = analogRead(0) * 4; // read pot
  buffer[1] = val >> 4; // MSB 11-4 shift right 4 places
  buffer[2] = val << 4; // LSB 3-0 shift left 4 places

  Wire.beginTransmission(MCP4725); // address device
  Wire.write(buffer[0]);  // pointer
  Wire.write(buffer[1]);  // 8 MSB
  Wire.write(buffer[2]);  // 4 LSB
  Wire.endTransmission();

  // just an indicator
  digitalWrite(13, HIGH);
  delay(100);
  digitalWrite(13, LOW);
  delay(100);

} // end loop