Re: radio frequency broadcasting

Doh, I meant to click preview. I meant 3 things I would recommend (:

"There are 3 types of people : those who can count and those who can't"

I'm not near my Arduino (acutally, it's an Olimex board with an Atmega168 in a ZIF) right now but I did try ints instead of longs and saw no performance increase. Given that millis() looks to be expensive, I will rewrite the code when I get a chance, and have it do a skad of operations and check the millis() after it and divide by the time (instead of calling millis() several times for EVERY loop). Thanks both of you for your help.

In the end I'll probably end up using a 555 timer for my 100 KHz application, since it won't actually need any configuration. But my first thought had been "I'll bet I can get an Atmega8 to serve as a cheap, precise arbitrary square wave generator."

hey it is quite the party in here!

But my first thought had been "I'll bet I can get an Atmega8 to serve as a cheap, precise arbitrary square wave generator."

Instead of direct synthesis, what about an XTAL osc, fed into a high-frequency divider controlled by the Arduino? It's basically what they would call a VFO or Variable Frequency Oscillator' in ham radio. That way the Arudino's pins could be taken up by I/o for displays and button for setting the frequency.

"I'll bet I can get an Atmega8 to serve as a cheap, precise arbitrary square wave generator."

"An extremely simple and low cost Sine/Square wave generator based on the Analog Devices AD9835 Direct Digital Synthesis (DDS) Generator chip. The frequency can be set for any frequency from 1Hz to 10MHz in 1Hz resolution steps."

All kinds of good ideas. I ran an experiment with a new test algorithm that still uses longs but doesn’t do anything inside the loop except toggle a digital output line. I saved millis, went into a million-iteration loop, and then checked millis again, and had only used 644 of them. That’s a ceiling of 1.54 MHz for square waves, well within my desired range (and good enough for AM radio!).

Here’s the code:

long millisAtStart=0;
long millisAtEnd=0;
long i=0;

#define BINARY(a,b,c,d,e,f,g,h) ((a)<<7|(b)<<6|(c)<<5|(d)<<4|(e)<<3|(f)<<2|(g)<<1|(h))

void setup()



void loop()
for(i=0; i<1000000; i++)
PORTD ^= BINARY(1,0,0,0,0,0,0,0);


Serial.print(" “);
Serial.print(” ");

By successive approximation, I found that a for loop that only toggles a port pin can do 1,552,450 square waves per second, which is 3.1236 (hmm, similar to pi) times 497 KHz, the radio frequency CosineKitty picked up this signal on. I tuned in 1550 on the dial of an AM radio and held it nearby but got nothing that changed when I reset the Atmega.

Hi Bigfun,

Good stuff ! CosineKitty’s discovery could be really useful. I wonder if it could be used to transmit data…

1,000,000 iterations / 644 milliseconds = 1,552 iterations per milliseconds.

Given that you need two iterations to make a square wave (first to set to 1, second to set back to 0, ^= is an XOR don’t forget), the frequency is 776.397kHz, isn’t it ?


Good point, so yeah, 776 kHz is the ceiling. That would explain my problem with the AM radio. Of course, it would be a square wave and not sine unless you ran it through some sort of filter.

I'll sound like a total newb, but what if you wanted to implement other frequencies? This could be useful as a quick frequency generator.

Yes, I can confirm that the rate you get is highly dependent on the kind of code you have in your loop, and that timer interrupts will cause slight distortions in your exact frequency. It is a toggle using XOR, so your square wave frequency will be half the number of iterations per second. On the other hand, because it is a square wave and not a pure sine wave, you will get harmonics at every integer multiple of the base frequency, though the energy in each higher harmonic will become weak very quickly.

If you have an oscilloscope, it is very helpful to see exactly what the square wave looks like, and visually determine the amount of time it takes for each full wave. I have a very old (vintage 1974) scope that still works great for this kind of thing.

One thing you can do is to make sure you put a long wire attached to the output pin you are toggling, as an antenna… with mine hooked up to a 8-meter long piece of wire that runs out my window, I was able to receive the signal all inside my house and around my yard outside.

I've used your work to make a Morse code beacon broadcasting at 1337 kHz. Details at

ah omg, can someone show me the partof the code that actually, sets up, and broadcasts the frequency!?!?!?!??! and would it be possible to add an audio input signal to that frequency and broadcats that too!?!:?!? come on people i need some help!


Check this out. This is probably close to what you want to do.

Pretty much the MC is used to generate an AM carrier signal but some more external components are needed to modify the carrier so it can generate sound. AM is a very simple for of transmission and can be done with a few external components.

I’m a radio operator so i <3 this kind of stuff.

I’m a radio operator so i <3 this kind of stuff.

So you also know that it is illegal to do this without a licence.

actually broadcasting in the so called american broadcast band is not actually illegal so long as you dont exceed 100mw and keep the antenna less than 10 ft. Look up Part 15 of the communications rules