Arduino No DAC SINEWAVE

4.1K125

Intro: Arduino No DAC SINEWAVE

/* RESISTIVE LADDER DAC OUTPUT

THIS WILL PRODUCE A SINEWAVE Connect a 1 Meg resistor to each pin. Connect all the resistors together. At the common point you will measure a SINE WAVE. With an oscilloscope. To create a usefull output a high impedance amplifier is required. */

void setup() { pinMode(2,OUTPUT); pinMode(3,OUTPUT); pinMode(4,OUTPUT); pinMode(5,OUTPUT); pinMode(6,OUTPUT); pinMode(7,OUTPUT); pinMode(8,OUTPUT); pinMode(9,OUTPUT); pinMode(10,OUTPUT); }

void loop() { // Call the Max Frequency loop 10,000 times for ( unsigned int lt=0; lt <= 10000; lt++){ // lt = loop time MaxFrequency(); } delay(200); // Max frequency loop END //Call the LOWER FREQUENCY loop 10,000 times for ( unsigned int LFlt=0; LFlt <= 10000; LFlt++){ //LFlt = Lower Frequency loop time Frequency(10); // 10 delayMicroseconds } delay(200); //Call the LOWER FREQUENCY loop 10,000 times with a longer delay between steps for ( unsigned int LFlt=0; LFlt <= 10000; LFlt++){ //LFlt = Lower Frequency loop time Frequency(100); // 100 delayMicroseconds } delay(200); // Lower Frequency END /* Uncomment this block to enable sweep // Call the Frequency function 200 times the loop time is much longer so less loops for (unsigned int St=0; St <= 200; St ++) // ST = Sweep time { for (int F=1; F <= 30; F = F + 1) // F = Delay Longer delay = lower frequency { Frequency(F); // Pass F as TIME to Frequency function } for (int F=50; F >= 1; F = F - 1){ Frequency(F); } delay(200); } // Sweep Frequency LOOP END */ // Uncoment to enable sweep }

void MaxFrequency() { for (int UP=2; UP <= 10; UP++) // Count UP pins 2 - 10 Change or add pins here { digitalWrite(UP, HIGH); } for (int DOWN=10; DOWN >= 2; DOWN--) // Count Down pins 10 - 2 Change or add pins here { digitalWrite(DOWN, LOW); } }

void Frequency(int TIME) // TIME = Delay time { for (int UP=2; UP <= 10; UP++) // Count UP pins 2 - 10 Change or add pins here { digitalWrite(UP, HIGH); delayMicroseconds(TIME); } for (int DOWN=10; DOWN >= 2; DOWN--) // Count Down pins 10 - 2 Change or add pins here { digitalWrite(DOWN, LOW); delayMicroseconds(TIME);

} delayMicroseconds(10); // Twiddle this for oscilloscope sync }

STEP 1: Read Me

Arduino-No-DAC-SINWAVE-

Output a sine wave with just resistors on your Arduino If you want a sine wave out you do not need a DAC . Just attach a resistive array to a set of pins . I used a bus terminator. Eight 1 meg ohm resistors tied together . Each pin provides additional current to the common point. By creating a loop of count up , count down nested loops you get 1-8 then 8-1 . Using an oscilloscope you will see a sine wave. This method will provide almost no current . A high impedance amplifier will be required to make the output usefull. At full speed the rise and fall time will smooth the output . As you add delay between each step you lower the frequency but see a more stepwise waveform. The included code will demonstrate full speed and then a lower speed. I have also included a swept wave that adds delay then subtracts delay. I will comment out the sweep to make testing easier. The max frequency will depend on the system clock. I know a faster method is possible but this is easy to understand.

STEP 2:

5 Comments

good afternoon. If you remember your project, tell me. I'm not going to sketch ... Could you repeat on e-mail. I am very grateful to you.

You did something different 'great'!

oops, I meant: j = int(256.0*sin(i/twoPi)) ;

Of course the code is such messed... You have made resistor-ladder DAC, but poor quality. And it doesn't look like sine wave, you could use sin() function, it takes a bit more memory, but generates sin-like numbers. And PWM with low-pass filter to take off the PWM part of the signal would generate pure sine with much less elements.

While this is an excellent example of using resistors to make a sort-of-dac, what you have here is a perfectly linear triangle wave, not a sine wave. Except you double the top and bottom points, so you get a little flat spot at the points. See here for a reference: http://www.prosoundweb.com/images/uploads/Synthesi...

With 16 resistors on the same 8 pins, arranged as an R-2R ladder, you could get LOTS more precision with the same number of pins. (Although unless u have very very precise resistors, about 6 pins would be the max). See wikipedia under "R-2R"