HC-SR04 Ultrasonic Module Testing Part 2

07.11.17

I would recommend taking a look at the information in the Part 1 project as i cover off on the information sheet and the basic operation of the Module in this project. I have elected to try to use the module with interrupts in this case as I would like to incorporate the ultrasonic measurement into the self balancing robot to avoid obstacles. In the case of the robot we cannot introduce the sorts of measurement delays required so interrupts help us get around that issue.

The link for the YouTube video is below, take a look at it as it covers off on the code and the actual testing.

Youtube Video

The previous project can be viewed at http://www.grumpyoldtech.technology/blog/HC_SR04_Ultrasonic_Module_Testing_Part_1

 

The Arduino code i used for this test is below, just copy and paste it into a blank Arduino project.

A full description of the code is in the YouTube video (link above):


//
//
// HC_SR04 Ultrasonic Sensor test - interrupt driven
//
// Code by: Grumpy Old Tech
//


// Pins

#define trigPin       12
#define echoPin       2
#define echoInterrupt 0

// Variables

volatile long         echoStart;
volatile long         echoDuration;
float                 distance;

volatile int          trigTimer;
volatile int          trigState = 0;

unsigned long         loopTimer;

// Setup

void setup() {

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  
  Serial.begin(9600);                       //Start the serial port at 9600 bps

  TCCR2A = 0;                               //Make sure that the TCCR2A register is set to zero
  TCCR2B = 0;                               //Make sure that the TCCR2A register is set to zero
  TIMSK2 |= (1 << OCIE2A);                  //Set the interupt enable bit OCIE2A in the TIMSK2 register
  TCCR2B |= (1 << CS21);                    //Set the CS21 bit in the TCCRB register to set the prescaler to 8
  OCR2A = 39;                               //The compare register is set to 39 => 20us / (1s / (16.000.000MHz / 8)) - 1
  TCCR2A |= (1 << WGM21);                   //Set counter 2 to CTC (clear timer on compare) mode

  attachInterrupt(echoInterrupt, echoPinChanged, CHANGE); 
  
  loopTimer = micros() + 4000;

}

void loop() {

  // Calculate the actual distance
  // Speed of sound = 340 m/s = 34000 cm/s = 34 cm/ms = 0.034 cm/µs
  // time = distance / speed
  // distance = time x speed
  // distance apart (cm) = time x speed / 2 = time x 0.034 / 2   *** distance is halved as signal goes to the object and back
  distance = echoDuration * 0.034 / 2.0;
  
  // send the value to the serial port
  Serial.print("Distance (cm): ");
  Serial.println(distance);

  // Delay 4 milliseconds
  while(loopTimer > micros());
  loopTimer += 8000;
}


// Interrupts

ISR(TIMER2_COMPA_vect) {

  // generate a echo trigger every 3000 counts = 3000 x 20µs = 60000µS = 60 ms
  trigTimer++;
  if (trigTimer > 3000) {

    trigTimer = 0;
    trigState = 1;
  }

  switch (trigState) {

    case 0:
      digitalWrite(trigPin, LOW);
      break;

    case 1:
      digitalWrite(trigPin, HIGH);
      trigState = 2;
      break;

    case 2:
    default:
      digitalWrite(trigPin, LOW);
      trigState = 0;
      break;
  }
}

void echoPinChanged() {

  switch (digitalRead(echoPin)) {
  
    case HIGH:
      echoStart = micros(); 
      break;
      
    case LOW:
      echoDuration = micros() - echoStart;
      break;
  }
}

 

Comments (0)


Leave a Comment