I needed a low-frequency sine-wave generator, and after 5 days of focussed effort and research, finally have exactly what I need. But the process brought up some mind-bogglinq questions, the answers to which could possibly save me from 5 days of struggling in the future.
QUESTION 1:
The Arduino project at:
states that using the Arduino UNO "It outputs four waveshapes: sine, triangle, pulse, and saw, each waveshape ranges in frequency from 1Hz-50kHz."
Yet the project at:
states that even using the Arduino DUO for it's much greater speed over the UNO, "maximum frequency for the signal with this sketch is around 170 Hz". Why the HUGE discrepency in how fast a board is needed, and how high a sine frequency can be produced?
QUESTION 2:
I have only the Arduino UNO board, on which my scope shows the analog output pins as pulsing rectangular waves at 400 pulses a second. Can this be made faster? The UNO project above shows 8 analog outputs linked together with resistors to get the 50,000 cps sine wave. But even 8 * 400 is only 3,200. How is it possible to get a 50,000 cps sine wave out of even 3,200 square pulses? (I must admit I didn't finish building this project, bumping into too many questions and inconsistancies along the way.)
(The 8-pin chip shown is an LM386 Low Voltage Audio Power Amplifier, for a strong ouput signal.)
QUESTION 3:
The sketch for the uno prject above did not reveal even one "pinMode()" or "analogWrite()". So I was unable to figure out how the eight output pins were being communicated with by the sketch. Where can I fnd "lessons" that teach such "advanced" Arduino programming techniques? I was at a total loss, when trying to understand this sketch.
As I said at the top, after 5 days, I finally came up with the answer to my project needs. I'm designing a brainwave monitor, and needed a sine-wave generator to test the input circuitry. Thus, I only needed to produce sine waves in the range of 0.5 to 40 cps.
I modified the little sketch at:
https://www.sparkfun.com/tutorials/329
to produce the sketch shown here, which does the job:
float in, out;
const int LED = 11;
unsigned long Mils = millis();
byte multiplier = 1;
void setup()
{
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
}
void loop()
{
if (digitalRead(2) == LOW)
multiplier = 1;
else if (digitalRead(3) == LOW)
multiplier = 10;
else if (digitalRead(4) == LOW)
multiplier = 20;
else if (digitalRead(5) == LOW)
multiplier = 40;
for (in = 0; in < 6.283; in += (multiplier * 0.006283))
{
while ((millis() - Mils) < 1) {};
Mils += 1;
out = sin(in) * 127.5 + 127.5; // 127.5
analogWrite(LED,out);
}
}
In order to remove the 400 cps square pulses that produce the sine-wave, I used the low-pass filter circuit and calculator shown below, and located at:
http://www.wa4dsy.net/robot/active-filter-calc
I entered 50 in both the resistor-value and cut-off frequency boxes and clicked "COMPUTE". It then showed me I needed .1 and a .05 as the matching capacitor values. This circuit on the Arduino UNO then worked perfectly for my purposes.
I use a 10K pot to control the amplitude of the sine wave. (The end terminals connecting to the Arduino output pin, and to ground. The center tap connected to the input of the curcuit shown above.)
"Multiplier" (in the sketch) is the number of cycles of the sine wave produced per second. You can set these to be whatever frequencies you want the input pins to select, adding more pins (and thus frequencies) if you choose. Then, when running it, moving a grounded jumper cable to any of these Arduino inputs will produce the selected frequency output.