Trigger Ultrasonic sensor hc-sr04 with PIC18F4520

Discussion in 'Embedded Systems and Microcontrollers' started by thar07, Sep 30, 2015.

  1. thar07

    Thread Starter Member

    Jan 3, 2015
    71
    0
    I'm going to interface Ultrasonic sensor hc-sr04 with PIC18F4520 to detect the obstacles (not to measure the distance). The sensor needs be triggered by a 10 micro-seconds pulse regularly. But the problem is when the pulses are generated by using timer or registers and send it to the sensor, the main program has to be paused until this being done. This slow down the main process.


    Is there a any other way to generate 10uS pulse ?
    If not how to manege this with the main program without compromising much time ?
     
  2. nerdegutta

    Moderator

    Dec 15, 2009
    2,515
    785
    How about interrupts? Can you use that?
     
  3. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    How about a pulse-width modulated output at a regular rate, set up so that it spends 10usec high? That requires zero processor involvement. But it may be that you can't get the repitition rate low enough. If that's a problem, you could turn on the timer, let it send a single PWM pulse and turn it off again.

    I suspect that you may need an interrupt to trigger the sensor and start the pulse-measurement operation (length of output pulse will be proportional to distance) but you can use a simple flag to detect whether the pulse has ended, if it gates a timer in the processor. But then again, if you're not very fussy about exact intervals between operations, you could just poll for whether a timer has elapsed and if so, trigger an operation without an interrupt. But you know, the processor is there to work for you; you can't build a project without making it do stuff.
     
  4. thar07

    Thread Starter Member

    Jan 3, 2015
    71
    0
    How to send a signal by interrupts ?
     
  5. thar07

    Thread Starter Member

    Jan 3, 2015
    71
    0
    Are you talking about ECCP or CCP modules ?

    TO trigger the sensor 10uS pulse should be sent time to time, like every 1 or 2 seconds.

    Is there a way to generate and send the pulse while the micro controller doing other things?
     
  6. nerdegutta

    Moderator

    Dec 15, 2009
    2,515
    785
    Read the sections about interrupts and timers in the datasheet.
     
  7. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    Are you talking about ECCP or CCP modules ?

    No, just the regular PWM output. But if you want to get a pulse every second or two, it won't be usable. However, if you're so short of processing power that doing this timing function is too much, you may need a bigger processor, or two processors, or something similarly radical.
     
  8. nerdegutta

    Moderator

    Dec 15, 2009
    2,515
    785
    He could perhaps use a 555 timer in astable mode?
     
  9. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    I would set you to looking at Timer2 in the data sheet. I will often use this to set up an automatic heartbeat timer where I increment a global variable once each and every millisecond so I have a simple way to time things out. The work is done inside an ISR so the main program need not worry about this, and I will oft put a few other things inside the routine too like button debouncing.

    Timer2 has both pre and post scalers, plus a compare register. Knowing the system clock you can set this up for some regular interval.

    Say your interval is 1 ms. Then you can increment a variable such that when it reaches 1000 (one second) you can then fire off that 10us pulse. For such a short pulse I would just make a dumb delay inside the ISR as you only loose .001% of your working time to do this.

    By setting variables you can make flags for the ISR to let the main loop know to do things. That depends on the overall design of your program.

    You would have to do some more explaining as to why you would need an external part to make this signal. The micro is all you need.
     
  10. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,677
    2,729
    What is the period?
     
  11. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    See post #5. TS states he needs a pulse every second or two.
     
  12. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,677
    2,729
    Ok....is that exactly every second or two or roughly every second or two?
     
  13. GopherT

    AAC Fanatic!

    Nov 23, 2012
    6,029
    3,790
    The sensor works if you have a 10 uSecond pulse or a 200uSecond pulse. Also, the sensor is activated on the high to low.

    That means, you can send the triggering pin high, then do some things with your program. When you get ready in another part of your program, pull the triggering pin low again. Just make sure you leave the triggering pin low for at least 60 mSec before going high again (echo must be received before you send the trigger pin high to prepare for the next trigger).


    The next issue is, the pulse does not start the clock on the Ultrasonic sensor - it just initiates the sequence. The output of the sensor will go high once the chirp is sent (That chirp and output pin going high is what starts the timing for distance sensing).

    Unfortunately, the HC-SR04 is not self-resetting like a PING))) sensor from Parallax. That means, if nothing is within 120" (about 4 meters), then the output will stay high. You can re-trigger it, it will send another chirp but the output will already be high. It will only go low again after it finds an echo.

    The good news about the HC-SR04 is that they cost about $2 while the parallax sensor is about $30.
     
  14. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
  15. dannyf

    Well-Known Member

    Sep 13, 2015
    1,787
    360
    The simplest way to generate it is via a bunch of NOP().

    Listening to the echos will depend on the distance, and desired resolutions. But some form of capture or external interrup t/ pin change interrupt would be doable. if you don't care much about precision, counting in a loop would also work as well.
     
Loading...