Square or Sinewave

Hello,
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?
Thanks,
Curtis

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.

Lefty

I assume the OP meant "MHz" and not "mHz" - 1.2 mHz is easy ! ;D

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! :slight_smile: 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.

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()
{
}

b

(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. :wink: )

If you can manage with an upper bound of 1MHz then you can use the FrequencyTimer2 library to simplify access to the timer.

Usage:
#include <FrequencyTimer2.h>

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

You can find the library here: Arduino Playground - FrequencyTimer2

Hello,
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() {
pinMode(11,OUTPUT);

FrequencyTimer2::setPeriod(10000);
FrequencyTimer2::enable();
}

void loop()
{
}

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.

Hello,
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.
Thanks,
Curtis

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:

#if defined(__AVR_ATmega328P__)
// A mega328 is mostly like an mega168, in terms of peripherals
#define __AVR_ATmega168__
#endif

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?

Hi,
just found this here

even though it belongs into the hardware-section it matches the topic very well.

Eberhard

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.
Curtis

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?)

Hello,
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.
Thanks,
Curtis

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

Hello Trialex,
Your right, it was in the wrong place so I created a directory in 'My Documents' named Arduino and another directory inside that called Libraries. I downloaded a new zip of FrequencyTimer2 there and extracted it. Still the same compiler errors.
Thanks for your suggestions,
Curtis

I created a directory in 'My Documents' named Arduino?

Hmm? the Arduino directory should already be there, that is where all the sketches you have created should be stored. If you needed to create the arduino directory then something is in the wrong place.

Have a look at the preferences tab (from the file menu in the IDE) and see where the IDE thinks your sketch files should be stored. This directory should already have existed. As mentioned in an earlier post, you should create the library directory (if it did not already exist) under the Arduino subdirectory indicated in the IDE.

Once you get the directories sorted you should delete any old copies of FrequencyTimer2 that may be in other directories so there is one copy.
For example of your arduino sketchbook location is:
C:\Documents and Settings\YourNameHere\My Documents\Arduino

Then there should be a subdirectory under this called Libraries :
?\My Documents\Arduino\Libraries

And a subdirectory under this called FrequencyTimer2 that has the .cpp and .h files
?\My Documents\Arduino\Libraries\FrequencyTimer2\ FrequencyTimer2.cpp
?\My Documents\Arduino\Libraries\FrequencyTimer2\ FrequencyTimer2.h

You should delete any other copies of FrequencyTimer2 that may be in the directory where you installed your arduino runtime code.

Hello,
I deleted all references to arduino. I then downloaded the latest arduino from the site. I created a directory named Arduino in My Documents and extracted the files there. There was no subdirectory named Libraries in there. There was one in the subdirectory named Hardware. I created a subdirectory in ..My Documents\Arduino\ called libraries. I then extracted the FrequencyTimer2 files into this place. ..My Documents\Arduino\Libraries\FrequencyTimer2\

Still the same. I googled this library and saw that many people were having the same problems. Same errors with no solution.
Thanks,
Curtis

Have you checked that the copy of FrequencyTimer2 is the correct version (see my earlier post for what to look for)

Hello,
I want to thank everyone for their help thus far. I ordered a breakout board that was suggested: http://www.sparkfun.com/commerce/product_info.php?products_id=9116

It came in and it looks promising. From reading the datasheet, I think it uses I2C. I believe the wire library will work. I have a Duemilanove. The pins are SDA=Pin4, SCL=Pin 5 for that board? Any help would be greatly appreciated since I am still learning this great product (Arduino).
Thanks,
Curtis