Arduino Due and MCP4901 DAC

Test sketch

#include <SPI.h>
#define CS 52

void setup() {

SPI.begin(CS);
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(CS, 3);
SPI.setClockDivider(CS,21); // 4MHz
}

void loop() {
byte msg1=0b11111111;
byte msg2=0b00000000;
SPI.transfer(CS,msg1,SPI_CONTINUE);
delay(1000);
SPI.transfer(CS,msg2,SPI_LAST);
delay(1000);
}

There is no output at all from the due

next try

#include <SPI.h>

void setup(){
	// initialize the bus for a device on pin 52
	SPI.begin(52);
	SPI.setClockDivider(52, 21);
}

void loop(){

SPI.transfer(52, 0xFF, SPI_CONTINUE); //transfer 0xFF to the device on pin 52, keep the chip selected
digitalWrite(3, HIGH);
delay(1000);
SPI.transfer(52, 0x00, SPI_CONTINUE); //transfer 0x00 to the device on pin 52, keep the chip selected
digitalWrite(3, LOW);
delay(1000);
}

only thing that works is digitalwrite on pin 3

wiring
pinout

Some years ago I found this .pdf
Arduino_DUE___DAC_MCP4922.pdf (567.5 KB)

Some years ago I found this .pdf
Arduino_DUE___DAC_MCP4922.pdf (567.5 KB)

this is what I ve tried in the first sketch

Due has 2 built in DACs of a better resolution, any reason why not use them?

they might be to slow for my application, anyway how do I use them?

this doesn't work

void setup() {  
  pinMode (DAC1,OUTPUT);
  
}

void loop() {
  analogWriteResolution(12); //12 bits resolution
  analogWrite(DAC1, 4096); //output values to the Digial to Analog Converter
  delay(100);
  analogWrite(DAC1, 0); 
  delay(100);

}

Try 4000 instead of 4096.

Anyway, if voltage doesn't move on DAC1 pin, you might have burn your DAC. Always connect at least a 1.5 KOhms resistor in serie with DAC0 or DAC1 since these pins can't output much current without burning.

The range for 12 bit resolution is 0-4095. I have Due, will test your sketch later

Tested it, and DAC outoput changes from 0.55V (0) to 2.73V(4095) without any load, just a multimeter connected to the ground and DAC

1 Like

FYI, a multimiter is somehow an infinite load... There is an issue whenever you connect a DAC to a very light load, hence the suggested 1.5 K Ohms resistor since a DAC can output a maximum of
3 mA.

the values i got right on the spot no idea why yo don’t like them

Thanks for the inputs/testing and yes 4096 seems to be my mistake.
Is there a way to get 0V out of the internal DAC?

No, the internal DAC will not produce rail to rail voltage outputs. It only produces from 1/6 to 5/6 of the reference voltage, that is, 0.55 V and 2.75V with Vref = 3.3 V.

See this project for a way of correcting this by using an operational amplifier.
Correcting the output range

1 Like

If you don’t mind to add operational amplifier, which you probably should anyway Arduino Project Hub

It works :sunglasses: is there a faster script to drive the internal dac?
I ve read somewhere its up to 1.7Mhz, I ve got about 0.3MHz

I reduced the resolution to 5 bits and the speed would be about right, but 5 bits isn't an acceptable resolution.

To get back to the topic, I am still trying to figure out what the SPI issue is :thinking:

#include <SPI.h>

void setup(){
	// initialize the bus for a device on pin 52
	SPI.begin(52);
	SPI.setClockDivider(52, 21);
}

void loop(){

	SPI.transfer(52, 0xFF, SPI_CONTINUE); //, SPI_CONTINUE); //transfer 0xFF to the device on pin 52, keep the chip selected
  //digitalWrite(3, HIGH);
	//delay(1);
	SPI.transfer(52, 0x00, SPI_LAST); //, SPI_CONTINUE); //transfer 0x00 to the device on pin 52, keep the chip selected
  //digitalWrite(3, LOW);
	//delay(1);
}

At least I ve got output from the due now

TEK00002
due_pin52

TEK00004
due_sck

TEK00005
due_mosi

TEK00006
OUT

doesn't look like there is actual SPI data getting transmitted

Your SPI code is all wrong.
See:-
https://www.arduino.cc/en/Reference/SPI

Also look at the data sheet for the MCP49x1, you wil see you need to set some status bits in the most significant nibble of the higher byte, not just put zero in it.



Datasheet

Can't believe this is such a major PITA on the DUE, had it all running on the UNO using the MPC49XX library

So I guess I should set SPI Mode to 1 and send something like

bit15 <------------------------ bit0
0 0 1 1 0 0 0 0 0 0 0 0 X X X X = off
0 0 1 1 1 1 1 1 1 1 1 1 X X X X = on

#include <SPI.h>

void setup(){
	// initialize the bus for a device on pin 52
	SPI.begin(52);
  SPI.beginTransaction(SPISettings(14000000, MSBFIRST, SPI_MODE1));
}

void loop(){

	SPI.transfer(52, 0b00110000, SPI_CONTINUE); //, SPI_CONTINUE); //transfer 0xFF to the device on pin 52, keep the chip selected
	//delay(10);
  SPI.transfer(52, 0b00000000, SPI_CONTINUE);
  delay(10);
  SPI.transfer(52, 0b00111111, SPI_CONTINUE);
  //delay(10);
  SPI.transfer(52, 0b11111111, SPI_CONTINUE);
  delay(10);
}

no µC output :see_no_evil:

No.
You only need to send 16 bits, that is two bytes, not 3.
Why do you still use a number in the brackets in the SPI begin when the documentation says it takes no parameters?
Why have you not set your chip select pin to be an output?

Why do you never release the chip select?

1 Like

You only need to send 16 bits, that is two bytes, not 3.

Where does it send 3 bytes? I think it sends 4x 1 byte including these don't care bits

Why do you still use a number in the brackets in the SPI begin when the documentation says it takes no parameters?
Why have you not set your chip select pin to be an output?

Did it just like here:

Some years ago I found this .pdf
Arduino_DUE___DAC_MCP4922.pdf (567.5 KB)

But I will edit my previous post/code

Why do you never release the chip select?

I had it constantly pulled down to GND while I was using the UNO, does this matter?

Yes and that PDF was full of the author saying “I don’t know ….” To me this says that he doesn’t know so why take his word for anything that contradicts the official documentation?

Yes it matters, without releasing the chip select the chip will not work correctly.

What bits are these? I can’t see any reference to don’t care. At the moment this doesn’t matter as you are not sending any data on those pins anyway, but once you get that bit going then it will matter to the A/D chip.

In your only diagram you didn’t indicate what actual PIN numbers you used for data and clock.

1 Like