I finally got around to getting the HC-SR04 ultrasonic sensor to work with the TI Launchpad MSP430.
One of the challenges to using the HC-SR04 with the TI Launchpad is that the HC-SR04 is a 5 volt device. The MSP430 is a 3.3 volt device. So, you will need to source 5VDC from somewhere other than the VCC pin of the MSP430.
Yesterday I found a cool hack here, where the blogger solders jumpers to the test points near the USB connector on the Launchpad. He was using the 5VDC to power a PIR sensor. Good hack!
I also found some simple code and a good write-up for how the HC-SR04 works here. It's the first time I've seen someone write the code out instead of just using the Ultrasonic.h library. I made some modifications to his code - to output to speaker instead of serial monitor, and also to use inches instead of centimeters.
So, my circuit is pretty simple. I have pins 12 and 13 of the Launchpad hooked up to Trig and Echo on the HC-SR04. VCC and GND on the HC-SR04 are connected to the hacked test points on the Launchpad. Make sure you identify which is ground and which is hot before you hook it up. The first time I hooked these up, I got the polarity reversed somehow. When I touched the HC-SR04, it scalded my hand it was so hot! I fixed the polarity and it cooled down and worked fine.
I also have pin 10 of the Launchpad connected to a half watt, 8 ohm speaker, the other lead from the speaker connected to ground.
Here is the code I used for this project. I used the Energia IDE to edit and upload the code to my Launchpad. The Launchpad has an MSP430G2553 chip in it.
/*
HC-SR04 Ping distance sensor]
VCC to arduino 5v GND to arduino GND
Echo to Arduino pin 13 Trig to Arduino pin 12
More info at: http://goo.gl/kJ8Gl
*/
#define trigPin 12
#define echoPin 13
void setup() {
// Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
int duration;
// int freq;
float distance;
int conversion;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 74.07;-
if (distance >= 20 || distance <= 1){
// Serial.println("Out of range");
noTone(10);
}
else {
// freq = map(conversion,2,12,500,1200);
tone(10,distance*40);
// Serial.print(distance);
// Serial.println(" in");
}
delay(10);
}
The code gets the distance from the sensor, then sends a TONE out on pin 10 at a frequency that is ten times the distance reading in inches. The closer your hand gets to the sensor, the lower pitched the tone is. The farther away your hand gets, the higher pitched the tone is.
This was the first time I was able to successfully get the HC-SR04 to work with the Launchpad. I found it to be very accurate out to 45 inches or so. I tested it with a yardstick and it performed flawlessly. I have seen specs that say the HC-SR04 can measure all the way out to 450cm, but I haven't seen it measure anywhere close to that far yet.
One of the challenges to using the HC-SR04 with the TI Launchpad is that the HC-SR04 is a 5 volt device. The MSP430 is a 3.3 volt device. So, you will need to source 5VDC from somewhere other than the VCC pin of the MSP430.
Yesterday I found a cool hack here, where the blogger solders jumpers to the test points near the USB connector on the Launchpad. He was using the 5VDC to power a PIR sensor. Good hack!
I also found some simple code and a good write-up for how the HC-SR04 works here. It's the first time I've seen someone write the code out instead of just using the Ultrasonic.h library. I made some modifications to his code - to output to speaker instead of serial monitor, and also to use inches instead of centimeters.
So, my circuit is pretty simple. I have pins 12 and 13 of the Launchpad hooked up to Trig and Echo on the HC-SR04. VCC and GND on the HC-SR04 are connected to the hacked test points on the Launchpad. Make sure you identify which is ground and which is hot before you hook it up. The first time I hooked these up, I got the polarity reversed somehow. When I touched the HC-SR04, it scalded my hand it was so hot! I fixed the polarity and it cooled down and worked fine.
I also have pin 10 of the Launchpad connected to a half watt, 8 ohm speaker, the other lead from the speaker connected to ground.
Here is the code I used for this project. I used the Energia IDE to edit and upload the code to my Launchpad. The Launchpad has an MSP430G2553 chip in it.
/*
HC-SR04 Ping distance sensor]
VCC to arduino 5v GND to arduino GND
Echo to Arduino pin 13 Trig to Arduino pin 12
More info at: http://goo.gl/kJ8Gl
*/
#define trigPin 12
#define echoPin 13
void setup() {
// Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
int duration;
// int freq;
float distance;
int conversion;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 74.07;-
if (distance >= 20 || distance <= 1){
// Serial.println("Out of range");
noTone(10);
}
else {
// freq = map(conversion,2,12,500,1200);
tone(10,distance*40);
// Serial.print(distance);
// Serial.println(" in");
}
delay(10);
}
The code gets the distance from the sensor, then sends a TONE out on pin 10 at a frequency that is ten times the distance reading in inches. The closer your hand gets to the sensor, the lower pitched the tone is. The farther away your hand gets, the higher pitched the tone is.
This was the first time I was able to successfully get the HC-SR04 to work with the Launchpad. I found it to be very accurate out to 45 inches or so. I tested it with a yardstick and it performed flawlessly. I have seen specs that say the HC-SR04 can measure all the way out to 450cm, but I haven't seen it measure anywhere close to that far yet.