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