Using an Ultrasonic Sensor (HC-SR04) with a Tiva C Series micro-controller and Energia
This guide will provide you with everything needed to connect an Ultrasonic Sensor (HC-SR04) to your Tiva C Series based micro-controller. In essence, this guide will work for most 3.3v based controller’s such as a 3.3v based Arduino and compatible boards as the Energia development platform is based off of the Arduino IDE.
What is an Ultrasonic HC-SR04 Distance Sensor Module?
It is a relatively cheap sensor to measure distance between itself and another object by sending an ultrasonic pulse and listening for an echo response. This sensor can be connected to many devices that provide 5v or 3.3v logic. As I will be using a 3.3v logic micro-controller for this demonstration, a 1K resistor will be required in order to not damage the GPIO on the micro-controller. You can purchase these HC-SR04 Sensor’s from SainSmart, they offer pretty decent prices as well as quick shipping.
Connecting everything up
There are four pins on the sensor, and they are fairly straight forward to connect. VCC requires a 5v signal, which in my case is coming from the VBUS pin on the Tiva C Series. If you were using the Arduino, you simply connect to the +5v pin. The GND pin goes to any common ground pin’s on your micro-controller, and the pins “TRIG” and “ECHO” will be connected to any available GPIO pins on your micro-controller. Keep in mind, when using a 3.3v logic controller you will be required to use a 1K resistor between the “ECHO” pin and your GPIO pin on the micro-controller as it by default will send 5v and we need to regulate this.
As I am using a Tiva C Series and like the Arduino IDE, I have adapted to using Energia IDE as it brings the Arduino way of coding to the Tiva. As I am new programmer, I have decided to attempt to write my first C++ library for this sensor. You can download this class library from GitHUB here.
Installation is very straight forward and there is an example included to get you up and running quickly!