Firefly – Serial Read Ultrasonic Sensor
Learn how to read serial data in Grasshopper / Firefly from an unsupported sensor connected to an Arduino. In this example an ultrasonic distance sensor (ping sensor) is used, but the same process could be applied to any sensor that requires a library and isn’t supported by Firefly. The values received from the serial read module are first stripped using a text split module to extract only the distance value. This value is then remapped (remap numbers module) to a value suitable for controlling a servo motor which is connected to a second Arduino through Firefly. [Note: Change the video quality to 1080p if you are having difficulty making out details]
WIRING & GRASSHOPPER CODE
Ultrasonic sensor HC-SR04 – this sensor uses 2 data pins, a trigger pin to send a ‘ping’ and an echo pin to listen for it’s return reflection (note: these sensors can also work with one data pin by linking the trigger and echo pin and using different code). [Connect Vcc → 5V, Gnd → GND, Trig → D12, Echo → D11] Servo motor SG90 9g – servos have 3 wires, 2 for power and a single data pin. Note: the output pin on the Uno Write module needs to be set to ‘servo’. [Servo Red → 5V, Brown → GND, Orange → D9]
I enjoyed this tutorial and have a few questions:
1. how is the second Arduino being powered?
2. why does the distance sensor not require Firmata since it is working in Firefly? or maybe another way to ask this, if Firmata is used on the first Arduino would the program still run?
3. related to the second question, is there a way to put all of these on one Arduino?
thank you in advance for your time.
1.) The Arduino with the servo which is running the Firefly firmarta would be powered from the PC USB as it needs to remain in constant communication with Firefly / grasshopper (however the servo(s) can be powered externally if it draws to much power from the PC USB). The Arduino with ultrasonic sensor is also powered from the same PC on a second USB slot as it needs to send the distance data to Firefly/grasshopper over USB.
2.) The Firefly firmata basically makes the Arduino a dumb interface for Firefly to read inputs from and send signals to, but it has no independence and must always be connected to firefly to work. Firefly does not currently support ultrasonic sensors so instead we run it on an independent arduino. This arduino does not require Firefly to work and if you disconnect it from a PC and just supply power it will still be sending distance data (though we need to connect it to a PC to see that data).
3.) Yes and no. These can easily be run on one arduino if you don’t use Firefly/grasshopper and instead program it all using the Arduino IDE. Firefly does not currently support these types of ultrasonic sensors, but you can use analogue IR distance sensors (search for “arduino sharp IR sensor”) which should work just like any other analogue sensor in firefly. Ultrasonic and IR distance sensors each have their pros and cons when it comes to measuring in different scenarios and from different materials.
This is a basic example that can be applied to any sensor (or serial communication) that the Arduino can support but Firefly cannot and is a workaround to that limitation.