R2R DAC ladder problems

Hello
I have been wanting to create a DAC using resistors as a project for my learning in Arduino. I stumbled upon the R2R ladder method where I am using 2k 1/2 watt and 1k 1/2 watt resistors to create the DAC. I have been having many issues with this project and was wishing for some help. My first issue is that I am not getting use of all of the 8 bits and instead only the most significant bit. Next the results from the DAC are the same so matter if I have ground connected at the least significant bit end. I did some research and found that I have have the R and 2R resistors around the wrong way, but again the same results when they were around the other way. To present my results I am using the serial plotter in the Arduino IDE which when I try to create a sin wave gives a rather interesting version of the graph which looks more like a large staircase going up and then one going back down with about 8 steps in each.Sorry I haven't figured out how to attach an image.
This is really confusing me as I have no clue what is going on. I think that my code is okay too, here it is

void setup() {
  // put your setup code here, to run once:
for(int i=4; i<=7;i++){
  pinMode(i,OUTPUT);
}
Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:
 
for ( int i = 0; i < 256; i++) { 
    PORTD = i;
    delay(10); 
    Serial.println(analogRead(A0)); 
  }
for ( int i = 255; i >-1; i--) { 
    PORTD = i;
    delay(10); 
    Serial.println(analogRead(A0)); 
  }
}

I am reasonably new to Arduino and electronics in general and so I thought that this project would help to improve my knowledge but it is instead turning into a nightmare. If someone could please help me that would be much appreciated.
cheers

So you are setting only four bits as OUTPUT.

A four bit number gives you sixteen possible steps, and your simple up-down ramp code would be expected to give you a staircase, each step occurring after 16 iterations of the low nybble.

Why are you surprised? :roll_eyes:

Port D bits on a 328 (Uno) are used for many hardware functions, like RXD and TXD used in Serial I/O.
Better outputs are full 8 bit ports (Mega) or shift registers where all bits really can change at the same time.

DrDiettrich:
Port D bits on a 328 (Uno) are used for many hardware functions, like RXD and TXD used in Serial I/O.

So six bits are available, but he is using only four. :roll_eyes:

But 8 steps indicate something more is wrong.

tdean_22:
with about 8 steps in each.

His "about" sounds close enough to 16 for my liking. :roll_eyes:

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Rather than do port manipulation first, why don't you use digitalWrite and just turn on outputs as you need them, using a DMM to check if the correct output is HIGH.

Tom.... :slight_smile:

TomGeorge:
why don't you use digitalWrite and just turn on outputs as you need them,

For testing, or the "Real Thing"?

That would result in awful transients due to intermediate stages.

Paul__B:
For testing, or the "Real Thing"?

That would result in awful transients due to intermediate stages.

Testing at slow change rate ... to check integrity of circuit.

Port manipulation will be the most efficient and precise..

Tom..... :slight_smile: :slight_smile: :slight_smile: :slight_smile:

Thanks for the replies. One thing that a friend had suggested to me was that maybe it is grounding out through the pins when they are set as LOW and is why it is not needing a ground pin. What would be a way which I could stop the circuit shorting out through an output pin and have it go through the normal ground?
I have hopefully attached an image of both my circuit and the simple serial plotter which I described the staircase in earlier.

Screen Shot 2020-05-17 at 9.45.30 PM.png

Photo on 24-05-20 at 8.57 PM.jpg
Well, with that photo taken in darkness, I cannot check what resistors you are using or your wiring, but it most certainly is not working and if your code is simply incrementing and decrementing I have to conclude that something is badly wrong in the wiring as there are indeed only eight steps and not 16. :roll_eyes:


I am going to hazard a theory that the blue wire is open circuit or not connected properly, or that its associated resistor is not connected.

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Have a read of this link, it will tell you how to do port manipulation correctly.

Thanks.. Tom.. :slight_smile:

The wiring is okay as far as the staircase has no missing or non-monotonous steps. It's only resistors of too much tolerance that make an unequal step height.

Hi,
You need to change only bits 4,5,6 and 7 in PORTD.
You need to count from 0 to 15 to get your steps.

So you need to bit shift 0 to 15 4 bits left in PORTD.
https://www.arduino.cc/en/pmwiki.php?n=Reference/Bitshift
At the moment you are counting from 0 to 256, and most of your variable is in the bits 0,1,2,3 and wasted.

If you reconfigure your circuit like this, so pin 4 is at the gnd end of the R2R, and thus the LSB.
The following code should work, I have had it running here.
Also change your IDE Monitor speed to 115200.

int outputPin[] = {4, 5, 6, 7}; //pin 4 LSB

void setup()
{
  for (int i = 0; i < 4; i++) {
    pinMode(outputPin[i], OUTPUT);
  }
  Serial.begin(115200);
  Serial.println("***PortManipulation_R2R_TG_4***");
}

void loop()
{
  for ( int i = 0; i <= 15; i = i + 1)
  {
    PORTD = (i << 4);    // take the value of i and bit shift 4 places to the left to align value with pins 4 to 7
    delay(10);
    Serial.println(analogRead(A0));
  }
  for ( int i = 15; i >= 0; i = i - 1)
  {
    PORTD = (i << 4);    // take the value of i and bit shift 4 places to the left to align value with pins 4 to 7
    delay(10);
    Serial.println(analogRead(A0));
  }
}

Tom... :slight_smile:

UNO_R2R_DAC.jpg

R5 should be 2R for correct ladder termination.

DrDiettrich:
R5 should be 2R for correct ladder termination.

Good catch, 2K on bread board, excessive blood in the coffee stream.

Thanks again for the replies, and for that code. I have uploaded it into my Arduino and the results are in the image attached below. Not sure if this is completely correct but it definitely looks like a better version of the sine wave. However, the ground connection in at pin 4 still doesn't need to be in to get this result. Not sure why this is, hoping someone could explain this to me.

Hi,
Yes that does look better.
Did you check your R2R wiring to be the same as mine?
You will not get a sinewave, you will get a triangular wave as the change in value is constant.

Do you understand how the code works?

Tom... :slight_smile:

Screen Shot 2020-05-25 at 9.09.27 PM.png

DrDiettrich:
The wiring is okay as far as the staircase has no missing or non-monotonous steps. It's only resistors of too much tolerance that make an unequal step height.

In actual fact, half the steps are missing as it is supposed to have 16. And the unequal step height suggests a missing connection.

tdean_22:
Thanks again for the replies, and for that code. I have uploaded it into my Arduino and the results are in the image attached below. Not sure if this is completely correct but it definitely looks like a better version of the sine wave. However, the ground connection in at pin 4 still doesn't need to be in to get this result. Not sure why this is, hoping someone could explain this to me.

You should run it at a lower frequency or capture a shorter time interval, it is very difficult to see one entire cycle of the sine wave because it's so small.