Digital to analogue (DAC) - mcp4728 library - not working?

Dear List,
There was a useful looking thread from a few years ago on a library developed for the mcp4728 Quad output DAC chip. I was going to reply on the thread but it has been dead for a while so I was advised to start a new thread. The link is here:

http://forum.arduino.cc/index.php?topic=51842.0

I have tried this library using an arduino Uno but it doesn't seem to work. At least I can't get it to work. I wonder if this is because it is no longer compatible with Arduino versions > 1.0

Is there any interest in this any more? i think this is a great idea for analogue outputs but I can't get the library to work. Is there anyone out there who is using this resource who can offer some advice? As far as I can tell, i have wired it up correctly.

THanks,
NickH

might be relative easy to fix

#if defined(ARDUINO) && ARDUINO >= 100
#define WIRE_WRITE Wire.write
#define WIRE_READ Wire.read
#else
#define WIRE_WRITE Wire.send
#define WIRE_READ Wire.receive
#endif

and replace the Wire.send calls with WIRE_WRITE

Thanks Rob.. but where should this be?
In the library (.cpp or .h) or is this in the sketch?
NickH

might be relative easy to fix

#if defined(ARDUINO) && ARDUINO >= 100
#define WIRE_WRITE Wire.write
#define WIRE_READ Wire.read
#else
#define WIRE_WRITE Wire.send
#define WIRE_READ Wire.receive
#endif

and replace the Wire.send calls with WIRE_WRITE

Answering my own reply, I guess this would be in the library with the definitition in .h and the new definition used in .cpp

It seems that from Arduino version 1.0 onwards, then the commands wire.send should be wire.write and wire.receive should be wire.read and this is a nice way of making the library compatible with older versions of the arduino software.

However, the library has already been updated to a 1.0 compatible version and from what I can see, this represents changes along the line that Rob has recommended.

so this seems not to be the problem.

I am now not sure if this is a hardware or software problem.

I have tried using pull up resistors to SCL and SDA inputs (A4 and A5) and tried removing them as a post elsewhere seemed to suggest these pins already had pull up resistors.

Any other suggestions?

Hopefully,
NickH

Ok, well I will post the answer in case anyone else is struggling. Rather obvious when you know how!!!

The library is fine but it will only work if it uses the correct address for the I2C device. The code seemingly is wired to what is supposed to be the default address for the chip (0). However, I needed to find an I2C scanning bit of code:

http://playground.arduino.cc/Main/I2cScanner

With this, it told me the address of my I2C device was 0x61. By letting the sketch know that this was the correct address, everything started to work.

I am not currently using pull up resistors. Not sure how important this is so if anyone can advise, please let me know.

THanks,
NickH

Read all about I2C here - Gammon Forum : Electronics : Microprocessors : I2C - Two-Wire Peripheral Interface - for Arduino -

in the 2nd part the reasons for pull up s are explained.

Saw this thread and wanted to add an example for other potential Arduino noobs. I recently purchased a Due and the Microchip MCP4728 evaluation board (eg. available from Digikey as part number MCP4728EV-ND, $15). I got this working realtively easily using the library referenced at the start of this theaad, with only some minor edits to the .cpp and .h files (changed wire.send() to wire.write(), wire.receive() to wire.read(), and WProgram.h to Arduino.h). I also added some comments to the original example sketch "BasicUse" and renamed that to MCP4728B_web.ino which is posted here. I have the mcp4728 library files in the usual Arduino install folder location (/libraries folder).

I'll try to attach the sketch and .cpp and .h files here (first post so I may botch this part). The MCP4728 eval board has 5K pullups on the SDA and SCL lines, so those were in parallel with the 1.5K pullups on the Due board (or, some forum comments indicate these may be 1K pullups instead, despite the schematic notation). Things worked despite the relatively low value of the parallel combination of pullups (either 833 ohm or 1154 ohm depending on the value of the Due pullups). The 5K pullups on the eval board are easily removed but things worked with them in place.

Lastly, I used the default MCP4728 I2C address, but the original library contains another example called changeDeviceID which can be used to change the I2C address. The file TwoWireBase.h in the SoftI2CMaster folder also must have WProgram.h changed to Arduino.h. Use that example to change the MCP4728 I2C address if necessary (I have not tried this yet but plan to shortly).

Overall, very easy to use this DAC with the Due and get four honest 0V to Vdd 12-bit outputs (or if using the internal reference 0 - 2.048V or 0- 4.096V output ranges). The Due DACs seem to operate only over part of the range (0.55V to 2.75V according to other forum posts).

mcp4728.cpp (11.5 KB)

mcp4728.h (2.43 KB)

MCP4728_Web.ino (10.9 KB)

Im currently working on a project that requires 4 analog outputs. Im using the arduino uno and was able to generate a sine signal using a resistor ladder set up. however the project requires 4 sine signals outputs. I want to replace the resistor ladder with the Mcp4728 so il be able to get the 4 outputs required. im new to the this and need some help. how to i get this set up working??

Buy a chip, wire it up per the datasheet,Table 3-1, Sections 3.1 to 3.6
Send it setup data. Then, you can send it the data you want the outputs to change to.
Really pretty simple if you can follow the datasheet.

I got the chip with the evaluation board. i connected the A4 and A5 pins of the Uno to the SCL and SDA pins on the board. Grounded the LDAC pin. I ran the 'MCP4728_Web.ino' given above and got the constant voltages at the analog pins. the code below is what im using to get my sine signal. i got this from another forum. How to i modify the 'MCP4728_Web.ino' code to output this instead??

byte sine[] = {127, 134, 142, 150, 158, 166, 173, 181, 188, 195, 201, 207, 213, 219, 224, 229, 234, 238, 241, 245, 247, 250, 251, 252, 253, 254, 253, 252, 251, 250, 247, 245, 241, 238, 234, 229, 224, 219, 213, 207, 201, 195, 188, 181, 173, 166, 158, 150, 142, 134, 127, 119, 111, 103, 95, 87, 80, 72, 65, 58, 52, 46, 40, 34, 29, 24, 19, 15, 12, 8, 6, 3, 2, 1, 0, 0, 0, 1, 2, 3, 6, 8, 12, 15, 19, 24, 29, 34, 40, 46, 52, 58, 65, 72, 80, 87, 95, 103, 111, 119,};

void setup(){
 //set digital pins 0-7 as outputs
 for (int i=0;i<8;i++){
   pinMode(i,OUTPUT);
 }
}

void loop(){
 for (int t=0;t<100;t++){//increment "t"
   PORTD = sine[t];//send sine wave to DAC, centered around (127/255)*5 = 2.5V
   delayMicroseconds(9);
 }
}