i want to run two HC-SR04 on one PIC16F877A and send the value mesured by the two ultrasonic to serial port.

this is my code using PIC C Compiler :

#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8) 
#define e1 PIN_B6
#define t1 pin_B7 
#define e2 pin_B4 
#define t2 pin_B5 
int a; 
int distanse(int,int); 

void main()

int u1,u2;
printf("%3u", u1);
printf("%3u", u2);


int distanse(int e,int t){
long long counter=0;
return counter/3.333333;

but the computer received random values ! what is the problem ?

  • $\begingroup$ For debugging, add a printf of counter in your so-called "distanse" function. Maybe the long long counter result isn't fitting into an integer $\endgroup$ Commented Apr 27, 2015 at 18:43

1 Answer 1


Ultrasonic sensors work by emitting a set of pulses and reading the return signal (here is a better overview), this causes problems when you have more than one as they cannot distinguish between their own sensor and the other. I would suggest putting a 500ms delay after each reading to avoid as much interference as possible while you keep the same read rate.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.