Arduino code to generate a 40 khz for sonar transducer

Discussion in 'Programmer's Corner' started by linolo, Jan 5, 2017.

  1. linolo

    Thread Starter New Member

    Dec 10, 2016
    4
    0
    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
     
  2. dannyf

    Well-Known Member

    Sep 13, 2015
    2,196
    417
    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.
     
  3. linolo

    Thread Starter New Member

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

    Well-Known Member

    Jun 4, 2014
    2,290
    451
  5. linolo

    Thread Starter New Member

    Dec 10, 2016
    4
    0
  6. dannyf

    Well-Known Member

    Sep 13, 2015
    2,196
    417
    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.
     
  7. dannyf

    Well-Known Member

    Sep 13, 2015
    2,196
    417
    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 (Text):
    1.  
    2. void setup() {
    3.     // put your setup code here, to run once:
    4.  
    5.     IO_OUT(LED_DDR, LED);                            //led as output
    6.  
    7.     //CTRL_PIN initialize as output, idles low -> disabling pwm output
    8.     IO_CLR(CTRL_PORT, CTRL_PIN); IO_OUT(CTRL_DDR, CTRL_PIN);
    9.  
    10.     //to be replaced to generate application specific frequencies
    11.     pinMode(PULSE_PIN, OUTPUT); analogWrite(PULSE_PIN, 127);    //PULSE_PIN 50% duty cycle.
    12.  
    13.  
    14.     //initialize tmr0
    15.     TIFR1 |= (1<<TOV1);                            //clear tmr0 overflow flag by writing '1' to it
    16.     tmr1_init(TMR_8x, 200, 200);                    //set up tmr0, starts running on exit = 8*200*200 / 16M = 20ms delay
    17.     TIMSK1 |= (1<<TOIE1);                            //enable overflow interrupt on tmr0
    18.  
    19.     interrupts();                                    //enable global interrupts
    20. }
    21.  
    22. void loop() {
    23.   // put your main code here, to run repeatedly:
    24.  
    25. }
    26.  
    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: Jan 6, 2017
  8. linolo

    Thread Starter New Member

    Dec 10, 2016
    4
    0
    thanks dannyf for your time and help, ill try the code and then get back for any feedback
     
  9. MrSoftware

    Active Member

    Oct 29, 2013
    547
    133
    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.
     
Loading...