Trigger Ultrasonic sensor hc-sr04 with PIC18F4520

Thread Starter

thar07

Joined Jan 3, 2015
71
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 ?
 

John P

Joined Oct 14, 2008
2,026
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.
 

Thread Starter

thar07

Joined Jan 3, 2015
71
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.
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?
 

John P

Joined Oct 14, 2008
2,026
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.
 

nerdegutta

Joined Dec 15, 2009
2,684
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.
He could perhaps use a 555 timer in astable mode?
 

ErnieM

Joined Apr 24, 2011
8,377
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.
 

GopherT

Joined Nov 23, 2012
8,009
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.
 

dannyf

Joined Sep 13, 2015
2,197
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.
 
Top