# How to generate the sinewave using arduino uno with DAC0800 kindly help us

Im beginner of the Arduino. i need to generate the 100hz sinewave using Arduino uno with DAC0800 or MCP4921 DAC please share the sample program and guideline to me Thankyou!...

determine how fast you write values to the DAC

create a table with the values to write (pre-calculate), one complete sinewave at the desired amplitude and with the appropriate offset for zero, and at the appropriate sample rate.

repeatedly write those samples to the dac at the sample rate they are calculated for

give it a shot

1 Like

here's some code for generating a table

``````#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int
genSampTbl (
float   freq,
int     fSamp,
int     amp,
int     offset,
int    **waveform )
{
int     nSamp = fSamp / freq;
*waveform     = (int *) malloc (nSamp * sizeof(int));

for (int n = 0; n < nSamp; n++)
(*waveform) [n] = offset + (int) (amp * sin (M_TWOPI * n / nSamp));

return nSamp;
}

// -----------------------------------------------------------------------------
void
dispTable (
int     *waveform,
int      nSamp,
char    *text )
{
printf ("%s: %s\n", __func__, text);
for (int n = 0; n < nSamp; n++)
printf (" %s: %3d %6d\n", __func__, n, waveform [n]);
}

// -----------------------------------------------------------------------------
void
genDispTable (
float   freq,
int     fSamp,
int     amp,
int     offset )
{
int *waveform;
int  nSamp = genSampTbl (freq, fSamp, amp, offset, &waveform);

char s [40];
sprintf (s, "%.0f Hz", freq);
dispTable (waveform, nSamp, s);
}

// -----------------------------------------------------------------------------
int
main ()
{
genDispTable (100, 1000, 500, 512);

genDispTable (120, 1000, 500, 0);
genDispTable ( 80, 1000, 500, 0);

return 0;
}
``````
1 Like

@gcjr nice. I had to put π in manually, gdb online didn't have the constant.

And I'm way lazy, so here's an alternate dispTable:

``````// -----------------------------------------------------------------------------
void
dispTable (
int     *waveform,
int      nSamp,
char    *text )
{
printf ("//%s: %s\n", __func__, text);

printf("int myTable[%d] = {\n", nSamp);
for (int n = 0; n < nSamp; n++)
printf ("%6d,\n",waveform [n]);

printf("};\n");
}
``````

exercise for the reader, print 8 values per line...

which BTW shows why that last comma is valid syntax in initializers.

a7

1 Like

there's nothing wrong with writing a program that writes code, but i was was think of just pre-calculating the table in the arduino setup().

Yes.

When I do something like the table maker code, I prefer to do it in a more, um, comfortable programming environment.

Sometimes being able to leave the generating code off the target is significant in terms of time or code space.

Just all trades off and deciding where to have what kind of fun.

a7

of course. i typically use AWK to generate tables (statically initialized arrays of C struct), often from a description written in a more human editable form

in this case, you'd need to run your script to generate the table and then rebuild the arduino code. by creating the table in the arduino code, you change the constants and just rebuild the arduino code.

another approach i learned from Hal Chamberlin (Byte Magazine) is to create a more complete sine table and use a fraction index to to skip thru the table for different frequencies

That isn't how this forum works. This is not a shop where you just turn up, ask for a solution, and walk away with a ready-to-run package that you cut-and-paste into your IDE. That's what Google is for (if you are lucky).

In these forums you are expected to do the work, and the people here will help you overcome the difficulties you encounter. That's because humans have limited spare time, unlike Google's search engine.

You've already been given a big helping hand by @gcjr and @alto777. They have shown you how to generate a table with a series of values representing a sine wave. All you have to do is read those samples at the relevant rate and output them to your DAC.

Have a go! Cut some code, try it out, and if it doesn't work, post it here. People will be only too pleased to help you sort it out.

I see your Hal Chamberlin and raise you a Don Lancaster.

just now poking around I inadvertently accumulated ← see what I did there? 100 megabytes of old page image PDFs from back in the day, an expression I avoid, but hey.

a7

Thank you!... i try to generate the table and again back to you Thanks again ....

Thank U....

Hi.... this code all ready used for two pins to generate the positive cycle and negative cycle PWM pulse but i need to generate the full sine wave signal from arduino with help of DAC.

Thanks for your valuable replay ......

Hi i have used below circuit ... to generate the sine wave signal

If you use an 8bit DAC to (indirectly) achieve a -12v to +12v amplitude sine wave, the steps will be about 0.1 volts. If you don't need that full swing, the IC itself can handle -10 volts through to +18v. You can make a sine table smaller, if necessary, by storing only 0 to 90 degrees. The rest by simple mirroring and flipping , taking advantage of its symmetry.