Upcoming Events

Jason Kelly Johnson will be running a one-day intensive Firefly/Arduino Workshop "RESPONSIVE BUILDING FACADES" on July 27, 2012 in San Francisco (as a part of the Architect's Newspaper "Art and Science of Building Facades" Symposium.) Register here.

*Read Jason's Hardware Recommendations blog post for 2012-13 Firefly Workshops.  

Are you teaching or participating in a workshop that is using Firefly?  Let us know about it and we'll feature it here on the Firefly website! (info@fireflyexperiments.com)

Discussions > Ping sensor

Hello all, I am new to Firefly and have a question in regards to a ping ultrasonic sensor: http://www.parallax.com/Store/Sensors/ObjectDetection/tabid/176/CategoryID/51/List/0/SortField/0/Level/a/ProductID/92/Default.aspx
it uses a pulse in/pulse out communication which i have working through Arduino with the following sketch from this tutorial:

Because the Arduino board sends a short pulse to trigger the detection, then listens for a pulse on the same pin using the pulseIn() function, im not sure how this can interface with Firefly. The duration of this second pulse is equal to the time taken by the ultrasound to travel to the object and back to the sensor then using the speed of sound this time is converted to distance.

I wanted to see if anyone knows how to input this information via FireFly or if there is another way to receive data from a Ping sensor into grasshopper....

January 31, 2011 | Unregistered CommenterCharles

Hi Charles,
I have to say that I haven't used this exact sonic sensor, but I think you should have no problem having this interface with Firefly. The easiest way would be to use the Generic Serial Read component. Basically, the Generic Serial Read component works very similarly to the Serial Monitor in the Arduino IDE. In the tutorial, I saw that you're sending some serial print lines:

Serial.print("in, ");

If you load up this code on the Arduino, and then open the Serial Monitor... you should see a sensor value in inches and another in centimeters (all on the same line). In Firefly, you would first need to open the serial port (the same way you would normally using the Open/Close Port component). Then, instead of using the Uno Read component, you would just use the Generic Serial Read. Simply, designate the port number, and then set the Start input to True and you should see the same data that was just printed to the serial port. Toggle the Timer and you should see this begin changing (just as you would as if you were using the Serial Monitor).

Of course, you could also try to use the Uno Read component, but you would need to modify the Firefly Firmata with your tutorial code... this will likely take a decent amount of time, so I would probably just stick to the Generic Serial Read component. Let me know if this works for you or if it doesn't make sense.

Good luck,

January 31, 2011 | Registered CommenterEditor

Thanks Andy, the serial monitor did the trick. Its a nice feature allowing the numeric conversions and ping rate to stay in arduino.
Thanks again,

February 1, 2011 | Unregistered CommenterCharles

So you were able to get the data to stream into Grasshopper? I haven't tested your code, but have you found any bugs with the data coming into GH? The reason I ask is that the speed at which the data is printed over the Serial Port is important. If you're loop runs too fast, then you'll be printing to the serial port so fast that the buffer which retrieves the data in GH can be flooded with data and sometime act sluggish or give erratic results. Normally this can be fixed by making sure your loop isn't running so fast that the print lines are occurring more than every 25ms or so. Anyway, I was just curious if everything on the Firefly side was working properly.

February 1, 2011 | Registered CommenterEditor

I have a question about the values coming in from my ping sensor using your code. I am trying to connect the value readings in grasshopper, but any connection from "Out" reads as an error. Is there a way to correct this?

February 21, 2011 | Unregistered CommenterRD

Hi RD... Are you using the code from the Arduino Ping tutorial? If so, then the reason you're probably getting errors is because of the way the data was formatted before being printed to the serial port. In that code, there are five print lines inside the loop() function:

Serial.print("in, ");

So, the first print command prints the value of the variable 'inches' to the line, and then the next command follows that with the word "in,". Then the value for the 'cm' variable is added to the line followed by the last word "cm". The last command (Serial.println) adds a carriage return and line feed to start a new line next time through the loop.
So, I'm imagining that you want just the distance information (either in inches or cm). You don't need all of those extra phrases ("in," or the "cm"). You just need the raw numeric data. The easiest solution would be to just comment out the lines that you don't want printed to the port. So, if you just wanted a single distance value in inches you would use this:


Notice, I used the Serial.println instead of Serial.print... You can certainly use Serial.print if you want, but Firefly uses the carriage return and line feed information (that is automatically included when you use the Serial.println function) to determine when to split the data coming in over the serial port. If you use the line above (and comment out all of the other lines) then you should see a single number coming out of the Generic Serial Read component. With a single numeric data, you can now connect it to any other GH component (that expects a number) with no problems.

One other thing you may want to look out for. I haven't actually tested your code, so I don't know how fast the loop() function is running. It's possible that if it's sending data too fast to the serial port, that the Firefly buffer (which reads the information coming over and stores it in an array before spitting it out) can get filled up. Depending on the speed of your loop() function you may get multiple values coming out of the Generic Serial Read. This is because in the time it took for Firefly to go and read the serial port for a value, the loop function had sent several values and they all got stored in the buffer. This isn't such a big deal, as I would just use a List Item component (Sets/Lists/List Item) to retrieve the (0) item (first time) from the list. This way, the data coming out of the list item will always be the first value sent over the port (regardless if more than one value is there). This is a little trick. The other option would be to slow down the timing of your loop() function but that could mess up the timing of other things in your code... so the list item works better in most cases. Let me know if this helps you.

February 21, 2011 | Registered CommenterEditor

Hey Andy and RD,
If you insert a delay(20); somewhere in the loop it comes in just fine. Also Andy is correct, make sure the data coming out of the serialPort is a single array of data printing once every line. I used the generic serial read component which worked great for me. Here is a video http://vimeo.com/20004747

February 21, 2011 | Unregistered CommenterCharles

Thanks for the help everyone. I used the advice posted, and I got the Grasshopper/Firefly model to correspond to the data. The issue I am having now is that I have a servo connected, and it is not corresponding to the Ping Sensor. I had a photoresistor before the ping sensor, and the servo responded to the data. It's probably a wiring issue, but I am not sure.

February 23, 2011 | Unregistered CommenterRD

Hi Andy

I'd like to use the UNO firmata with a ping sensor, so that I have control via firefly, but I am not sure how to add the arduino code for the ping sensor to the Uno Firmata. Any suggestions?

I am using the same sensor and code which Charles mentions in this first post.


January 11, 2012 | Unregistered CommenterFrederich

I have problem that i can use this Arduino file in this link http://www.instructables.com/id/Reading-Ultrasonic-Sensor-Data-to-F...

to measure the distance by ultra sonic sensor but now i cant move the servo motors in the same file so can anyone help me for arduino file can connected with firefly to measure and move the motors in the same time.

I am waiting your replay as soon as possible, thank you in advanced

October 11, 2015 | Unregistered CommenterAhmed Elshafei