I was attempting to interface ultrasonic sensor HC-SR04 with Landtiger NXP (LPC1768) but the distance reading is always zero. I tried using pin 2 and pin 3(trigger, echo) and then changed to pin 0, pin 1 (trigger, echo) but without any valid reading. It’s always zero. Please can someone help?
I use the board nxp lpc1768 and in the datashhet of hc-sr04 there is really no description of the trigger and echo pin. pin2 for the trigger and pin3 for the echo do not work with my board (Landtiger v2.0 nxp lpc1768)
int main(void)
{
SystemInit(); //Gets called by Startup code, sets CCLK=100Mhz, PCLK=25Mhz
initTimer0(); //Init Timer for delay functions - defined in ocf_lpc176x_lib.c
initUART0();
int echoTime=0;
float distance=0;
LPC_GPIO0->FIODIR |= (1<<2); //Set P0.2(TRIG) as output
LPC_GPIO0->FIODIR &= ~(1<<3); //Set P0.3(ECHO) as input (explicitly)
LPC_GPIO0->FIOCLR |= (1<<2); //Set P0.2 LOW initially
while(1)
{
//Output 10us HIGH on TRIG pin
LPC_GPIO0->FIOSET |= (1<<2);
delayUS(10);
LPC_GPIO0->FIOCLR |= (1<<2);
while((!LPC_GPIO0->FIOPIN &(1<<3))); //Wait for a HIGH on ECHO pin
startTimer0(); //Start counting
while(LPC_GPIO0->FIOPIN &(1<<3)); //Wait for a LOW on ECHO pin
echoTime = stopTimer0(); //Stop counting and save value(us) in echoTime
distance = (0.0343 * echoTime)/2; //Find the distance
printf("Distance = %0.2fcm\n",distance);
delayMS(1000); //1 update per second
}