Go Down

Topic: Turn your Arduino into an AM radio transmitter! (Read 5161 times) previous topic - next topic

Nick Gammon

Jan 23, 2012, 11:14 pm Last Edit: Jan 24, 2012, 06:14 am by Nick Gammon Reason: 1
This simple project shows how you can make your Arduino into an AM-band transmitter.

Wiring: Plug a wire into pin D9.



Sketch:

Code: [Select]
const byte ANTENNA = 9;

void setup()
 {
 // set up Timer 1
 TCCR1A = _BV (COM1A0);  // toggle OC1A on Compare Match
 TCCR1B = _BV(WGM12) | _BV(CS10);   // CTC, no prescaler
 OCR1A =  9;       // compare A register value to 10 (zero relative)
 }  // end of setup

void loop()
 {
 pinMode (ANTENNA, OUTPUT);
 delay (500);
 pinMode (ANTENNA, INPUT);
 delay (300);
 }  // end of loop


Theory: The 16 MHz clock is divided by 10 (that is, 1.6 MHz) and that is used to toggle pin 9 at that rate, giving a frequency of 800 KHz, since one toggle turns the output on, and second toggle turns it off.

If you hold the Arduino near an AM radio tuned to around 800 KHz you should hear a hissing toggling on and off (like Morse code) as the carrier is turned on and off for 0.5 / 0.3 second intervals.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

robtillaart

FUN!
what stopped you from implementing a real morse lib into the code ? ;)
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Nick Gammon

Trying to keep it simple. :)

But I'm sure adding Morse into it would be pretty trivial.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Techone

Nice work.

I am going to try it. Just one wire... SIMPLE hook-up... and load the code... you are done...

Thank Nick Gammon

retrolefty

Well the next logical step would be to add in the tone library and allow a audio frequency tone output pin to modulate the timer output pin 'AM carrier frequency', then you would be talking true Amplitude Modulation, AM. However with everything being square wave outputs, don't now how easy it would be to combine these two signal outputs into a single AM output. Needs a little cleaver passive filtering and combining function circuit.

Lefty

3i3i


neoredstone

#6
Apr 09, 2015, 07:32 am Last Edit: Apr 09, 2015, 07:34 am by neoredstone
The OCR1A variable is related to the frequency.
The OCR1A variable is one less than the actual divisor.
OCR1A - Frequency
15 - 500 khz
14 - ~530 khz
13 - ~570 khz
12 - ~610 khz
11 - ~670 khz
10 - ~730 khz
9 - 800 khz
8 - ~890 khz
7 - 1000 khz
6 - ~1140 khz
5 - ~1330 khz
4 - 1600 khz
3 - 2000 khz
2 - ~2670 khz
1 - 4000 khz
0 - 8000 khz *would not recommend this setting

The formula is (16÷(OCR1A+1)÷2)×1000 = frequency in khz

neoredstone

#7
Apr 09, 2015, 07:41 am Last Edit: Apr 09, 2015, 07:49 am by neoredstone
The way to add tone to this, is by changing the delay between the two states, to get a 500 or 1000 hz (I'm not to sure) tone, you simply put in
Code: [Select]
pinMode (ANTENNA, OUTPUT);
 delay (1);
 pinMode (ANTENNA, INPUT);
 delay (1);

And you can adjust the delay to meet your specifications.

headpigeons

This is really handy. I added some basic tone abilities and encoded twinkle twinkle.



Code: [Select]

const byte ANTENNA = 9;

unsigned long A = calcDelayTimeForFreq(440);
unsigned long B = calcDelayTimeForFreq(494);
unsigned long C = calcDelayTimeForFreq(523);
unsigned long D = calcDelayTimeForFreq(587);
unsigned long E = calcDelayTimeForFreq(659);
unsigned long F = calcDelayTimeForFreq(699);
unsigned long G = calcDelayTimeForFreq(784);
unsigned long _A2 = calcDelayTimeForFreq(880);
unsigned long rest = 1;

unsigned long q = 652;
unsigned long e = q/2;
unsigned long s = e/2;
unsigned long t = s/2;
unsigned long h = q*2;
unsigned long f = q*4;

void setup() {
  // set up Timer 1
  TCCR1A = _BV (COM1A0);  // toggle OC1A on Compare Match
  TCCR1B = _BV(WGM12) | _BV(CS10);   // CTC, no prescaler
  OCR1A =  9;       // compare A register value to 10 (zero relative)
}  // end of setup

void tone(unsigned long dTime, unsigned long playTime) {
  unsigned long end = millis() + playTime;
 
  while (end > millis()) {
    pinMode (ANTENNA, OUTPUT);
    delayMicroseconds(dTime);
    pinMode (ANTENNA, INPUT);
    delayMicroseconds(dTime);   
  }
}

void note(unsigned long dTime, unsigned long playTime) {
  tone(dTime,playTime-50);
  tone(rest,50);
}

unsigned long calcDelayTimeForFreq(unsigned long freq) {
  return 500000/freq;
}


void twinkleTwinkle() {
  note(C,q);
  note(C,q);
  note(G,q);
  note(G,q);
  note(_A2,q);
  note(_A2,q);
  note(G,h);
 
  note(F,q);
  note(F,q);
  note(E,q);
  note(E,q);
  note(D,q);
  note(D,q);
  note(C,h);
 
  note(G,q);
  note(G,q);
  note(F,q);
  note(F,q);
  note(E,q);
  note(E,q);
  note(D,h);
 
  note(G,q);
  note(G,q);
  note(F,q);
  note(F,q);
  note(E,q);
  note(E,q);
  note(D,h);
 
  note(C,q);
  note(C,q);
  note(G,q);
  note(G,q);
  note(_A2,q);
  note(_A2,q);
  note(G,h);

  note(F,q);
  note(F,q);
  note(E,q);
  note(E,q);
  note(D,q);
  note(D,q);
  note(C,h);
}

void locateTone() {
  note(A,t);
  note(B,t);
  note(C,t);
  note(D,t);
  note(E,f);
  note(D,t);
  note(C,t);
  note(B,t);
}

void loop() {
  twinkleTwinkle();
}


I actually attached this to a disconnected sprinkler valve control wire and followed it around my yard with an am radio to find where the old valves were located. Pretty handy.

janvier123

We are looking for Arduino Projects. Register today and post your project.

Go Up