Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using an HC-SRO4 ultrasonic sensor for a wall-avoidance, maze-solving robot. However, I have recently run into an issue where I am getting a whirring noise from the sensor because it is taking measurements way too often.

This is the code I have right now:

const int serialPeriod = 250;       // only print to the serial console every 1/4 second
unsigned long timeSerialDelay = 0;

const int loopPeriod = 20;          // a period of 20ms = a frequency of 50Hz
unsigned long timeLoopDelay   = 0;

// specify the trig & echo pins used for the ultrasonic sensors
const int ultrasonic2TrigPin = 8;
const int ultrasonic2EchoPin = 9;

int ultrasonic2Distance;
int ultrasonic2Duration;

void setup()

    // ultrasonic sensor pin configurations
    pinMode(ultrasonic2TrigPin, OUTPUT);
    pinMode(ultrasonic2EchoPin, INPUT);

void loop()
    debugOutput(); // prints debugging messages to the serial console

    if(millis() - timeLoopDelay >= loopPeriod)
        readUltrasonicSensors(); // read and store the measured distances

        timeLoopDelay = millis();

void readUltrasonicSensors()
    // ultrasonic 2
    digitalWrite(ultrasonic2TrigPin, HIGH);
    delayMicroseconds(10);                  // must keep the trig pin high for at least 10us
    digitalWrite(ultrasonic2TrigPin, LOW);

    ultrasonic2Duration = pulseIn(ultrasonic2EchoPin, HIGH);
    ultrasonic2Distance = (ultrasonic2Duration/2)/29;

void debugOutput()
    Serial.print("timeSerialDelay: ");

    if((millis() - timeSerialDelay) > serialPeriod)
        Serial.print("ultrasonic2Distance: ");
        Serial.print("cm: ");

        timeSerialDelay = millis();

and my wiring:

breadboard wiring

Link to product

Does anyone know why the scanning is happening constantly? It should only be happening every 1/4 of a second, not constantly. The weird thing is that this wasn't happening earlier before I started using the cable.

I believe it has something to do with the 8.5" extension cable I have (needed because I can't mount three sensors, one facing to each side and one in front, on a single breadboard). This is because when I mounted the sensor directly onto the breadboard, the delay was correct and the whirring was not present. However, as I just mentioned, I need the extension cable to be able to have the three sensors.

Does anyone have a solution for this?


share|improve this question
If loopPeriod = 20 then it should be sampling at 50Hz, like the comment says – TheDoctor Mar 23 '14 at 15:49
Change loopPeriod to 250 for a quarter second sample rate – TheDoctor Mar 23 '14 at 15:49
Okay, did that. It did slow down the rate, but I could still hear a faint clicking which got louder as it got closer to an object and it will pause if the value reaches 0cm. Why is this? – RPi Awesomeness Mar 23 '14 at 16:02
Most ultrasonic sensors emit a bit of sound in the audible spectrum. I have noticed this a lot in the Maxbotics EZ line. – TheDoctor Mar 23 '14 at 16:07
So, it is just something I have to put up with? Okay. I suppose I can survive that. But why is the scanning pausing (and dropping to 1/4 second rates) when it reaches 0cm? Thanks for the quick response! – RPi Awesomeness Mar 23 '14 at 16:09
up vote 3 down vote accepted

If you want to sample at 4Hz (4 times per second), you need to change your headers accordingly:

const int serialPeriod = 250;       // only print to the serial console every 1/4 second
unsigned long timeSerialDelay = 0;

const int loopPeriod = 250;          // a period of 250ms = a frequency of 4Hz
unsigned long timeLoopDelay   = 0;
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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