Jump to content
43oh

HC-SR04 ultrasonic sensor w/ Energia?


Recommended Posts

I don't have my LaunchPad yet, but I've used that sensor with Arduino. There is library for it and it's rather simple, so try it with Energia. My previous account got locked and instantly deleted after post without any links in it, so I don't want to risk and can't provide you an URL. Just google "HC-SR04 Arduino Library" :)

Link to post
Share on other sites
  • 1 month later...

To make this work quickly:

 

1. Load the examples->sensors->ping example

2. Change

const int pingPin = 7;

to

const int pingPin = P2_1;

const int pingReceive = P2_2;

3. Change

pinMode(pingPin, INPUT);

duration = pulseIn(pingPin, HIGH);

to

pinMode(pingReceive, INPUT);

duration = pulseIn(pingReceive, HIGH);

 

Connect

1. Vcc to Vcc, Gnd to Gnd, Trig to P2_1, Echo to P2_2

 

Program chip and open serial port to observe values. At 3.5 V It seems to track my hand between 2 and 25cm and a dinner plate to 60 cm. At 5V it tracks the hand to 55 cm and the plate to 2 meters(I didn't try further)

 

Note you are running the HC-SR04 on 3.5V when it is a 5V device. It still works, but the range is much shorter! You can tap into the 5V pin next the USB connector on you dev board, or I just have the MSP on breadboard running from a 5V supply. If you run the sensor at 5V, use a 4.7k series resistor on the echo pin since the MSP inputs are not 5V tolerant.

 

Here is the entire sketch of the modified PING sketch (credits to original authors etc)

const int pingPin = P2_1;
const int pingReceive = P2_2;

void setup() {
 // initialize serial communication:
 Serial.begin(9600);
}

void loop()
{
 // establish variables for duration of the ping, 
 // and the distance result in inches and centimeters:
 long duration, inches, cm;

 // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
 // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
 pinMode(pingPin, OUTPUT);
 digitalWrite(pingPin, LOW);
 delayMicroseconds(2);
 digitalWrite(pingPin, HIGH);
 delayMicroseconds(5);
 digitalWrite(pingPin, LOW);

 // The same pin is used to read the signal from the PING))): a HIGH
 // pulse whose duration is the time (in microseconds) from the sending
 // of the ping to the reception of its echo off of an object.
 pinMode(pingReceive, INPUT);
 duration = pulseIn(pingReceive, HIGH);

 // convert the time into a distance
 inches = microsecondsToInches(duration);
 cm = microsecondsToCentimeters(duration);

 Serial.print(inches);
 Serial.print("in, ");
 Serial.print(cm);
 Serial.print("cm");
 Serial.println();

 delay(100);
}

long microsecondsToInches(long microseconds)
{
 // According to Parallax's datasheet for the PING))), there are
 // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
 // second).  This gives the distance travelled by the ping, outbound
 // and return, so we divide by 2 to get the distance of the obstacle.
 return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
 // The speed of sound is 340 m/s or 29 microseconds per centimeter.
 // The ping travels out and back, so to find the distance of the
 // object we take half of the distance travelled.
 return microseconds / 29 / 2;
}

 

I found a writeup that I can't link to after I wrote this.

Link to post
Share on other sites
I should have updated this post, but I was successful with getting the HC-SR04 to work with the MSP430/Energia a few weeks ago.

 

http://www.meanpc.com/2012/09/using-hc- ... th-ti.html

 

I did use the 5V pins near the USB connector, but I didn't even think about the voltage on the echo pin - good catch.

 

I watched your video and your comment that Energia ( which really means msp430-gcc ) is not efficient is misplaced. In your code, you use floating point, multiplication and division. The msp430g2553 doesn't have hardware floating point nor any multiple or divide instructions. If you use floating point with division and multiplication, this forces msp430-gcc to include software implementations of the floating point math to be linked with your code to accomplish those tasks. This does cause larger binaries. I'm not sure CCS would do any better, it would also have to implement the floating point routines in software.

 

The key to small code on the msp430, you need to avoid floating point math. You also want to avoid multiple, divide and modulo instructions for the most efficient code. There are many posts here on 43oh that illustrate how to substitute integer math for floating point math. Other posts show how to use the shift operator with power of two operands to perform fast multiple and divide.

 

The msp430g2553 is a great chip with great peripherals, it isn't the goto chip if you want to use floating operations. There are different msp430 chips that do include support hardware multiply and divide. The msp430g2553 isn't one of them.

 

-rick

Link to post
Share on other sites

FYI - I was able to determine response from the HC-SR04 without floating point math. I took the number of Microseconds the pin was high and divided by 58 to get the number of CM (just doing some of the math ahead of time). You could pre-multiply the MS by 10 before dividing to get higher precision, but the sensor only has a .3 CM accuracy.

 

Rickta59 - i'm sure CCS would not do any better - the same libraries would have to be included. Compiler switches might help, but I'm sure Energia has the same capabilities.

 

Maybe it's worth a warning in Energia when you use something that requires floating point math or the extra libraries? Similar to the way CCS5 now warns about energy effeciencies and un-initialized interrupts.

Link to post
Share on other sites
  • 2 years later...

i tried this but it is not showing the exact cm, can any one help

 

 

int trigPin = P1_6;
int echoPin = P1_7;
 
long duration;
  long distance;
void setup() 
{
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT); 
 
}
 
void loop() {
  
  digitalWrite(trigPin, LOW);  // Added this line
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/58.2);
 
    Serial.println(distance);
  
  delay(1000);
}
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...