I’ve been asked to help to write a simple PIC code to interface with the ultrasonic sensor.
It’s actually very simple by referring to the timing diagram.
Connect the Trigger pin to an output and the Echo pin to an input. Just output a 10us pulse on the trigger pin and wait for the feedback on the echo pin.
The pulse width of the echo pin will determine the distance. The longer the pulse width, the further the object is. I’m using the PIC’s built-in timer to time the duration of the pulse width.
The code is written for the Hi-Tech C Lite compiler on MPLAB X.
The schematic.
Here is a video demo 🙂
Respected Sir l am doing a project ultrasonic radar system , I want help for coding ( base on pic18f microntroller ) .plz help mii
nice work. I wonder how this system can be applied to RF signals.
Hello admin i see you don’t monetize your blog. You can earn extra
bucks easily, search on youtube for: how to earn selling articles
//can someone tell me what is wrong with my code? i only see zeros in my serial monitor .
int vals[10];
unsigned int distance = 0;
char txt[20];
void sendPulse(void);
unsigned int readUltrasonic(void);
void main()
{
LATB=0b00000000;
TRISB=0b11110000;
UART1_Init(9600);
ADCON1=0x0F;
T0CON=0x08;
while(1) {
distance = readUltrasonic()/1.7;
IntToStr(distance,txt);
UART1_Write_Text(txt);
Delay_ms(3000);
}
}
void sendPulse(void) {
RB0_bit = 1;
Delay_us(10);
RB0_bit = 0;
}
unsigned int readUltrasonic(void){
unsigned int time = 0;
while(1) { if(RB4==0) { break; } }
sendPulse();
while(1) {
if(RB4==1)
{
TMR0L = 0;
while(1)
{
if(RB4==0){
time = TMR0L;
return time;
}
}
}
}
}
Can i ask for the library of ultrasonic sensor using pic?
u said in the post u are using high tech c lite compiler but now i see your comment is xc8 which one is true??
thank you so much for the effort to help us learn. can you assist me with the code for hc-sr04 sensor and PIC16F877A in MikroC. i have a project that is due and i need this part to work. i am using 20MHz crystal.
sir,we need coding for hcsr04 interfacing with pic16f877a for tank level controller.tank height of 13cm
sir can u give me a code for interfacing the pic16f877a with hcsr04 without lcd display……
sir,we are doing project with ultrasonic sensor[hcsr04] for water level controller.i need coding to trigger the ultrasonic sensor without lcd display….pls help us….
plz any body help me.i want to make a small project using ultasonic hrs04 with OP AMP not any microcontroller.which give output in form of teo led blue and red.when ti are close then it turn red and when it largge range then blue.
or
CONTROLING BI DIRECTIONAL MOTOR WITH ULTRASONIC HR S04(NO MICRO CONTROLLER)
plz any body help me.i want to make a small project using ultasonic hrs04 with OP AMP not any microcontroller.which give output in form of teo led blue and red.when ti are close then it turn red and when it largge range then blue.
hi sir.thank you so much for the code and set up.it works well.but the max dist(im getting) shown is only 150 cm.but the rangeof hc-sr04 is supposed to be 2-400cm right?im using pic 16f877a with same set up.only using 20mhz crystal instead of 16 mhz crystal…could you please help me!!
You’re welcome.
Try to read the raw ADC values instead. In my code I have translated it into cm.
Have a look at the raw values and try to figure out how to scale it to the max distance of 400cm.
sir can you show how to make that circuit in isis which..i be grateful if u could reply that
Does anyone have HEX file for this code please?
It’s in the download link.
Hola muy buen trabajo, solo quisiera saber como compilarlo en el MPLAB X gracias.
hi , can you give me the hex file please , I will very very very very very thankful .
amazing work sir .. hope the best for you
I just want to ask about which file of these files i’ll burn on the pic
hi there,
glad to found your project interfacing between PIC and Ultrasonic sensor.But could you show me how to use this code for assembly language?
Thank you
I can’t help you with that unfortunately. Assembly language is too time consuming.
Oh. I got V. Brown in the Rice deal, too. That makes it better, right? I appreciate the banter though guys. I knew I’d get ripped. I’d be on board with the ripping. In the future, I’ll keep my trades from “that league” to myself. lol…VN:F [1.9.21_1169](from 3 votes)
That’s really shrewd! Good to see the logic set out so well.
No question this is the place to get this info, thanks y’all.
Could I possibly repost this to one of our sites with this subject? I’d link back to the authentic, of study course. Let me personally know by simply email everything you think?
Nice work.
just a question how you calculate the distance? on the code it shows the number of counts d=(TMR0)/1.7 the results in cm how you get this?
Distance=(velocity of sound)*(time delay)/2, with velocity =340m/s (at 20 degree Celsius), and what is the duration of the timer since it operates on 16MHz crystal is it 1/16microsecond per unit (in timer TMR0) ?.
Thank you
Got it!..
1/[(Fosc/4)/prescaler=128]*340m/s/2=1.8 instead of 1.7
Sorry for the late reply. Actually the value obtained from the PIC is a rough estimate of distance, so I just use 1.7 from trial and error. I used a ruler to measure the real distance to compare what I got from the PIC.
Personally I think the Tenerians shouldve been more healthier than the Kiffians. Fish and a farming guaranteed higher grade protein and continuous supply of nourishment on daily basis. Hunting large game is never a sure thing and wont exactly turn you into a superman either.
Sir I am working on a project which deals with PIC18F46k22..Sir could u help me out to interface the HC SR-04 Ultrasonic sensor with PIC18F46k22 with its code? It’s urgent, kindly help me out.
Another thing sir is… the sensor need around 10us of delay to work.. what will happened if supplied with 16us of delay?
I think it will work but it will overlap with the echo signal.
Hello sir, just received my HC-SR04 but the PIC still not arrived.. I want to know how can I test it if the unit is not faulty? can i just supply a voltage to the trigger pin and vcc while grounding the gnd???
You can power the sensor but the output pulses are so fast that you cannot see it by just putting an LED.
However you can use an oscilloscope and set it to trigger on positive edge while measuring the echo pin.
Of course you’ll need a tiny pulse on the trigger pin. I think a push button will do.
sir,,i want to ask ,if i use pic 16f877,which one should i change on the coding?what is the different between this two types of pic?
I believe they’re similar. Try to upload the same code and see what happens.
I think it should work just fine.
thanks sir i will..
SIR CAN I GET THE CODE FOR HC SR04 PLZZZZZZZZZZZZZZZZ
The code can be downloaded above.
It’s written in MPLAB X and using the XC8 Compiler.
If you’re using another compiler you’ll have to convert the code yourself.
Hello sir,
May I know what crystal did you used on the project?
16mhz
sir ,i am unable to find the code given in this article can u mail it to me.
Click on the Download link above. It’s a RAR file, use WinRar to extract it.
This code is for MPLAB X. It’s an entire folder filled with random files and the code is in it (newmain.c).
There is also a header file there for the LCD.
If I were to send it to your e-mail the attachment will be like 20 files.
sir can u give me a code for interfacing the pic16f877a with hcsr04 please..its urgent
respected sir/mam,
i am doing a project based on ultrasonic sensor .your this article is really very helpful for me.
i need your help.in our project we have to interface four sensors like this.i will be very thankful to you if u guide me about interfacing.i would also like to refer your code.thank u..
Just make 4 different copies of the sendPulse() and readUltrasonic() functions and change to the corresponding pins of your sensors. Remember Trigger pin should be an output while the Echo pin should be an input. Set the corresponding TRIS registers also.