meanpc 3 Posted August 15, 2012 Share Posted August 15, 2012 I'm very appreciative of the work you guys have done on Energia. Has anyone done anything with the HC-SR04 sensor using Energia? Quote Link to post Share on other sites
maxik 1 Posted August 15, 2012 Share Posted August 15, 2012 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" Quote Link to post Share on other sites
chibiace 46 Posted August 15, 2012 Share Posted August 15, 2012 pulsein? Quote Link to post Share on other sites
cubeberg 540 Posted August 15, 2012 Share Posted August 15, 2012 Make sure you watch out - it's a 5v device and the LP isn't 5v tolerant. I used a simple voltage divider to get one working on an LP-based RC car. You can find more info on the voltage divider here and my code is here(although it's in C). Might be portable to Energia. Quote Link to post Share on other sites
chibiace 46 Posted August 17, 2012 Share Posted August 17, 2012 http://trollmaker.com/article3/arduino- ... ic-sensor/ Quote Link to post Share on other sites
profmason 0 Posted September 27, 2012 Share Posted September 27, 2012 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. Quote Link to post Share on other sites
meanpc 3 Posted September 27, 2012 Author Share Posted September 27, 2012 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. Quote Link to post Share on other sites
cde 334 Posted September 28, 2012 Share Posted September 28, 2012 While always good practice when using 5v parts (especially this sensor with it's lack of real datasheets), everything I'm finding online show that the echo pin output is about 2v? A scope would confirm. Quote Link to post Share on other sites
meanpc 3 Posted September 28, 2012 Author Share Posted September 28, 2012 I don't have a scope. I'm assuming the pulses are much to quick for me to capture accurately with my $30 multimeter. Would be nice if someone with an o-scope could check it out. Quote Link to post Share on other sites
cubeberg 540 Posted September 28, 2012 Share Posted September 28, 2012 I had two running on my LP for my RC Car project - if you run them quick enough, you'll get a clean 5v with a multimeter. You definitely need a voltage divider or you'll damage your LP. Quote Link to post Share on other sites
Rickta59 589 Posted September 28, 2012 Share Posted September 28, 2012 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 Quote Link to post Share on other sites
cubeberg 540 Posted September 28, 2012 Share Posted September 28, 2012 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. Quote Link to post Share on other sites
saip 0 Posted October 28, 2014 Share Posted October 28, 2014 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); } Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.