Ultrasonic Sensor HC-SR04 Interfacing With MSP-EXP430G2 TI Launchpad

Overview of Ultrasonic Sensor

HC-SR04 Ultrasonic Sensor Module
HC-SR04 Ultrasonic Sensor Module

 

Ultrasonic Module HC-SR04 works on the principle of SONAR and RADAR system. It can be used to determine the distance of an object in the range of 2 cm – 400 cm.

The module has only 4 pins, Vcc, Gnd, Trig, and Echo.

When a pulse of 10µsec or more is given to the Trig pin, 8 pulses of 40 kHz are generated. After this, the Echo pin is made high by the control circuit in the module. The echo pin remains high till it gets the echo signal of the transmitted pulses back.

The time for which the echo pin remains high, i.e. the width of the Echo pin gives the time taken for generated ultrasonic sound to travel to the object and back.

Using this time and the speed of sound in air, we can find the distance of the object using the simple formula for distance using speed and time.

For more information about ultrasonic module HC-SR04 and how to use it, refer to the topic Ultrasonic Module HC-SR04 in the sensors and modules section.

 

Connection Diagram of HC-SR04 with MSP-EXP430G2 TI Launchpad

Interfacing Ultrasonic Sensor Module With MSP-EXP430G2 TI Launchpad
Interfacing Ultrasonic Sensor Module With MSP-EXP430G2 TI Launchpad

 

Finding the distance of obstacle using the ultrasonic module With MSP-EXP430G2 TI Launchpad

Here, we will be using Sheldon Will’s HC-SR04 library from GitHub.

Download this library from here

Extract the library and add it to the libraries folder path of Energia IDE. The folder needs to be restructured for using the library and for it to be visible in the examples of Energia IDE

For information about how to add a custom library to the Energia IDE and use examples from it, refer to Adding Library To Energia IDE in the Basics section.

We have provided the modified library folder in the attachments section below.

Note: If you choose to download the library from the link given above, structure the folder such that hcrs04.h and hcrs04.cpp are immediately inside the folder and not in some sub-folder. Also, rename the hcsr04.ino to a name of your liking and put it in a subfolder of the same name.

We have modified the hcsr04.ino according to the interfacing diagram shown above. The modified sketch is given in the source code section below and is also available in the sketch below.

 

Tread Carefully: MSP-EXP430G2 TI Launchpad board has a RAM of 512 bytes which is easily filled, especially while using different libraries. There are times when you need the Serial buffer to be large enough to contain the data you want and you will have to modify the buffer size for the Serial library. While doing such things, we must ensure that the code does not utilize more than 70% RAM. This could lead to the code working in an erratic manner, working well at times, and failing miserably at others. 

There are times when the RAM usage may exceed 70% and the codes will work absolutely fine, and times when the code will not work even when the RAM usage is 65%. 

In such cases, a bit of trial and error with the buffer size and/or variables may be necessary.

 

Ultrasonic Sensor HC-SR04 Code for MSP-EXP430G2 TI Launchpad

#include <hcrs04.h>

#define PINTRIG 6
#define PINECHO 5

hcrs04 mySensor(PINTRIG, PINECHO);
 
void setup()
{
  Serial.begin(9600); /* Define baud rate for serial communication */
  Serial.println("Basic HC-SR04 Ultrasonic Sensor Example");
  mySensor.begin(); /* Initialize the sensor */
}

void loop()
{
  //Get results from the sensor
  float DISTANCE = mySensor.read(); /* Read the distance value */
  Serial.print("Distance : ");
  Serial.print(DISTANCE);
  Serial.println(" cm");
  delay(200);
}

 

Video of Distance Measurement using HC-SR04 and MSP-EXP430G2 TI Launchpad


Components Used

TI Launchpad MSP-EXP430G2
TI Launchpad MSP-EXP430G2
1
Ultrasonic Module HC-SR04
Ultrasonic module HC-SR04 is generally used for finding distance value and obstacle detection. It can operate in the range 2cm-400cm.
1

Downloads

HC-SR04_Interfacing_With_TI_Launchpad_INO Download
HC-SR04 Library Download
Ad