Go Down

Topic: Square or Sinewave (Read 5755 times) previous topic - next topic


I just recieved my Arduino Board and I am ready to try a project. I want to create a continuous squarewave or sinewave (<= 1.2 mhz) in order to excite an RLC circuit. Is that possible? I am a beginner with this system. Can someone lead me in the right direction or tell me if there is a sample?


The square wave should be easy, but the sine wave will not be easy.
Though i don't know how easy it will be for the arduino to switch on a pin and off 1,200,000 times a second...


Sine wave, no way without external filtering circuit. However to just excite a RLC circuit a square wave should be fine. I don't know the maximum frequency the timers can be operated on but I think that it might be possible to output a 1.2Mhz digital signal. You will have to wait for one of the software gurus around here to verify if and how it would be coded.



I assume the OP meant "MHz" and not "mHz" - 1.2 mHz is easy !  ;D
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.


Guru? No.  Datasheet reader?  Yes.

Here's one way to do it.  It's a clock divider using Timer 2.  Frequency is calculated as follows:

Freq = 16000000/(2 * (OCR2A + 1))

This means that you can only get 1.3333MHz or 1.1429Mhz.  Sorry, no 1.2MHz for you!  :)  On the other hand, you can generate a nice 1.6000MHz signal really easy.

Because it uses Timer 2 in CTC Mode (Clear Timer on Compare Match Mode), it doesn't create interrupts, and you can run anything else you like at the same time.

Warning:  this is not for the feint of heart.  It's a bit technical.  Not too technical though; you just have to read a 448+ page datasheet.  Nice light reading.  Cures your insomnia.

Code: [Select]

void setup()
 // OCR2A is on "Pin 10" on the Arduino Mega
 //             "Pin 11" on the Arduino 2009 ('328p)
 // Set OCR2A as an output
 DDRB |= (1 << PB3);  // for Arduino Mega, change PB3 to PB4

 // Set Timer 2 to CTC Toggle mode (see datasheet)
 TCCR2A = (1 << COM2A0) | (1 << WGM21);

 // No prescalar
 TCCR2B = (1 << CS20);

 // divider:
 // Freq = 16MHz / (2 * (OCR2A + 1))
 // e.g. OCR2A = 4, Freq = 1.6000MHz
 //      OCR2A = 5, Freq = 1.3333MHz
 OCR2A = 5;

void loop()


(P.S. 1.2milliHertz: while(1) { digitalWrite(10, 1); delay(416666); digitalWrite(10, 0); delay(416666); } - not that great for experimenting with frequency response though.  ;) )
http://wiring.org.co/ - Wiring - Where Arduino came from. (Wiring begat Arduino)

http://loftypremises.com/ - Everything you wanted to know about me.... and less

http://roguerobotics.com/ - #1 Canadian Arduino distributor


Sep 21, 2009, 11:23 am Last Edit: Sep 21, 2009, 11:24 am by mem Reason: 1
If you can manage with an upper bound of 1MHz then you can use the FrequencyTimer2 library to simplify access to the timer.

[font=Courier New]#include <FrequencyTimer2.h>

 FrequencyTimer2::setPeriod(period); // period in microseconds
 FrequencyTimer2::enable();  // start pulsing on pin 11 (set this to output mode in setup)[/font]

You can find the library here: http://www.arduino.cc/playground/Code/FrequencyTimer2


I tried the code below and I get many errors coming from the "FrequencyTimer" library. Where do I put the downloaded library files? I changed my hardware to accept a square wave from 100 khz to 900 khz. Thanks, Curtis
// SqrWave
#include <FrequencyTimer2.h>

void setup() {

void loop()


Oct 07, 2009, 10:19 am Last Edit: Oct 07, 2009, 10:19 am by mem Reason: 1
If you are using arduino 0017 you can follow the information on the Arduino reference page:  

To install, unzip the library to a sub-directory of the libraries sub-directory of your Arduino sketchbook directory (shown in the Arduino preferences dialog). If this is the first library you've installed, you'll need to create the libraries directory. After unzipping the library, (re-)launch the Arduino environment; you should see the library in the Import Library menu.

If you are still getting errors after copying the library into the correct place, please say what Arduino version you are running, your operating system and post the first few lines of the error message.


Thanks for the reply. Here are the errors:
This is one of the errors, they are all the same from different files in FrequencyTimer2:
C:\Documents and Settingings\Intern10\Desktop\Arduino-0017\Hardware\Libraries\FrequencyTimer2\FrequencyTimer2.cpp: In 'Static Member Function Void FrequencyTimer2::SetOnOverFlow(Void(*)())':

There was also the same type of error in that file and the other 'TIMSK' was not declared in this scope.

I am using Windows XP and Arduino 0017.


If you are using a standard Arduino bard then check to make sure you have the latest version, you should see this code at the beginning of the FrequencyTimer2.cpp file:
Code: [Select]
#if defined(__AVR_ATmega328P__)
// A mega328 is mostly like an mega168, in terms of peripherals
#define __AVR_ATmega168__

Download the latest version if yours does not have that code at the beginning.

If you are running the latest code, can you say what board are you using?


just found this here
even though it belongs into the hardware-section it matches the topic very well.



Thank you Wayoda,
I already ordered it today. I cannot get the FrequencyTime2 library to work. This product is just what I want.
Thanks everyone and I will let you know if it works.


That board wayoda found looks good, interested to hear how you get on with it.

I am also interested in understanding why you had trouble with FrequencTimer2.
Did you check it see if the code you are running is the same as I posted above?

Also, what kind of arduino board are you using  (does it have a 168 or 328 chip?)


I tried all the code in the post and solved some errors but the library still doesn't work. I keep trying. My board is a 2009 on with the 328 chip.


You didn't put it in the right place (I think)

You put it in the /hardware/libraries folder yeah?

That was right for previous versions of the IDE, for 0017 read mem's post above.

It should go in the "My Documents" area, under /Arduino/libraries

Go Up