Introduction: Reading Ultrasonic Sensor Data to FireFly
This Instructable will be looking at how to configure a Ultrasonic Sensor using Arduino and Firefly for the purpose of detecting an objects distance and either changing geometry within Rhino and eventually how to use the data for clash detection within an autonomous machine.
What you need:
- Rhino 5
- Grasshopper 3D Version 0.9.0075
- Firefly Version 22.214.171.124
- Firefly Firmata
- Arduino Uno
- USB A to B
- 3 Jumper wires
- Ultra Sonic Sensor (I used RadioShack® Ultrasonic Range Sensor but any will work)
Step 1: Loading the Firefly Firmata Onto Your Arduino Uno
- Loading the Firefly Firmata_ Ping onto the Arduino Uno
- Connect the Arduino Uno to your computer and launch the Arduino software
- Open the Firefly Firmata into the Arduino software and upload it to your Arduino Uno.
- Not what port your Arduino Uno is connected to by going to:
- Tools > Serial Port
- Close the Arduino Software
Step 2: Connecting the Ultra Sonic Sensor to You Arduino Uno
What you'll need:
- Arduino Uno
- Ultra Sonic Sensor
- Bread Board
- 3 Jumper Wires
- Plug your sensor into the Bread board so there are still open holes behind it
- Insert 3 different colored jumper wires behind the connecting pins of the Sensor in the Bread Board.
- Noting which cable corresponds to which sensor insert the correct wire into the Arduino Uno Board
- GND to GND
- VCC to the correct power (in my case 5V)
- SIG to a Digital Pin (I am using Digital Pin 2)
Step 3: Starting Your Sketch and Opening a Port
- Launch Rhino and Grasshopper
- Start your Firefly sketch by navigating to the Firefly tab in Grasshopper
- Opening the Port
- Click on the Port node and bring it into your sketch
- Create a Panel and connect it to the "Ports Available" node.
- This will let you see what ports are available on your machine, and is why you will need to know what port your Arduino Uno is connected to
- Create a "Value List" node and double click on it
- Enter the Value's item you see in your Panel
- In my case I will enter "0" and "1" and click "OK"
- select the port that your Arduino Uno is on by clicking on the down arrow
- Insert a "List Item" node
- Connect your Panel to the L input
- Connect the Value List to the i input
- Opening the Port
- Insert a "Open Port" node into the sketch
- Connect the "List Item" node to the "Port" input
- Insert a "Boolean Toggle" to the Sketch
- Connect it to the "Open" input
- Create a "Value List" and edit the values to include and connect it to the "Baud" input on the "Open Port" node
- 2400, 4800, 9600, 14400,19200, 28800, 38400, 57600 and 115200
- Select the 115200 value
- Connect a Panel to the "Msg" output of the "Open Port" node
- Double Click on the False Button in your Toggle node to turn it to true
- You should now see your panel coming from the open port reading
- "Hooray! The serial port is open and you may now begin reading or writing data to the serial port."
Step 4: Reading From the Arduino Board
Now that you have your Port opened the next step is to read the data that the Ultra Sonic Sensor is collecting.
- Insert a "Uno Read" and another "Boolean Toggle" node into the sketch
- Connect the newly added "Boolean Toggle" to "Start" input
- Connect the "List Item" that is calling out what port to the "Port" input
- I like to group my panels for the outputs I'm not using and leave the ones I am as individual
- Connect APin0 to APin5 to a Panel
- Connect DPin2 to a Panel
- Connect DPin4 and DPin7 to a Panel
- Connect Msg to a Pannel
- You will see that these have the value of <null> and the Msg will read "The serial port is open so you may begin reading data by setting the Start boolean value to True."
- Insert and Attach a "Timer" node to the "Uno Read" and set it to the time you want it to run commands
- Double Click the Toggle attached to "Uno Read"
- If you look in the DPin2 Panel as you move an object closer and further from the Scanner the values will change
- The Default unit is Centimeters, you can either modify this in the Firmata or do conversion within Grasshopper
Step 5: Converting to a Different Unit
If you don't want to use CM as your base unit you can convert the distance reading either in Firefly or the Firmata. I chose to use Firefly and use an add in called "TT Tollbox" but you can also do a multiplication function to accomplish this. The reason I am using the TT Toolbox plugin is because it lets you switch quickly between units.
- Navigate to the TT Toolbox tab and insert a unit converter node into the sketch
- Connect the output from the Panel attached to DPin2 to the "F" input
- Create a Value List node and edit the contents to:
- Create a duplicate and attach one to the "Target Unit" (TU) input and change it to the unit that you want to display, and the other to the "Source Unit" (SU) Input and change it to "cm".
- Attach a Panel to the "Value" output to see the converted number
We have a be nice policy.
Please be positive and constructive.