Ultrasonic Sensor With MSP430 and IAR/CCS





Introduction: 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

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.



  • Epilog Challenge 9

    Epilog Challenge 9
  • Sew Warm Contest 2018

    Sew Warm Contest 2018
  • Paper Contest 2018

    Paper Contest 2018

We have a be nice policy.
Please be positive and constructive.




Hi, I'm using the MSP430FR4133 but it does not let me run the code. This Microcontroller is compat with you code

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

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



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


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

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

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

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.

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

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