Launchpad vs Leonardo – Ultrasonic Sensor

The next challenge is proximity detector using the ultrasonic sensor.

In this challenge, the micros need to capture the PWM signal transmitted by the ultrasonic sensor. The distance of the nearby object is translated on to four LEDs. The micros also need to buzz if the object is too near.

The Ultrasonic sensor is HC-SR04 module. I could not remember where I got it from. But, you can easily buy them online. Dealextreme also sells them.

For the Arduino, I used the Ultrasonic Ranging Module library published by ITead Studio.

For the Launchpad, I wrote my own code utilising the capture mode of Timer A0.

MSP430 Launchpad Code

//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the version 3 GNU General Public License as
//  published by the Free Software Foundation.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
//  R. Voon 2012

#include <msp430g2231.h>

#define P1_ECHO      BIT1
#define P1_TRIGGER   BIT0
#define P1_LED0	     BIT2
#define P1_LED1	     BIT3
#define P1_LED2	     BIT4
#define P1_LED3	     BIT5
#define P1_BUZZER    BIT6

//modify thresholds below to suit your need
#define TH0 500
#define TH1 400
#define TH2 300
#define TH3 200
#define TH4 100
#define TH5 10

unsigned int measure = 0;
unsigned int measure_1 = 0;

void main()
{
	long i;
	WDTCTL = WDTPW + WDTHOLD;
	P1DIR = (P1_TRIGGER + P1_LED0 + P1_LED1 + P1_LED2 + P1_LED3 + P1_BUZZER);
	P1SEL = BIT1;
	P1OUT = 0x00;
	BCSCTL1 |= CALBC1_1MHZ;
	DCOCTL |= CALDCO_1MHZ;
	BCSCTL2 &= ~SELS;
	BCSCTL2 |= DIVS0;
	TACCTL0 |= CM_3 + SCS + CCIS_0 + CAP + CCIE;
	TACTL |= TASSEL_2 + MC_2 + ID_0;
	_BIS_SR(GIE);   // general interrupt enable

	while (1)
	{
		P1OUT |= P1_TRIGGER;
		for (i = 0; i < 3; i++);
		P1OUT &= ~P1_TRIGGER;
		for (i = 0; i < 8000; i++);
    }
}

//***timerA interrupt routine
#pragma vector=TIMERA0_VECTOR
__interrupt void TimerA0(void)
{
    long i;
    if((P1IN & P1_ECHO) > 0
    {
		measure_1 = TACCR0;
		TACCTL0 |= CM_3;
    }
	else
	{
		measure = TACCR0 - measure_1;
		TACCR0 = 0;

		P1OUT &= ~(P1_LED0 + P1_LED1 + P1_LED2 + P1_LED3 + P1_BUZZER);
		if (measure > TH0)
		{
		}
		else if (measure > TH1)
		{
			P1OUT |= P1_LED3;
		}
		else if (measure > TH2)
		{
			P1OUT |= P1_LED3 + P1_LED2;
		}
		else if(measure > TH3)
		{
			P1OUT |= P1_LED3 + P1_LED2 + P1_LED1;
		}
		else if(measure > TH4)
		{
			P1OUT |= P1_LED3 + P1_LED2 + P1_LED1 + P1_LED0;
		}
		else if(measure > TH5)
		{
			P1OUT |= P1_LED3 + P1_LED2 + P1_LED1 + P1_LED0 + P1_BUZZER;
		}
		for (i = 0; i < 8000; i++);
	}
	TACTL &= ~TAIFG;
}

Arduino Leornardo Code

#include <Ultrasonic.h>

//modify thresholds below to suit your need
int TH0 = 12;
int TH1 = 9;
int TH2 = 7;
int TH3 = 5;
int TH4 = 3;
int TH5 = 1;

int ledPins[] = {5,4,3,2};
int buzzer = 6;

Ultrasonic ultrasonic(12,13);
long measure;

void setup() {
  Serial.begin(57600);  // for debugging
  for (int thisLed = 0; thisLed < 4; thisLed++) {
    pinMode(ledPins[thisLed], OUTPUT);
  }
  pinMode(buzzer, OUTPUT);
}

void loop()
{
  measure = ultrasonic.Ranging(CM);
  Serial.println(measure);
  for (int thisLed = 0; thisLed < 4; thisLed++)
  {
     digitalWrite(ledPins[thisLed], LOW);
  }

  digitalWrite(buzzer, LOW);

  if (measure > TH0)
  {
  }
  else if (measure > TH1)
  {
    digitalWrite(ledPins[3], HIGH);
  }
  else if (measure > TH2)
  {
    digitalWrite(ledPins[3], HIGH);
    digitalWrite(ledPins[2], HIGH);
  }
  else if(measure > TH3)
  {
    digitalWrite(ledPins[3], HIGH);
    digitalWrite(ledPins[2], HIGH);
    digitalWrite(ledPins[1], HIGH);
  }
  else if(measure > TH4)
  {
    digitalWrite(ledPins[3], HIGH);
    digitalWrite(ledPins[2], HIGH);
    digitalWrite(ledPins[1], HIGH);
    digitalWrite(ledPins[0], HIGH);
  }
  else if(measure > TH5)
  {
    digitalWrite(ledPins[3], HIGH);
    digitalWrite(ledPins[2], HIGH);
    digitalWrite(ledPins[1], HIGH);
    digitalWrite(ledPins[0], HIGH);
    digitalWrite(buzzer, HIGH);
  }

  delay(100);
}

This entry was posted in Arduino, MSP430, My Projects, ultrasonic and tagged , , , , , , , . Bookmark the permalink.

5 Responses to Launchpad vs Leonardo – Ultrasonic Sensor

  1. Junnel Camingawan says:

    The code for msp430 have errors. Thanks

  2. Bahadır Coşkuner says:

    First of all thank you for your application and open source code. It works so good when ı use pin configuration as you do. I use newer version of MSP430 with 2553 chip on it. But when ı try to use another pins except P1.0 and P1.1 i.e. P2.0 and P2.1 it doesnt work as it has to be. I mean nor leds neither buzzer doesnt work. I wonder if P1.0 and P1.1 has special meaning. How can use other pins? What is your suggestions about this…
    Thank you again for your demonstration.

  3. MAHMUT says:

    sorry could you send a photo about this circuit because I have some errors and my program does not work.

  4. Rando says:

    Does error still persist here? Will not compile IAR…

    //***timerA interrupt routine
    #pragma vector=TIMERA0_VECTOR
    __interrupt void TimerA0(void)
    {
    long i;
    if((P1IN & P1_ECHO) > 0
    {
    measure_1 = TACCR0;
    TACCTL0 |= CM_3;
    }
    else
    {
    measure = TACCR0 – measure_1;
    TACCR0 = 0;

    P1OUT &= ~(P1_LED0 + P1_LED1 + P1_LED2 + P1_LED3 + P1_BUZZER);
    if (measure > TH0)
    {
    }
    else if (measure > TH1)
    {
    P1OUT |= P1_LED3;
    }
    else if (measure > TH2)
    {
    P1OUT |= P1_LED3 + P1_LED2;
    }
    else if(measure > TH3)
    {
    P1OUT |= P1_LED3 + P1_LED2 + P1_LED1;
    }
    else if(measure > TH4)
    {
    P1OUT |= P1_LED3 + P1_LED2 + P1_LED1 + P1_LED0;
    }
    else if(measure > TH5)
    {
    P1OUT |= P1_LED3 + P1_LED2 + P1_LED1 + P1_LED0 + P1_BUZZER;
    }
    for (i = 0; i < 8000; i++);
    }
    TACTL &= ~TAIFG;
    }

Leave a Reply

Your email address will not be published. Required fields are marked *