Therefore, we initialize the RB0 pin as a digital output pin and RB4 pin as a digital input pin. RB0 is used to provide 10us trigger signal to HC-SR04 trigger pin and RB4 pin is used to detect positive and negative edges of echo output signal of ultrasonic sensor. Initialize the LCD library and set the PORTD as a digital output pin. If you don’t know how to use GPIO Ports of pic microcontrollers, you can refer to this article: End LCD module connections Enable GPIO Pins Also, connect RS pin with RD0 and EN pin with RD6. These lines define the connection of PORTD pins RD5-RD2 with D4-D7 pins of LCD. We just need to define the connection of LCD pins with pic microcontroller pins. In mikroC for pic compiler, LCD library provides all functions to print data on the screen. Time_taken= (TMR1L | (TMR1H=0 & time_taken<=400)įirst, we define the PIC16F877A microcontroller pins to which 16×2 LCD data and command pins are connected. While(PORTB.F4) /* wait till echo output signal goes low */ While(!PORTB.F4) /* wait till echo output signal goes high */ * send 10us puls to triger pin of HC-SR04 from RB0 pin */ T1CON = 0x10 /* Set timer1 prescaler value to 1:2 prscale and disable timer1 */ Lcd_Cmd(_LCD_CLEAR) /* Clear text from LCD */ PORTB.B4 =0 /* Initially sets the RB4 pin as activ low */ĭelay_ms(2000) /* wait for one second */ TRISB.B4 = 0 /* Set RB4 pin as a digital input pin */ PORTB.B0 =0 /* Initially sets the RB0 pin as activ low */ TRISB.B0 = 0 /* Set RB0 pin as a digital output pin */ Lcd_Cmd(_LCD_CURSOR_OFF) /* Turn of LCD Cursor display */ Lcd_Cmd(_LCD_CLEAR) /* Clear whatever is written on LCD */ Lcd_Init() /* initialize LCD library functions */ TRISD=0x00 /* initialize PORTD as digital ouput pin for LCD */ * Main code to take distance measurement and initializations */ * variables to store pulse duration and to display string on LCD 8 */ The crystal oscillator frequency is 8HMz. You may refer to this article:Ĭode is written using Mikro c for pic and it will work perfectly with pic16f877a and pic16f887 microcontroller. If you do not know how to interface LCD with pic microcontroller. The circuit given below shows the connections between Ultrasonic sensor HC-SR04, 16×2 LCD and Pic microcontroller. 16×2 LCD is used to display the measured distance value. After that, multiply the pulse duration with the speed of light to get the measured distance from pulse duration. Finally, this will give us the pulse duration. To convert, these ticks into pulse duration, divide the number of ticks with the operating frequency of the timer module. This variable contains the number of ticks of the timer for the time duration of the output pulse. Now, save the content of the timer registers into a variable. As soon as the falling edge is detected, disable the timer module. After that wait for the falling edge to occur. To measure pulse duration with the PIC16887 timer, detect the rising edge of the pulse as soon as the rising edge is detected, enable the timer module and the timer will start counting. We have previously posted an article on how to use timers of pic microcontroller, you can refer to this article: But 1mm is not an issue when measuring distance in the units of cm and meter. Both sensors have the same electrical properties and can be used in replacement for each other except HY-SRF05 has one 1mm better precision than HC-SR04. In this tutorial, we will be using HC-SR04 ultrasonic sensor but there is another version of this sensor which is HY-SRF05. Before we proceed further with this tutorial, we recommend you to read about ultrasonic sensors and its working here: This sonar sensor has important and useful applications for embedded systems projects such as contactless range meter, contactless water level measurement, and sonar system. But the concepts, you will learn to interface the HC-SR04 ultrasonic sensor with any pic microcontroller remain the same. Science Buddies also hosts a library of instructions for over 1,500 other hands-on science projects, lesson plans, and fun activities for K-12 parents, students, and teachers! Visit us at to learn more.In this tutorial, we will learn to interface HC-SR04 ultrasonic sensor with pic microcontrollers and how to measure distance with the HC-SR04 ultrasonic sensor? There are a variety of Pic microcontroller chips available. See our complete playlist of Arduino tutorials here: and the Science Buddies website for cool science projects you can do with an Arduino. We have a separate video that covers the PING))) ultrasonic distance sensor. Learn how to use the HC-SR04 ultrasonic distance sensor, also called a sonar sensor, to measure the distance to a nearby object using an Arduino.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |