I am trying to read my ultrasonic sensor. It sends a pulse in the trigger pin and the echo pin receives the pulse. Using pulseIn I can know exactly the time it took to the pulse go and back. But I need to use port register which is much faster and I need it to be fast in this specific case.

My code is very simple:

void setup() {

    //pin 12 as output
    DDRB = B00010000;
    //pin 12 as low
    PORTB = B00000000;


void loop() {

    //pin 12 as high
    PORTB = B00010000;
    //pin 12 as low
    PORTB = B00000000;

    long start_time = micros();

    //While reading high in pin 13 keep running the loop
    while (PINB & B00100000)) {


    Serial.println(micros() - start_time);



The problem is that it does not work, I mean, the while loop is not being processed so the serial is always printing a very small number. It doesnt matter if I move the ultrasonic sensor to distant walls the while loop still does not work. What am I doing wrong?

  • How have you wired it up? Dec 4, 2014 at 3:22
  • VCC to 5V, GND to GND, trigger to 12 and echo to 13.
    – Samul
    Dec 4, 2014 at 3:31
  • And does echo follow the behavior described in your code (normally high, pulls low on receive)? Where is the datasheet for the sensor? Dec 4, 2014 at 3:32
  • It's the HC-SR04 ultrasonic sensor. With pulseIn it works perfectly, but using PINB it does not.
    – Samul
    Dec 4, 2014 at 3:35

1 Answer 1


According to anything I could find, the Echo output is normally low and then goes high for a duration equal to the range. Your initial while condition is inverted, and you need to check for both edges.

unsigned long up, down;

// trigger pulse
while (!(PINB & _BV(PB5)))
  ; // wait for leading edge
up = millis();
while (PINB & _BV(PB5))
  ; // wait for trailing edge
down = millis();
Serial.println(down - up);
  • Thank you so much for helping but there is a problem: in your code you putted PIND, but it should be PINB right? Another thing: when I try to copile your code I get error saying that PB5 was not declared. I changed it to only 5 ok? But it still does not work. Could you please check what am I doing wrong? I edited your question to add my code cause I couldnt add it here in the coments ok? Sorry for that.
    – Samul
    Dec 4, 2014 at 12:11
  • Use PINB5 instead.
    – Gerben
    Dec 4, 2014 at 16:55
  • still having problem. Look: when my sensor is doing nothing it registers LOW. When an ultrasonic pulse arrives it registers HIGH right? So why do I need 2 whiles? Couldnt I just start a timer when the trigger sends a pulse and stop the timer when sensor reads HIGH? I tried "while sensor is not high keep looping" but it does not work. What am I doing wrong? The HIGH/LOW logic of mine is wrong?
    – Samul
    Dec 5, 2014 at 3:13
  • The duration the Echo signal is held high is proportional to the distance detected. Dec 5, 2014 at 8:02
  • So you are saying that this image is wrong? forum.arduino.cc/…
    – Samul
    Dec 5, 2014 at 12:34

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.