8-bit R-2R DAC need help.

So I have this school project to create a DAC converter, and we will have to make it write out a sine-wave on a oscilloscope. I decided to make a R-2R converter, but for some reason it is not working. I have tried to connect it to a oscilloscope to get out a graph, but it is just noise. Can anyone please help me and tell what i am doing wrong? I am using 10k and 20k resistors. The red wire is ground and white is output.

Can you not use the PWM ( analogWrite ) ?
please show your code, in code tags [ the left top icon that looks like </> ]

As Johnny says, you can't make a true sine wave. I assumed you really meant a modified sine wave. What did it say in your assignment?

Is there any specific frequency specified in your assignment? You can smooth the modified sine wave better with lower frequency, and LC or RC circuits.

I am unsure as to how you think that circuit would make a sine wave in the first place?

Are we missing something?

Some example DACs

I mean the first hit on google gives this:

A PDF lecture on making DACs!

And was not too hard to find this...

Sine Waves generating uisng OPAMPS.

Johnny010:
I am unsure as to how you think that circuit would make a sine wave in the first place?

Are we missing something?

The circuit alone won't produce any sine-wave, but you be able to convert digital signal to analog. And with some code you will have a sine-wave.

Yeah, a modified wave is fine. The only requirement to frequency is that you should be able to change between two frequencies with a button, and change between to wave-form with another button.

I could not find the code tags. So ill post it without. It is a simple code, it should output triangles, but the result is just some random noise. Btw I am new to electronics, so all the documents on online does not tell me anything. I honestly dont understand anything of them.

void setup() {
// put your setup code here, to run once:
for (int i = 38; i < 46; i++) {
pinMode(i, OUTPUT);
}

}

void loop() {
// put your main code here, to run repeatedly:
for (int a = 0; a < 256; a++) {
PORTD = a;
delayMicroseconds(20);
}
for (int b = 255; b >= 0; b--) {
PORTD = b;
delayMicroseconds(20);
}

}

Can you not use the PWM ( analogWrite ) ?

Oh, you are doing this with an MCU?

So take a digital value and make a sine like output?
Assuming frequency of the output is proportional to the digital value?
What are the ranges?

You were close.

You need to use the sin().

Read up about your maths...sine/cosine/tangent.

What board are you running that on?
This implies a Mega:
for (int i = 38; i < 46; i++) {
pinMode(i, OUTPUT);
}
Yet D38 to D46 is not PORTD on a Mega.

Try PORTC and 30-37, PC0 = 37, PC7 = 30.

Sine curves

So a sin equation such as:

analogWrite((128*sin("some maths here to get the correct frequency"*x)+128))
x++;

Will give a sine curve from 0 to 256 centred about 128 on the y axis.

128*sin((1/10)x)+128

Pop that in the calculator below and play with the value before the x to see the effect on it and the frequency it will produce.
The first 128 sets the "amplitude". The second 128 is an offset of 128 in the y axis (so it goes 0-256).
The sin(x) gives the actual oscillation...where any function playing on x will change the frequency.

CrossRoads:
What board are you running that on?
This implies a Mega:
for (int i = 38; i < 46; i++) {
pinMode(i, OUTPUT);
}
Yet D38 to D46 is not PORTD on a Mega.
https://www.arduino.cc/en/uploads/Main/arduino-mega2560_R3-sch.pdf

Try PORTC and 30-37, PC0 = 37, PC7 = 30.

It is on mega 2560. On this site, it said the the PORTD is from 38-46.

And guys I appreciate the tips, but i dont even that the R2R ladder works.

Can someone please explain how it works, the hole process?

The R2R ladder works just fine. Look it p on Wikipedia.

The code tags are in the menu with </> or use [code ] code here [/code ] (without the spaces after code).

Indeed use one port to do it like you try. But set the right pins. Maybe try a slower frequency first.

Also, to make a sine, just make a lookup table :wink: Don't try to use sin(), it's slow. And yeay, a full 8 bit lookup table takes 256bytes but you have plenty, just make it a const PROGMEM.

"On this site, it said the the PORTD is from 38-46."
What site is that?

CrossRoads:
"On this site, it said the the PORTD is from 38-46."
What site is that?

septillion:
Also, to make a sine, just make a lookup table :wink: Don't try to use sin(), it's slow. And yeay, a full 8 bit lookup table takes 256bytes but you have plenty, just make it a const PROGMEM.

I can not see how this holds true?

8 bit possible inputs.

256 possible values of x.

You'd need a LUT per value of x as y will be different for each iteration(x).