Pages: [1]   Go Down
Author Topic: setup PWM for output ultrasound wave  (Read 885 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 3
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi guys,

Please can someone help me to setup one of the pwm output to produce a ultrasound ( form 20khz to 60khz ) wave ?


Thanks you a lot.


Ps i'm sorry if someone think that this question it's already answered in some tutorial but i doni find any easy for me.i thinks i need to use timer programming but i dont know how to do...
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 602
Posts: 33375
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Ty this code, it produces it's signal on pin 3:-
Code:
/* Code to pulse pin 3 with a modulated signal
* Can be used to drive an IR LED to keep a TSOP IR receiver happy
* This allows you to use a modulated receiver and a continuous beam detector
* By Mike Cook Nov 2011 - Released under the Open Source license
*/
 volatile byte pulse = 0;

ISR(TIMER2_COMPB_vect){  // Interrupt service routine to pulse the modulated pin 3
    pulse++;
  if(pulse >= 8) { // change number for number of modulation cycles in a pulse
    pulse =0;
    TCCR2A ^= _BV(COM2B1); // toggle pin 3 enable, turning the pin on and off
  }
}

void setIrModOutput(){  // sets pin 3 going at the IR modulation rate
  pinMode(3, OUTPUT);
  TCCR2A = _BV(COM2B1) | _BV(WGM21) | _BV(WGM20); // Just enable output on Pin 3 and disable it on Pin 11
  TCCR2B = _BV(WGM22) | _BV(CS22);
  OCR2A = 51; // defines the frequency 51 = 38.4 KHz, 54 = 36.2 KHz, 58 = 34 KHz, 62 = 32 KHz
  OCR2B = 26;  // deines the duty cycle - Half the OCR2A value for 50%
  TCCR2B = TCCR2B & 0b00111000 | 0x2; // select a prescale value of 8:1 of the system clock
}

void setup(){
  setIrModOutput();
  TIMSK2 = _BV(OCIE2B); // Output Compare Match B Interrupt Enable
}

void loop(){
// do something here
}



It if for a IR modulator but will work just as well with ultrasonics.
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 108
Posts: 5144
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Please can someone help me to setup one of the pwm output to produce a ultrasound ( form 20khz to 60khz ) wave ?

What does "wave" mean in this context? You cannot put out a sinus wave using PWM. But you can put out pulses with 60kHz. So the question is: what do you need the output for? This may influence the way to produce the output signal.

Here is the playground article: http://www.arduino.cc/playground/Code/PwmFrequency
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 3
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks to all for answer.


I need a sound wave on output  ( maybe square wave ? )  to control a tweeter.


Please Mike can you also help me to modify your code for change frequency in "any" in the range specified ?


Thanks friends
Logged

0
Offline Offline
Shannon Member
****
Karma: 200
Posts: 11694
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Please can someone help me to setup one of the pwm output to produce a ultrasound ( form 20khz to 60khz ) wave ?

What does "wave" mean in this context? You cannot put out a sinus wave using PWM. But you can put out pulses with 60kHz. So the question is: what do you need the output for? This may influence the way to produce the output signal.

Here is the playground article: http://www.arduino.cc/playground/Code/PwmFrequency

Ultrasound transduceers are normally highly resonant, so square v. sine wave makes no difference, but the frequency has to match the
transducer.
Logged

[ I won't respond to messages, use the forum please ]

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 602
Posts: 33375
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
can you also help me to modify your code for change frequency in "any" in the range specified ?

Look at the code:-
Code:
OCR2A = 51; // defines the frequency 51 = 38.4 KHz, 54 = 36.2 KHz, 58 = 34 KHz, 62 = 32 KHz
  OCR2B = 26;  // defines the duty cycle - Half the OCR2A value for 50%

Just poke different numbers into OCR2A and OCR2B to change the frequency.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

According to my arithmetic:

Code:
OCR2A = 0, frequency = 2000000 Hz   <--- won't work in this case
OCR2A = 1, frequency = 1000000 Hz
OCR2A = 2, frequency = 666667 Hz
OCR2A = 3, frequency = 500000 Hz
OCR2A = 4, frequency = 400000 Hz
OCR2A = 5, frequency = 333333 Hz
OCR2A = 6, frequency = 285714 Hz
OCR2A = 7, frequency = 250000 Hz
OCR2A = 8, frequency = 222222 Hz
OCR2A = 9, frequency = 200000 Hz
OCR2A = 10, frequency = 181818 Hz
OCR2A = 11, frequency = 166667 Hz
OCR2A = 12, frequency = 153846 Hz
OCR2A = 13, frequency = 142857 Hz
OCR2A = 14, frequency = 133333 Hz
OCR2A = 15, frequency = 125000 Hz
OCR2A = 16, frequency = 117647 Hz
OCR2A = 17, frequency = 111111 Hz
OCR2A = 18, frequency = 105263 Hz
OCR2A = 19, frequency = 100000 Hz
OCR2A = 20, frequency = 95238 Hz
OCR2A = 21, frequency = 90909 Hz
OCR2A = 22, frequency = 86957 Hz
OCR2A = 23, frequency = 83333 Hz
OCR2A = 24, frequency = 80000 Hz
OCR2A = 25, frequency = 76923 Hz
OCR2A = 26, frequency = 74074 Hz
OCR2A = 27, frequency = 71429 Hz
OCR2A = 28, frequency = 68966 Hz
OCR2A = 29, frequency = 66667 Hz
OCR2A = 30, frequency = 64516 Hz
OCR2A = 31, frequency = 62500 Hz
OCR2A = 32, frequency = 60606 Hz
OCR2A = 33, frequency = 58824 Hz
OCR2A = 34, frequency = 57143 Hz
OCR2A = 35, frequency = 55556 Hz
OCR2A = 36, frequency = 54054 Hz
OCR2A = 37, frequency = 52632 Hz
OCR2A = 38, frequency = 51282 Hz
OCR2A = 39, frequency = 50000 Hz
OCR2A = 40, frequency = 48780 Hz
OCR2A = 41, frequency = 47619 Hz
OCR2A = 42, frequency = 46512 Hz
OCR2A = 43, frequency = 45455 Hz
OCR2A = 44, frequency = 44444 Hz
OCR2A = 45, frequency = 43478 Hz
OCR2A = 46, frequency = 42553 Hz
OCR2A = 47, frequency = 41667 Hz
OCR2A = 48, frequency = 40816 Hz
OCR2A = 49, frequency = 40000 Hz
OCR2A = 50, frequency = 39216 Hz
OCR2A = 51, frequency = 38462 Hz
OCR2A = 52, frequency = 37736 Hz
OCR2A = 53, frequency = 37037 Hz
OCR2A = 54, frequency = 36364 Hz
OCR2A = 55, frequency = 35714 Hz
OCR2A = 56, frequency = 35088 Hz
OCR2A = 57, frequency = 34483 Hz
OCR2A = 58, frequency = 33898 Hz
OCR2A = 59, frequency = 33333 Hz
OCR2A = 60, frequency = 32787 Hz
OCR2A = 61, frequency = 32258 Hz
OCR2A = 62, frequency = 31746 Hz
OCR2A = 63, frequency = 31250 Hz
OCR2A = 64, frequency = 30769 Hz
OCR2A = 65, frequency = 30303 Hz
OCR2A = 66, frequency = 29851 Hz
OCR2A = 67, frequency = 29412 Hz
OCR2A = 68, frequency = 28986 Hz
OCR2A = 69, frequency = 28571 Hz
OCR2A = 70, frequency = 28169 Hz
OCR2A = 71, frequency = 27778 Hz
OCR2A = 72, frequency = 27397 Hz
OCR2A = 73, frequency = 27027 Hz
OCR2A = 74, frequency = 26667 Hz
OCR2A = 75, frequency = 26316 Hz
OCR2A = 76, frequency = 25974 Hz
OCR2A = 77, frequency = 25641 Hz
OCR2A = 78, frequency = 25316 Hz
OCR2A = 79, frequency = 25000 Hz
OCR2A = 80, frequency = 24691 Hz
OCR2A = 81, frequency = 24390 Hz
OCR2A = 82, frequency = 24096 Hz
OCR2A = 83, frequency = 23810 Hz
OCR2A = 84, frequency = 23529 Hz
OCR2A = 85, frequency = 23256 Hz
OCR2A = 86, frequency = 22989 Hz
OCR2A = 87, frequency = 22727 Hz
OCR2A = 88, frequency = 22472 Hz
OCR2A = 89, frequency = 22222 Hz
OCR2A = 90, frequency = 21978 Hz
OCR2A = 91, frequency = 21739 Hz
OCR2A = 92, frequency = 21505 Hz
OCR2A = 93, frequency = 21277 Hz
OCR2A = 94, frequency = 21053 Hz
OCR2A = 95, frequency = 20833 Hz
OCR2A = 96, frequency = 20619 Hz
OCR2A = 97, frequency = 20408 Hz
OCR2A = 98, frequency = 20202 Hz
OCR2A = 99, frequency = 20000 Hz
OCR2A = 100, frequency = 19802 Hz
OCR2A = 101, frequency = 19608 Hz
OCR2A = 102, frequency = 19417 Hz
OCR2A = 103, frequency = 19231 Hz
OCR2A = 104, frequency = 19048 Hz
OCR2A = 105, frequency = 18868 Hz
OCR2A = 106, frequency = 18692 Hz
OCR2A = 107, frequency = 18519 Hz
OCR2A = 108, frequency = 18349 Hz
OCR2A = 109, frequency = 18182 Hz
OCR2A = 110, frequency = 18018 Hz
OCR2A = 111, frequency = 17857 Hz
OCR2A = 112, frequency = 17699 Hz
OCR2A = 113, frequency = 17544 Hz
OCR2A = 114, frequency = 17391 Hz
OCR2A = 115, frequency = 17241 Hz
OCR2A = 116, frequency = 17094 Hz
OCR2A = 117, frequency = 16949 Hz
OCR2A = 118, frequency = 16807 Hz
OCR2A = 119, frequency = 16667 Hz
OCR2A = 120, frequency = 16529 Hz
OCR2A = 121, frequency = 16393 Hz
OCR2A = 122, frequency = 16260 Hz
OCR2A = 123, frequency = 16129 Hz
OCR2A = 124, frequency = 16000 Hz
OCR2A = 125, frequency = 15873 Hz
OCR2A = 126, frequency = 15748 Hz
OCR2A = 127, frequency = 15625 Hz
OCR2A = 128, frequency = 15504 Hz
OCR2A = 129, frequency = 15385 Hz
OCR2A = 130, frequency = 15267 Hz
OCR2A = 131, frequency = 15152 Hz
OCR2A = 132, frequency = 15038 Hz
OCR2A = 133, frequency = 14925 Hz
OCR2A = 134, frequency = 14815 Hz
OCR2A = 135, frequency = 14706 Hz
OCR2A = 136, frequency = 14599 Hz
OCR2A = 137, frequency = 14493 Hz
OCR2A = 138, frequency = 14388 Hz
OCR2A = 139, frequency = 14286 Hz
OCR2A = 140, frequency = 14184 Hz
OCR2A = 141, frequency = 14085 Hz
OCR2A = 142, frequency = 13986 Hz
OCR2A = 143, frequency = 13889 Hz
OCR2A = 144, frequency = 13793 Hz
OCR2A = 145, frequency = 13699 Hz
OCR2A = 146, frequency = 13605 Hz
OCR2A = 147, frequency = 13514 Hz
OCR2A = 148, frequency = 13423 Hz
OCR2A = 149, frequency = 13333 Hz
OCR2A = 150, frequency = 13245 Hz
OCR2A = 151, frequency = 13158 Hz
OCR2A = 152, frequency = 13072 Hz
OCR2A = 153, frequency = 12987 Hz
OCR2A = 154, frequency = 12903 Hz
OCR2A = 155, frequency = 12821 Hz
OCR2A = 156, frequency = 12739 Hz
OCR2A = 157, frequency = 12658 Hz
OCR2A = 158, frequency = 12579 Hz
OCR2A = 159, frequency = 12500 Hz
OCR2A = 160, frequency = 12422 Hz
OCR2A = 161, frequency = 12346 Hz
OCR2A = 162, frequency = 12270 Hz
OCR2A = 163, frequency = 12195 Hz
OCR2A = 164, frequency = 12121 Hz
OCR2A = 165, frequency = 12048 Hz
OCR2A = 166, frequency = 11976 Hz
OCR2A = 167, frequency = 11905 Hz
OCR2A = 168, frequency = 11834 Hz
OCR2A = 169, frequency = 11765 Hz
OCR2A = 170, frequency = 11696 Hz
OCR2A = 171, frequency = 11628 Hz
OCR2A = 172, frequency = 11561 Hz
OCR2A = 173, frequency = 11494 Hz
OCR2A = 174, frequency = 11429 Hz
OCR2A = 175, frequency = 11364 Hz
OCR2A = 176, frequency = 11299 Hz
OCR2A = 177, frequency = 11236 Hz
OCR2A = 178, frequency = 11173 Hz
OCR2A = 179, frequency = 11111 Hz
OCR2A = 180, frequency = 11050 Hz
OCR2A = 181, frequency = 10989 Hz
OCR2A = 182, frequency = 10929 Hz
OCR2A = 183, frequency = 10870 Hz
OCR2A = 184, frequency = 10811 Hz
OCR2A = 185, frequency = 10753 Hz
OCR2A = 186, frequency = 10695 Hz
OCR2A = 187, frequency = 10638 Hz
OCR2A = 188, frequency = 10582 Hz
OCR2A = 189, frequency = 10526 Hz
OCR2A = 190, frequency = 10471 Hz
OCR2A = 191, frequency = 10417 Hz
OCR2A = 192, frequency = 10363 Hz
OCR2A = 193, frequency = 10309 Hz
OCR2A = 194, frequency = 10256 Hz
OCR2A = 195, frequency = 10204 Hz
OCR2A = 196, frequency = 10152 Hz
OCR2A = 197, frequency = 10101 Hz
OCR2A = 198, frequency = 10050 Hz
OCR2A = 199, frequency = 10000 Hz
OCR2A = 200, frequency = 9950 Hz
OCR2A = 201, frequency = 9901 Hz
OCR2A = 202, frequency = 9852 Hz
OCR2A = 203, frequency = 9804 Hz
OCR2A = 204, frequency = 9756 Hz
OCR2A = 205, frequency = 9709 Hz
OCR2A = 206, frequency = 9662 Hz
OCR2A = 207, frequency = 9615 Hz
OCR2A = 208, frequency = 9569 Hz
OCR2A = 209, frequency = 9524 Hz
OCR2A = 210, frequency = 9479 Hz
OCR2A = 211, frequency = 9434 Hz
OCR2A = 212, frequency = 9390 Hz
OCR2A = 213, frequency = 9346 Hz
OCR2A = 214, frequency = 9302 Hz
OCR2A = 215, frequency = 9259 Hz
OCR2A = 216, frequency = 9217 Hz
OCR2A = 217, frequency = 9174 Hz
OCR2A = 218, frequency = 9132 Hz
OCR2A = 219, frequency = 9091 Hz
OCR2A = 220, frequency = 9050 Hz
OCR2A = 221, frequency = 9009 Hz
OCR2A = 222, frequency = 8969 Hz
OCR2A = 223, frequency = 8929 Hz
OCR2A = 224, frequency = 8889 Hz
OCR2A = 225, frequency = 8850 Hz
OCR2A = 226, frequency = 8811 Hz
OCR2A = 227, frequency = 8772 Hz
OCR2A = 228, frequency = 8734 Hz
OCR2A = 229, frequency = 8696 Hz
OCR2A = 230, frequency = 8658 Hz
OCR2A = 231, frequency = 8621 Hz
OCR2A = 232, frequency = 8584 Hz
OCR2A = 233, frequency = 8547 Hz
OCR2A = 234, frequency = 8511 Hz
OCR2A = 235, frequency = 8475 Hz
OCR2A = 236, frequency = 8439 Hz
OCR2A = 237, frequency = 8403 Hz
OCR2A = 238, frequency = 8368 Hz
OCR2A = 239, frequency = 8333 Hz
OCR2A = 240, frequency = 8299 Hz
OCR2A = 241, frequency = 8264 Hz
OCR2A = 242, frequency = 8230 Hz
OCR2A = 243, frequency = 8197 Hz
OCR2A = 244, frequency = 8163 Hz
OCR2A = 245, frequency = 8130 Hz
OCR2A = 246, frequency = 8097 Hz
OCR2A = 247, frequency = 8065 Hz
OCR2A = 248, frequency = 8032 Hz
OCR2A = 249, frequency = 8000 Hz
OCR2A = 250, frequency = 7968 Hz
OCR2A = 251, frequency = 7937 Hz
OCR2A = 252, frequency = 7905 Hz
OCR2A = 253, frequency = 7874 Hz
OCR2A = 254, frequency = 7843 Hz
OCR2A = 255, frequency = 7813 Hz

OCR2B in each case should be (((OCR2A + 1) / 2) - 1)
« Last Edit: November 09, 2012, 04:15:42 pm by Nick Gammon » Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 602
Posts: 33375
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I hope you did it with a spread sheet.

OCR2A = 0, frequency = 2000000 Hz

Won't work but the rest looks credible.
Logged

0
Offline Offline
Shannon Member
****
Karma: 200
Posts: 11694
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm pretty sure OCR2A = 0 means the counter is jammed at 0.  TOP is the largest value, so if TOP = 0 the counter cannot change and its outputs will be jammed too.
Logged

[ I won't respond to messages, use the forum please ]

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 602
Posts: 33375
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm pretty sure OCR2A = 0 means the counter is jammed at 0.  TOP is the largest value, so if TOP = 0 the counter cannot change and its outputs will be jammed too.
No what happens is that it wraps round and has the same effect as if you put 256 into the counter.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I hope you did it with a spread sheet.

Lua actually.

Code:
for i = 1, 256 do
  print ("OCR2A =", i - 1 .. ", frequency =", round (16e6 / 8/ i), "Hz")
end -- for

This does the same thing without an ISR:

Code:
const byte LED = 3;  // Timer 2 "B" output: OC2B
const long frequency = 20000L;  // Hz

void setup()
 {
  pinMode (LED, OUTPUT);

  TCCR2A = _BV (WGM20) | _BV (WGM21) | _BV (COM2B1); // fast PWM, clear OC2B on compare
  TCCR2B = _BV (WGM22) | _BV (CS21);         // fast PWM, prescaler of 8
  OCR2A =  (F_CPU / 8) / frequency - 1;      // zero relative  
  OCR2B = ((OCR2A + 1) / 2) - 1;             // 50% duty cycle
  }  // end of setup

void loop()
  {
  // do other stuff here
  }

Plug your desired frequency into "frequency".

With the prescaler of 8 you can generate frequencies in the range 7813 Hz to 1 MHz.

Testing appears to confirm that OCR2A being zero doesn't work. It wouldn't with PWM modes because there is no "half" point (of zero) for the PWM duty cycle to be in. You could use OCR2A of zero for other modes. Since that register is zero-relative, an entry of zero means the timer does a single count.
« Last Edit: November 09, 2012, 05:32:10 pm by Nick Gammon » Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 602
Posts: 33375
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
This does the same thing without an ISR:
The thing about using an ISR is that you can get to modulate the signal on a per cycle basis.
Logged

Pages: [1]   Go Up
Jump to: