Using An Ultrasonic Sensor (HC-SR04) with a 3.3v micro-controller (Tiva C Series)

Using an Ultrasonic Sensor (HC-SR04) with a Tiva C Series micro-controller and Energia

HC-SR04-2This 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.

HC-SR04 Ultrasonic Sensor_bbEnergia Based C Library for the HC-SR04 Sensor

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!


10 thoughts on “Using An Ultrasonic Sensor (HC-SR04) with a 3.3v micro-controller (Tiva C Series)

  1. a 1k resistor does not regulate a 5V output. you need either a voltage divider or a level shifter circuit.
    Using a 1K resistor will damage the port over time or immediately

    • Well, myself and what appears to many others agree, that level shifting is generally overkill in this application and in fact a 1k resistor is sufficient.

      Extra precaution would in fact be to use a resistor divider with a 2k2 and a 3k3 resistor, but for the most part this is for personal satisfaction. But really, for this application, again, a 1k resistor is more than enough to do current limiting.

      But thanks for the comment.

    • It will take whatever you’re giving it. I can’t recall what the the minimum specs are, but 3.3v logic works fine. You can check the datasheet to find out what the voltage range is accepted for the TRIG pin.

      • Thanks for the quick reply. Let me try that out. Meanwhile, The datasheet said at least a 10us high need to be input to TRIG to start sensing. I assume that’s a hard requirement.:)

  2. The Tiva C TM4C123G Launchpad is 5V tolerant (on most pins) as noted on section 10.1 of the datasheet of the MCU, so what you are doing is only limiting the current of the input, which is a good idea, but it has an effect on the response to inputs (the switch curve is delayed).

    I’m not sure that this has a negative effect on the precision of the measurements since I haven’t tested it yet, but I intend to do so.

    The “sweet spot” is just a bit under (10%) the max amount of current that the pin is able to sink according to the specs, so you should set the resistance accordingly.

    • Have you been able to test the precision of measurements yet? If so, what methods do you use to test? Sounds like you know alot more than me when it comes to this, I am very amateur! But I am about to implement these sensors in a real world situation and wouldn’t mind knowing if I am reading false results or not. Thanks!

      • I haven’t had time to measure this implementation, but as a general idea, just putting a resistor in series doesn’t lower the V on the MCU pin, it only reduces the current that the pin is sinking. In this case, the Tiva C pin s 5V tolerant, so the resistor is not needed.

        If you have an MCU that is not tolerant to 5V you should use a voltage divider (2 resistors) to lower the voltage on the MCU pin and thus making it a safe implementation. This solution is not optimal since it wastes energy on each resistor, but it is safe for the MCU.

        If you want to check if there is a deviation in your measurements, use an oscilloscope to look at the pulses on the MCU pin, if you see square pulses everything’s ok, if they are more like shark fins, then you could have a small problem, depending on the delay of the rise time (most likely negligible).

        My point is, don’t just put resistors in series, they can alter the way the system works.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s