Introduction: Ultrasonic Sensor With MSP430 and IAR/CCS

Picture of Ultrasonic Sensor With MSP430 and IAR/CCS

Hello. In this tutorial I will show you step by step on how to use ultrasonic sensor with TI MSP430. I'm using MSP430G2452, however, a G2553 or any other chip wil also work, but some numbers could be different. Ofcourse I assume that you already know how to use IAR or CCS.

But before I start, you should know that I'm new in this kind of programming. So if there will be any mistakes, please do feel free to correct me. I've tried to comment the program as much as I could, so I hope you'll understand it.

So, what you'll need is:

  1. MSP430
  2. ultrasonic sensor (I have SRF05, and I'm using one pin for TRIG and the other for ECHO. There is also option where you connect MODE pin to GND and use only one pin, but I won't do that one)

What you'll have to do is generate a pulse whose lenght depends from sensor to sensor. On SRF05 is 10us. When sensor recieves the pulse, it will generate a 8 cycles of sonic burst independed from microcontroller, and sent ECHO pulse back to microcontroller, and we must measure that pulse. (It's hard for me to describe whole program in words, so just take time to understand the code). To get cm from result, we simply divide it by 58. (for any further information regarding sensor, you should google for documentation).

Step 1: Connecting

There isn't much to tell here. You can connect TRIG and ECHO pin wherever you like. Just make sure that you change values in the code. Also, don't connect to P1.0, because we need this pin to turn LED on/off.

I connected TRIG pin to P1.1 and ECHO to P1.2.

Step 2: Code

Picture of Code

So, here we are. The difficult part. It took me about two weeks of trying to get it working, since there isn't any info. about this kind of projects on internet. So to spare nerves of some poor begginer I decided to make this instructable. I would sure need it. :)

Thats it. If you have any questions, comment or mail me, and I'll reply soon as I can.

P.S, almost every question is answered in users guide from TI. Download it and if you are not sure what for example

P1IFG means, just ctrl+f, copy it in the search bar and search all the 600 pages.

UPDATE: as it turned out, there is a problem with blinking led. The sensor is probably sending corrupted data.. After little research I found out that sensor is for 5V, and not 3.3V, so maybe that's the problem. As soon as I find out I'll let you know.

UPDATE1.0 : It's a bit more complicated then I had at first thought. problem is, infact, that sensor works on 5V. So if you want it to work better, you must connect 5V supply on Vcc and GND of sensor. also, you must connect GND from MSP430 to GND of sensor and GND power supply. And then there are TRIGG and ECHO pin. As far as I know, if there is a rise from 3.3V (MSP430) to 5V on sensor (TRIGG pin), there is no problem regarding singal. Problem is with ECHO pin, because sensor outputs 5V signal to 3.3V of MSP430. so you have to use a voltage divider on ECHO wire. I'm adding a schematics that I drew in 20seconds, but it sould do the trick.


DanielA428 (author)2016-11-22

Hi, can I change the echo and trigger pins? I need the 1.1 and 1.2 for UART

ThiagoL25 (author)2016-06-02

I don't understand what this interrupt do, can you explain to me ? sorry, for my bad english.

#pragma vector=PORT1_VECTOR

__interrupt void Port_1(void)


if(P1IFG&0x04) //is there interrupt pending?


if(!(P1IES&0x04)) // is this the rising edge?


TACTL|=TACLR; // clears timer A

miliseconds = 0;

P1IES |= 0x04; //falling edge




sensor = (long)miliseconds*1000 + (long)TAR;//calculating ECHO lenght


P1IFG &= ~0x04;//clear flag



GürkanÖ3 (author)ThiagoL252016-06-24

hi ThiagoL25 can you send me the pdf please from this project

lozza1950 (author)2015-04-09

How would or could a sensor be connected to a stepper motor , to maintain a programmed constant height of let's say a point at the tip of a Z axis as the Z axis travels along the X axis of a CNC

KristjanB (author)lozza19502015-04-10

yes, I think it could be done. but i think that if you have stepper motor you can know its position without ultrasonic sensor. i dont have much experience in this area, sorry

Prashanth535 (author)2015-03-08

form where can i observe the output.....?

KristjanB (author)Prashanth5352015-03-08

for now you can change distance for LED light. I'm working on communication between msp430 and computer, but it might take some time. when I complete it, I will let you know.

Prashanth535 (author)KristjanB2015-03-09

we are considering the distance in cm so how can we get this as output like 17cm ,18cm....?

KristjanB (author)Prashanth5352015-03-09

im sorry I don't understand question. you mean how to change distance on which LED turns on?

Prashanth535 (author)KristjanB2015-03-09

what i mean that where will we see distance or we have to use lcd display for that...? like as shown below fig.

KristjanB (author)Prashanth5352015-03-10

oh that. yes, you can either use LCD screen, or use a terminal on computer. unfortunatly programming MSP is not like Arduino, where there is only SerialWrite.. :)

Prashanth535 (author)KristjanB2015-03-10

thanks for ur kind response and which pin of MSP430 would i connect the LCD display...............?

KristjanB (author)Prashanth5352015-03-10

no problem. I'm sorry I don't know that. perhaps you should google for sample code for using LCD screen with MSP430. and probably it's easier to establist UART connection (i think its UART, maybe its not) and print values on computer terminal.

UmangC2 (author)2015-02-26

Hi Thank you for this tutorial

i am unable to understand this part of code:

sensor = (long)miliseconds*1000 + (long)TAR;

can you please explain me?

KristjanB (author)UmangC22015-02-26

Hey. ok. so the

CCR0 = 1000; means that timer counts up to 1000 at 1mhz. so 1000 means 1milisecond, and 1 out of 1000 means 1 microsecond. okey, so everytime timer counts to 1000 it triggers an interrupt. In interrupt, we add 1 to int miliseconds. And as you can see, there is also reset commant (miliseconds = 0;)in


So, when we come into interrupt and signal has rising edge, miliseconds are put to 0. then program leaves the interrupt and waits for the falling edge. meantime, miliseconds are still running. so when we come again in interrupt, we have falling edge and go to ELSE function. there we use miliseconds as time between edges. and TAR register that holds the value of timer. Oh, and about *1000, i dont know why.. I did that trough experiment hehe. Thats the best I can explain. I hope you understand.

The code seems to have a minor bug, but I can't find it.. so if you spot something wrong, please tell :)

KristjanB (author)KristjanB2015-02-26

oh and longs are just to be safe. you dont realy need them as far as i know

About This Instructable




More by KristjanB:Powerful handheld fanUltrasonic Sensor with MSP430 and IAR/CCS
Add instructable to: