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.

Attachments

Be the First to Share

    Recommendations

    • Make it Glow Contest

      Make it Glow Contest
    • First Time Author Contest

      First Time Author Contest
    • PCB Challenge

      PCB Challenge

    22 Discussions

    0
    skarunasagar3
    skarunasagar3

    1 year ago

    Can you pl post a video , how to make it.

    0
    luizhenriquemarinhofga
    luizhenriquemarinhofga

    Question 1 year ago

    when i use the sensor outdoor it bugs and the led stays on, do you know how i fix it? i guess it's related with no answer of echo's pin because it work perfectly indoor(in my room)

    1
    vinutakulkarni
    vinutakulkarni

    Question 1 year ago on Introduction

    Sir, we have followed all your instructions, but we have used msp430g2553. We are getting distance as shown in the attachment below. Should we change any other variables or some settings?
    Kindly help us with same, we have been trying it out since a month.

    ultra.PNG
    0
    hadrian99
    hadrian99

    2 years ago

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


    ____________________________________________________
    https://showbox.software/
    https://tutuapp.win/
    https://showbox.software/

    0
    darshanabhyratae
    darshanabhyratae

    Question 2 years ago on Step 1

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

    0
    JaazielR
    JaazielR

    3 years ago

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

    0
    DanielA428
    DanielA428

    4 years ago

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

    0
    ThiagoL25
    ThiagoL25

    4 years ago

    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

    }

    else

    {

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

    }

    P1IFG &= ~0x04;//clear flag

    }

    }

    0
    GürkanÖ3
    GürkanÖ3

    Reply 4 years ago

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

    gurkan90@hotmail.de

    0
    lozza1950
    lozza1950

    5 years ago on Introduction

    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

    0
    KristjanB
    KristjanB

    Reply 5 years ago on Introduction

    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

    0
    KristjanB
    KristjanB

    Reply 5 years ago on Introduction

    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.

    0
    Prashanth535
    Prashanth535

    Reply 5 years ago on Introduction

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

    0
    KristjanB
    KristjanB

    Reply 5 years ago on Introduction

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

    0
    Prashanth535
    Prashanth535

    Reply 5 years ago on Introduction

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

    images.jpg
    0
    KristjanB
    KristjanB

    Reply 5 years ago on Introduction

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

    0
    Prashanth535
    Prashanth535

    Reply 5 years ago on Introduction

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

    0
    KristjanB
    KristjanB

    Reply 5 years ago on Introduction

    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.

    0
    UmangC2
    UmangC2

    5 years ago on Step 2

    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?