Understandable Rangefinder Example

This is the circuit I used for the basic rangefinder example we looked at during today’s lecture.

2014-11-19 14.25.28

The connection between the MSP430 and HC-SR04 rangefinder probable merits some explanation:

  1. The trig pin on the rangefinder can safely be connected directly to a digital output pin on the MSP430, because the 3V pulses coming from the MSP430 will not cause any problems in the rangefinder.
  2. However, the 6V pulses (possibly only 5V in practice) coming from the rangefinder’s echo pin should never be connected directly to a digital input pin on the MSP430, because that voltage is too high and could potentially damage the MSP430. That’s why the diode/resistor circuit is used to create a safe interface between the two devices.
  3. When the echo pin of the rangefinder (which is a digital output) is low (0V), which it is most of the time, it pulls down the digital input pin of the MSP430 through the diode (Note: it must be a regular silicon diode – not an LED!). Because there is a small voltage drop (0.7V) across a silicon diode when it is conducting current, the MSP’s digital input pin won’t actually be pulled down all the way to 0V – it will be about 0.7V – but that’s certainly low enough that the MSP430 reads the pin as low (i.e. as binary 0 rather than 1).
  4. When the echo pin of the rangefinder is high (5 or 6V ish), which only happens during an echo pulse, it “tries” to yank the digital input of the MSP430 up to the same voltage (which could damage it), but it can’t because the diode is connected the “wrong way around”! (which is exactly what we intend because raising the MSP430’s digital input to 5 or 6V would probably fry it). Instead, the 10kOhm resistor pulls the digital input up to 3V.
  5. A diode is like a one-way valve for electric current and in this case it’s being used so that the rangefinder echo pin can only pull the MSP430’s digital input in one direction.
  6. The net result is that 5 or 6V pulses coming out of the rangefinder’s echo pin are transformed into 3V pulses at the digital input pin of the MSP430.

This is the C code I used:

// Rangefinder example for MSP430G2553
// Written by Ted Burke - Last modified 19-11-2014
// This program uses a rangefinder to measure the distance to the
// nearest object. If the distance is below a threshold, an LED lights.
//  - Trigger pulses are sent to the rangefinder via P1.0
//  - Each pulses are received from the rangefinder via P1.1
//  - The LED is controlled by P2.0
#include <msp430.h>
int main( void )
    WDTCTL = WDTPW + WDTHOLD; // Disable watchdog timer
    P1DIR = 0b00000001; // Make P1.0 an output for trigger pulses
    P2DIR = 0b00000001; // Make P2.0 an output for indicator LED
        // Send a 100us trigger pulse on P1.0
        P1OUT = 0b00000001;
        P1OUT = 0b00000000;
        // Wait for start of echo pulse on P1.1
        // (i.e. wait while P1.1 stays low)
        while ((P1IN & BIT1) == 0);

        // Delay for fixed time, then check if P1.1
        // is still high. If not, distance is less
        // than threshold
        if ((P1IN & BIT1) == 0)
            P2OUT = 0b00000001; // LED on
            P2OUT = 0b00000000; // LED off
        // Now just wait for echo pulse to end
        // (i.e. wait while P1.1 stays high)
        while ((P1IN & BIT1) > 0);
    // The program never actually reaches this point
    return 0;
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

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

WordPress.com Logo

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s