I am fairly new to the Arduino ecosystem and have started working with an MKRZero DAC to generate a sine wave from 0 to 3.3V. The code I am using does not have any delay in the 'loop' section of the code to control the frequency, and hence the default frequency of the sine wave obtained is 4 Hz. I want to increases this frequency to greater than 2000 Hz. How do I achieve the same?
Following is the Arduino code that I am using:
float x = 270; // Current degrees for sine wave (initially 270 so that
// the sine wave is initially zero).
//Setup Function
void setup() {
//Setup DAC/Analog Output
//pinMode(A0, OUTPUT); //Set A0 as a DAC
analogWriteResolution(10); //Change the DAC resolution to 10-bits
analogWrite(A0, 0); // Initialize Dac to Zero
}
//Loop Function
void loop() {
//Local Variables
int output; // Value to output to the DAC
/*
- Calculate Sine Value
- The sine wave should be a value between 0 and 1023 (i.e. a 10 bit
- value). By scaling the output by 512 and offsetting by 512, the value
- should be between 0 and 1024. Casting to an int appears to limit the
- value to 1023. To convert degrees to radians, x is multiplied by
- pi/180 = 0.017453.
*/
output = (int) (512.0 * sin(0.017453 * x) + 512); // Sine Wave
//Output Analog Value
analogWrite(A0, output); // Write the analog output to A0
x += 0.35;
if(x>=360.0) x -= 360.0;
}