HELP - Arduino Uno generate Sinusoidal pwm

Hi all,
new to this forum. i am trying to produce sinusoidal wave of 40Hz with pwm of 5kHz using only one 8-bits timer. i read the datasheet but doesnt really understand how to deal with this thing.

my approach:(Modified 04/09/12)

void setup(){
  pinMode(5, OUTPUT);
  
  TCCR0A = 0;
  TCCR0B = 0;
  // use fast pwm (mode7) and prescaler of 8. TOP = OCR0A
  // non-inverting for OC0B
  // TCCR0A = 0b01100011; TCCR0B = 0b00001011
  TCCR0A |= _BV(COM0A0) | _BV(COM0B1) | _BV(WGM01) | _BV(WGM00);
  TCCR0B |= _BV(WGM02); 

  TIMSK0 |= _BV(OCIE0B);

  OCR0A = 164;
  OCR0B = 0;  
  
  TCCR0B |=  _BV(CS01);
  
}

ISR(TIMER0_COMPB_vect){
  
  //i have created a look up table in sinetable[125] //
  if ( count < 125) {
    OCR0B = sinetable[count];
  }
  else 
  {
    count = -1;
  }
  ++count;
}

Please help me to correct the code. Or is there any better way to create the wave.
and another question, what i need to do to "Serial.print" in the "ISR". i tried and keep getting stuck at only first few characters or some weird characters.

Thank you.

Well in my opinion trying to create a sine wave output from a pwm analogWrite() command is a pretty difficult thing to do, as you have two variables that have to be manipulated, duty cycle for the amplitude information and update time for the frequency information, followed by the required external low-pass filtering required.

I would think a easier solution would be to run the tone library code, set it to output a continuous 40Hz square wave output signal, then have pass it through an external low-pass (or narrow bandpass) filter to extract the sine wave required.

Lefty

I'm gonna need to agree with retro lefty in his post. I'm in the radio buss and I've used the arduino many times to generate transmitter keying tones and it's way way easier to use the tone library and then filter it. I've had very good luck this way.
I'm no expert at this stuff, but it's simple.

thanks for the replies but i need to use only the microcontroller to generate the sinusoidal wave, because later i will need to program it to the lilypad. therefore i need to figure out how to do this by using the fast pwm mode. besides, in future i may need to make a remote control device to control the amplitude and make change of some of the variable in the pwm. so what i am thinking is to use only one timer to do it.

ok i figured it out the problem is the "|=" sign
i will test the wave and will post again if any problem

thanks.

weesiang_loke:
thanks for the replies but i need to use only the microcontroller to generate the sinusoidal wave, because later i will need to program it to the lilypad. therefore i need to figure out how to do this by using the fast pwm mode. besides, in future i may need to make a remote control device to control the amplitude and make change of some of the variable in the pwm. so what i am thinking is to use only one timer to do it.

Then it will not be possible for you to create a sine wave as none of the arduino compatible AVR chips contain a true D/A output, you will always have to have external circuitry (low-pass filtering and amplification or external D/A device) to create sine waves. A PWM output is fundamentally a digital output signal, the voltage is always either 0 or +5vdc and any specific point of time, where a sine wave is a smoothly changing analog voltage.

Lefty

Is this a duplicate post ? I am pretty sure that AWOL and I replied to the same question yesterday and the replies are not here ?

anyway here is the link AWOL posted - http://interface.khm.de/index.php/lab/experiments/arduino-dds-sinewave-generator

and here is an arduino in action generating lots of waveforms.

[url=RCArduino: The Must Build Arduino Project - The Illutron B

A low pass filter helps, in the case of the synth the filter is simply a serier diode and parallel capacitor.

Duane B
rcarduino.blogspot.com]RCArduino: The Must Build Arduino Project - The Illutron B

A low pass filter helps, in the case of the synth the filter is simply a serier diode and parallel capacitor.

Duane B

alright my bad. actually i am going to pass the wave into a dc motor using a H-bridge. so i think i only need to create something like a sine wave but in the pwm form. from what i heard that will trick the motor that actually a sine wave is created. but anyway thanks for the replies and sorry for the confusion i have made.

regards,

weesiang_loke:
alright my bad. actually i am going to pass the wave into a dc motor using a H-bridge. so i think i only need to create something like a sine wave but in the pwm form. from what i heard that will trick the motor that actually a sine wave is created. but anyway thanks for the replies and sorry for the confusion i have made.

regards,

Yes, your requirements are still a little fuzzy. What is your bottom line objective, be be able to control the speed and direction of rotation for a simple DC motor? Is so you can forget the whole sine-wave thingee, just drive the H-drive controller with a variable duty cycle PWM signal, thousands of us have done just that using standard Arduino analogWrite() commands. If your objective is something else just state it without your guess on how it should be performed.
Lefty

ok... so what i am doing is to use arduino to generate the fixed frequency of pwm with different pulse width to generate the sine wave. Then channel them to drive a dc motor (custom made motor) which will 'oscillate' about 40 times in a second. I am not sure if any other frequency and pwm will work on the motor. So i have to stick to 40 Hz and 5kHz. Later i may want to add a remote control device to change the amplitude and frequency of the sine wave. so i think the analogWrite() is not so useful and timerOne only for timer1. So i guess my best option is to use fast pwm mode.

weesiang_loke:
ok... so what i am doing is to use arduino to generate the fixed frequency of pwm with different pulse width to generate the sine wave. Then channel them to drive a dc motor (custom made motor) which will 'oscillate' about 40 times in a second. I am not sure if any other frequency and pwm will work on the motor. So i have to stick to 40 Hz and 5kHz. Later i may want to add a remote control device to change the amplitude and frequency of the sine wave. so i think the analogWrite() is not so useful and timerOne only for timer1. So i guess my best option is to use fast pwm mode.

What does an 'oscillating DC motor' do in operation? I haven't ever heard of that mode of operation.

Lefty