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.



    • Water Contest

      Water Contest
    • Tiny Home Contest

      Tiny Home Contest
    • Creative Misuse Contest

      Creative Misuse Contest

    19 Discussions

    I'm coming to you because I need your knowledgeable programming skills.

    I use an MSP430 (F5529) and I want to connect a temperature sensor. The problem is:

    I will use a port with a digital analog converter, available on the MSP but I can find no source of information about its configuration (Code Composer Studio code in C) nor the port to use (P1.0 or P1.1 or P1.2 or ...).

    I tried to find information, but my research did not succeed ...

    Looking forward to your help ..

    how to connect IR sensor to MSP430? How to write the code for IR sensor?

    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



    1 reply

    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

    1 reply

    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

    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.

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


    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.. :)

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

    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.

    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?

    2 replies

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