Rangefinder example circuit and program from today’s lecture

In today’s lecture, we went through an example circuit and program using the HC-SR04 ultrasonic rangefinder. This was the actual breadboard circuit I used for the demo:

20150311_135858

This circuit diagram shows the interface between the rangefinder and the MSP430 microcontroller. If the two devices were running at the same supply voltage, then the interface would simply consist of two wires. However, the ultrasonic rangefinder requires a higher supply voltage than the MSP430. Here, the MSP430 is supplied from the 3.3V regulator, whereas the rangefinder is supplied directly from the battery pack (6V ish). Therefore the echo pulses from the rangefinder cannot be connected directly to the MSP430 in case they damage it.

The exact value of the resistor R is not too important. It’s just a pull-up resistor for the digital input of the MSP430 (P1.3). It gently pulls the voltage up to 3.3V. However, if the echo pin of the rangefinder is low (0V), it pulls P1.3 low through the diode. Conversely, when the echo pin is high, it can’t pull P1.3 up to 6V because the diode is reverse biased. Therefore the diode allows the echo pin to pull P1.3 down, but it doesn’t let it pull it up.

20150311_160945

Here’s the example program we went through in today’s lecture:

//
// RoboSlam Example: Rangefinder test
// Code is for MSP430G2452 or MSP430G2553
// Written by Ted Burke - Last updated 11-3-2015
//

#include <msp430.h>

// main function
int main( void )
{
    // disable watchdog timer to prevent time out reset
    WDTCTL = WDTPW + WDTHOLD;

    // configure digital inputs and outputs
    P1DIR = 0b00001000;   // Port 1 outputs: P1.3 (trig)
    P2DIR = 0b00111000;   // Port 2 outputs: P2.3 (LED), P2.4-5 (motor)

    // test LED and motors
    int d;
    while(1)
    {
        // Send 20us trigger pulse
        P1OUT |= BIT3;
        __delay_cycles(20);
        P1OUT &= ~BIT3;
        
        // Wait for start of echo pulse
        while((P1IN & BIT2) == 0)
        {
            // nothing here
        }
        
        // Measure how long the pulse is
        int d = 0;
        while((P1IN & BIT2) > 0)
        {
            // The following delay was worked out by trial and error
            // so that d counts up in steps corresponding to 1cm
            __delay_cycles(30);
            d = d + 1;
            if (d >= 400) break;
        }
        
        if (d < 30)
        {
            P2OUT = 0b00101000; // forward
        }
        else
        {
            P2OUT = 0b00010000; // reverse
        }
        
        __delay_cycles(100000);
    }
}

Here’s the final modified version we saw at the end of the lecture, in which the rangefinder code has been encapsulated in a function:

//
// RoboSlam Example: Rangefinder test
// Code is for MSP430G2452 or MSP430G2553
// Written by Ted Burke - Last updated 11-3-2015
//

#include <msp430.h>

// function prototype
int distance();

// main function
int main( void )
{
    // disable watchdog timer to prevent time out reset
    WDTCTL = WDTPW + WDTHOLD;

    // configure digital inputs and outputs
    P1DIR = 0b00001000;   // Port 1 outputs: P1.3 (trig)
    P2DIR = 0b00111000;   // Port 2 outputs: P2.3 (LED), P2.4-5 (motor)

    // test LED and motors
    int d;
    while(1)
    {
        d = distance();
        
        if (d < 30)
        {
            P2OUT = 0b00101000; // forward
        }
        else
        {
            P2OUT = 0b00010000; // reverse
        }
        
        __delay_cycles(100000);
    }
}

int distance()
{
    // Send 20us trigger pulse
    P1OUT |= BIT3;
    __delay_cycles(20);
    P1OUT &= ~BIT3;
    
    // Wait for start of echo pulse
    while((P1IN & BIT2) == 0);
    
    // Measure how long the pulse is
    int d = 0;
    while((P1IN & BIT2) > 0)
    {
        // The following delay was worked out by trial and error
        // so that d counts up in steps corresponding to 1cm
        __delay_cycles(30);
        d = d + 1;
        if (d >= 400) break;
    }
    
    return d;
}
Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

3 Responses to Rangefinder example circuit and program from today’s lecture

  1. Pingback: RoboSumo: Day Eight | cathalfitzie58

  2. Pingback: RoboSumo: Rangefinder | c15394761-RoboSumo Project

  3. Pingback: RoboSumo: Preparation for Robosumo tournament (The Code, Part 1 – weeks 9, 10 & 11) | c15394761-RoboSumo Project

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 )

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