Frequency Sine wave

I'm new to Arduino and have a small problem with a project of mine.
I'm trying to make a sine wave in Arduino which I have to read into MATLAB (that last part works fine). I searched in other topics for a solution of my specific problem, but could not find it.

The problem is that I make a sine wave according to the general formula (2pif*t). The parameter t is calculated with the millis()/1000.00 function. The frequency f is a parameter I can set for myself. The thing is, the frequency has to be very low to get any values (visible in the serial monitor). I know it has something to do with aliasing, but I'm not sure in what way and how I can calculate the maximum frequency.


float ttime;          
float f=1;
float sinserial;

void setup() {
// put your setup code here, to run once:
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
// calculate the time and the sinewave
ttime = millis()/1000;
sinserial = sin(2*PI*f*ttime);
 
// print the results to the serial monitor:
Serial.print("time = ");
Serial.print(ttime);
Serial.print("\t output = ");
Serial.println(sinserial);

// wait 2 milliseconds before the next loop
delay(2);
}

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.. :slight_smile:

It would be quite difficult to calculate the maximum frequency. Easier to find out by trial and error. If you increase the serial speed, you will be able to get to a higher frequency, e.g. 115200.

Please read the forum guidelines before you break any more rules.

I looked at a "read me" topic but I didn't see this guideline. I'm sorry

To adjust your sinewave for a low frequency, you could use something like that:

#define sinsize (256)
#define Period  (2000) // Frequency  = 1/(Period * sinsize)

uint8_t sinus[sinsize];

void setup() {

  Serial.begin(250000);
  for (int i = 0; i < sinsize; i++)
  {
    sinus[i] = 100 * sinf(i * 2 * PI / sinsize) + 100;

  }

}


void loop() {
  uint32_t Oldmicros;
  static uint8_t i;
  if ((micros() - Oldmicros) > Period) {
    Serial.println(sinus[i]);
    if (++i == sinsize) i = 0;
    Oldmicros = micros();
  }
}

You can see this sinewave in: Menu > Tool > Serial Trace

By replacing the milis() with micros() you should be able to get higher frequencies right

sinserial = amplitude * sin(2 * PI * frequency * micros() * 0.000001);

Thanks for your help, but I still don't get it. I think I'm also confused with sampling rate, baud rate, frequencies etc. Can anyone help me with that?

I also changed the f which you can see in the attached figure. I don't understand why the changes occurs.

sineWave.png

ttime = millis()/1000;

Since millis() and 1000 are both integers this division will result in an integer number of seconds, even though you then store that integer in a float. If you use a float constant (1000.0) then the result will be a floating point number with fractions of a second.