0

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() {

    Serial.begin(9600); 
    //pin 12 as output
    DDRB = B00010000;
    //pin 12 as low
    PORTB = B00000000;

}

void loop() {

    //pin 12 as high
    PORTB = B00010000;
    delayMicroseconds(10);
    //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);

    delay(1000);

} 

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?

4
  • How have you wired it up? Commented Dec 4, 2014 at 3:22
  • VCC to 5V, GND to GND, trigger to 12 and echo to 13.
    – Samul
    Commented 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? Commented 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
    Commented Dec 4, 2014 at 3:35

1 Answer 1

2

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);
5
  • 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
    Commented Dec 4, 2014 at 12:11
  • Use PINB5 instead.
    – Gerben
    Commented 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
    Commented Dec 5, 2014 at 3:13
  • The duration the Echo signal is held high is proportional to the distance detected. Commented Dec 5, 2014 at 8:02
  • So you are saying that this image is wrong? forum.arduino.cc/…
    – Samul
    Commented 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.