Ultrasonic

Hello,

I want to control the ultrasonic sensor.

I have a sensor without electronic circuit. The circuit i made myself.

Now i would like to control with 40kHz by using analogwrite.

Which port should i use. My Probelem ist, I get no 40kHz rectangular signal by PWM.

I look forward to your help .

:astonished:

Welcome on the forum

you should provide more information about your goal (google translate might be helpful) and the code you use so far.

Adafruit provide the following code to produce a 38kHz IR carrier.... you can adjust the numbers to try to get 40. It doesn't use analogWrite, but just does digtalWrites. 40kHz needs a total of 25uS.

 // 38 kHz is about 13 microseconds high and 13 microseconds low
   digitalWrite(IRledPin, HIGH);  // this takes about 3 microseconds to happen
   delayMicroseconds(10);         // hang out for 10 microseconds, you can also change this to 9 if its not working
   digitalWrite(IRledPin, LOW);   // this also takes about 3 microseconds
   delayMicroseconds(10);         // hang out for 10 microseconds, you can also change this to 9 if its not working

I found that gave 36kHz with delays of 10; 9 gave closer to 38.

Full code here.

This code gives me a slightly asymmetric wave close to 40kHz

// to get a square wave

byte wavePin = 2;

void setup()
{
  pinMode(wavePin, OUTPUT);
}

void loop()
{

// 40kHz is 25us
   digitalWrite(wavePin, HIGH); // seems about 4
   delayMicroseconds(8);         
   digitalWrite(wavePin, LOW);   
   delayMicroseconds(9);    
}

40kHz.bmp (219 KB)

  • the function call of - loop() - overhead can be removed to make it more symmetrical (see below)
  • using direct port manipulation instead of digitalWrite is faster so better timing possible
  • using a HW timer i.s.o delayMicroseconds will also improve timing
// to get a square wave

byte wavePin = 2;

void setup()
{
  pinMode(wavePin, OUTPUT);
}

void loop()
{
  while(1)
  {
   digitalWrite(wavePin, HIGH); // seems about 4
   delayMicroseconds(9);         
   digitalWrite(wavePin, LOW);   
   delayMicroseconds(9); 
  }
}

This is the code I have for making the PWM go at the sorts of frequencies you want.
Just do an analogWrite to pin 3 with a value of 128.

/* Code to pulse pin 3 with a modulated signal
* By Mike Cook Nov 2011 - Released under the Open Source license
*/

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

void loop(){
// do something here
}

OCR2A controls the PWM frequency, it looks like something in the order of a value of 49 should give you what you want.

Hello at all,

thank you all for your help.
Iam sorry for my english :slight_smile:

So, I will just using only analogwrite. Digitalwrite ist not efficiently.

I solve the problem, with the function pulsein() and libary pwm01.h .

If someone wants the code, I can share it.

greetings

I think you should share it anyway, even if nobody wants it today.... it might help someone in a year or more, you never know.