Arduino code to generate a 40 khz for sonar transducer

Thread Starter

linolo

Joined Dec 10, 2016
4
Hello everybody

I am working on a project and would like some help. I am looking for Arduino uno code to generate a 40 KHZ pulse for 8 cycles and delay of 50 ms to drive my ultrasonic transmitter. At the moment I am only using the function generator in labs to achieve this.

thanks
 

dannyf

Joined Sep 13, 2015
2,197
It depends on the design of the rest of your application.

The simplest would be there flip the output pin a few times and then wait, as the chip has no ability to generate single pulse from its peripherals .

Otherwise, you can use one timer or externally interrupt to count the pulse output from another. But use latency could be an issue at the kind of frequency you are talking about.
 

Thread Starter

linolo

Joined Dec 10, 2016
4
Thanks dannyf for your reply ill have a look in the link. it looks like your using the pic for the coding.
 

dannyf

Joined Sep 13, 2015
2,197
Another way to generate this pulse training is to use the SPI.

Fire 8 pulses, you can use the sclk line. For other numbers, you can use three sdat / mosi line, by loading thee transmission data register with special values that conform to your desired pattern.

If more than four pulses are transmitted on the sdat line, gaps may exist due to latencies in servicing the use.
 

dannyf

Joined Sep 13, 2015
2,197
here is this approach in action:

1) a 50% pulse train is generated by using the analogWrite() function -> you can configure it to run faster or slower.
2) timer1 is configured to alternate between counting pulses and counting internal clock (to generate a delay).
3) once sufficient number of pulses have been reached, the pwm output is shorted, followed by a period of delay.

Code:
void setup() {
    // put your setup code here, to run once:
  
    IO_OUT(LED_DDR, LED);                            //led as output
  
    //CTRL_PIN initialize as output, idles low -> disabling pwm output
    IO_CLR(CTRL_PORT, CTRL_PIN); IO_OUT(CTRL_DDR, CTRL_PIN);
  
    //to be replaced to generate application specific frequencies
    pinMode(PULSE_PIN, OUTPUT); analogWrite(PULSE_PIN, 127);    //PULSE_PIN 50% duty cycle.
  
  
    //initialize tmr0
    TIFR1 |= (1<<TOV1);                            //clear tmr0 overflow flag by writing '1' to it
    tmr1_init(TMR_8x, 200, 200);                    //set up tmr0, starts running on exit = 8*200*200 / 16M = 20ms delay
    TIMSK1 |= (1<<TOIE1);                            //enable overflow interrupt on tmr0
  
    interrupts();                                    //enable global interrupts
}

void loop() {
  // put your main code here, to run repeatedly:

}
the entire action is done in the isr so no code in the main loop. the program is configured to listen for 4 pulses, and then delay 20ms.

pwm periodic.PNG
 
Last edited:

MrSoftware

Joined Oct 29, 2013
2,188
We used an arduino to generate a PWM signal for controlling an analog servo, and we discovered that the clock is not that stable, at least in our case. The frequency of our output signal would vary slightly, causing our servo to hunt a little bit. I guess my point is, if your 40kHz needs to be bang on perfect, don't be too surprised if the arduino gives you less than perfect timing, especially if it's running any other code at the same time.
 
Top