Robofish: create your robot fish with Arduino

By on February 26, 2013
Pin It


Today we introduce you a robot fish that is able to swim and recognize tank edges and obstacles. We’ll find out how we can easily create it with common insulating material and a few servo motor controlled by Arduino.


The mechanical part

For the fish body we used common polystyrene as the one used as insulating material for walls. Inexpensive, very robust and lightweight: it floats easily and is easily moldable. Great deal.


To make fish’s swimming more realistic, we need three joints between the trunk and the caudal fin. As actuators we have chosen of common modeling servos: small, powerful enough and easily controlled by a microcontroller.
The servos are ideal because with them you can manage the movement of a small shaft connecting all segments of the fish, varying at will the position, even a few degrees.
The fish body is divided into a central part and three parts, each of which moved by a servo. The caudal fin is made with plastic recycled from supermarket goods packages.


The flexibility of the fin allows you to give more realism to movement. To obtain harmonic motion, each part is attached to the next by a servo: the body of the servo should be glued on a body segment while the servo arm (connected to the shaft) shall be glued on the next.


Obviously Robofish needs a system to detect obstacles such as the tank edges. To create this, we used two Sharp GP2Y0D805PCB infrared sensors with digital output. These components can detect objects up to 5 cm and are quite easy to manage. Once encased in a plastic bag, can operate in water.


Position the sensors on the front, tilted to 45°, one on left and one on the right. Then carve out the room for control, batteries and sensors.
The profile of the fish is at your choice. Don’t worry too about weight as in our prototype we added 460g of further weight to ensure right level of floating.


Electrical connections


As control we opted for an Arduino-compatible Board produced by Seeedstudio but any Arduino board will do. Four simple AA batteries will provide a voltage compatible with the servo feed, while the Arduino board derives its tension thanks to its internal 5 volt regulator.
As it is difficult to use a conventional switch (you can’t handle it since everything is covered in plastic) we used a small magnetic contact, such as those used to detect the windows opening. It consists of a Reed contact activated by a small magnet. Place the contact on the outer edge of the robot so by approaching the magnet, you can activate the circuit. This explains that little bump on the head of the robots seen in the video.
Of course, the robot fish will work until the magnet is on his head. Clever and cheap.


The firmware

Using Arduino it’s easier as cabling is easy and immediate and the realization of the sketch is deadly simple.
Servo movement is repetitive and follows a precise scheme. We provided the ability to personalize few variables so that you can test different kinds of swimming. The swim takes place by moving the three servos in synchronous, according to a pattern that resembles the letter s: each segment varies slightly out of phase with respect to the previous.
For the servo movement we used servo.h library, already available on the Arduino IDE.  You simply declare the servo and the multi-touch corresponding output using the servo. attach (pin). The servo.detach() method allows to release the output from the servo to use it eventually as PWM output. Commands managing servos movement are two.
The first, ServoWriteMicroseconds, allows you to set the position of the servo command by specifying the length in microseconds. The value in this case should be between 1,000 and 2,000 µs. The midpoint of 1500 corresponds to the neutral position respect the shaft line. The second statement, Write, allows positioning by specifying the angle in degrees: the value of the angle must be between 0 and 180°, 90° intermediate value corresponding to the neutral position of the servo.

The two sens_SX and sens_DX named sensors are wired respectively on pins 5 and 6. In the main loop there’s is a for cycle that increments the i variable at regular intervals from 0 to 360 cyclically, in a total time defined by the variable velocity.
The value of the i variable together with that of phase, determines the position of the servo motors. This value is referred to a full 360° loop and then must be offset, keeping in mind the neutral position of the servo, of the potential shift and that of the maximum deflection you want to give to the movement.
The latter is defined by the variable maxDeflexion (decimal degrees) and defines the maximum deflection of each stroke: the higher this value, the wider will be the movement of the three trunks of the fish.


With phase set to 0 mail all the servo will move along; increasing this value introduces a phase shift between the servo position. As a result you will get a movement that is similar to the letter S, the more pronounced the greater the value of phase is.


When detecting obstacles, the neutral position of the servo moves gradually on the opposite side of the obstacle. In this way, the fish will describe an arc of circle away from the obstacle.
The arc can be controlled through maxDefobs variable: the higher this value, the more will be the swim line curvature. When the obstacle is no longer being detected (time controlled with lostTime), the servo motion resumes to go regular. Each servo PIN range between-maxDeflexion and + maxDeflexion, while if an obstacle is detected it ranges between the values of -maxDeflexion + maxDefobs and maxDeflexion + maxDefobs.


Before testing the robot into water you must carefully check every mechanical and electrical connection. Turn the fish on and control the movements and ensure that both sensors provide signal to the Arduino. Use a multimeter to measure the voltage on their output: in the absence of obstacles the signal should be high. Checking  voltage supply is not a bad idea: make sure is at least 5.5 V.
At this point we’re ready to waterproof the robot: there are many solutions, we have introduced the robot in a plastic bag (breathe inside to see if it has holes and seal it with duct tape). Use rubber bands to keep the bag lying close to the body of the robot and make sure the servo are free to move.
The swim will take place in surface water: you’ll probably have to weigh down the robot with sufficient wright to keep it at the correct height. As mentioned above, in our prototype, we added like 460 grams. Once the robot floats in the right way you can turn it on by placing the small magnet and let him sinuously swim in digital freedom.


From Openstore

Arduino Uno

Servo motors

IR sensor

About Boris Landoni

Boris Landoni is the technical manager of Skilled in the GSM field, embraces the Open Source philosophy and its projects are available to the community.


  1. Pingback: robotics | Pearltrees

  2. it-hertz

    March 4, 2013 at 2:10 PM

    -what is the complete list of material and method for the robofish..the codes has some error.

    • BorisLandoni

      March 8, 2013 at 9:11 AM

      There isn’t a BOM, Just Arduino and servomotors.
      The code is written for the previus version of IDE

      • it-hertz

        March 9, 2013 at 1:15 PM

        Do you have a codes for written for arduino?

        • BorisLandoni

          March 11, 2013 at 9:44 AM

          The code is written for Arduino

  3. Pingback: Robofish: create your robot fish with Arduino | Open Hardware News |

  4. Pingback: Arduino | Pearltrees

  5. xyz

    March 19, 2013 at 8:28 AM

    codes does not working… I tried all version of ide. too many errors (79 errors) in codes. which version of IDE did you use?

  6. xyz

    March 19, 2013 at 9:16 AM

    when delete line 73 its works. error in line 73

    • BorisLandoni

      March 20, 2013 at 2:27 PM

      I’m sorry, is a WP problem…

  7. Mark

    March 25, 2013 at 4:05 PM

    These guys are publishing your article like it were authored by them:

    • BorisLandoni

      March 25, 2013 at 5:59 PM

      thanks for reporting

  8. Pingback: Robofish: el pez robot potenciado con Arduino!! » SAWERS TECHNOLOGY

  9. xyz

    March 26, 2013 at 10:21 AM

    will u fix the problem?

    • BorisLandoni

      April 5, 2013 at 4:01 PM

      Yes, now it’s ok

  10. Pingback: Robofish: An Arduino Controlled Robotic Fish | Spell FoundrySpell Foundry

  11. i'm pugkee

    June 6, 2013 at 3:30 AM

    Thank you very much. I am a newbie for sth like this.

  12. Svin3t

    June 17, 2013 at 8:46 PM

    Did you use the Arduino uno, if not wich did you use?
    And where did you buy the IR sensors?

  13. Aitazaz Aijaz

    September 2, 2013 at 9:52 PM

    i am going to present it in my university. so kindly tell me that is this code correct..? and what are components required for this(complete list).. hope you will rep as soon as possible..

    • BorisLandoni

      September 18, 2013 at 11:15 AM

      Hi the code works of course.
      It isn’t wtire for the last version of Arduino IDE.
      The BOM is ok

  14. sowmya

    October 7, 2013 at 5:40 PM

    where did u buy servo motors ??

  15. GRW

    November 26, 2013 at 12:53 AM

    I see that u call pin 13 as an output mode, but I am using the Leonardo. I am trying to incorporate a bi-color led…my led code works fine as a separate code but when I try to incorporate it to this code I get several errors…I am calling pin 13 for my LED by the way? can you help me understand why 13 is being use as an output in this and in the schematic I don’t see it?

    • BorisLandoni

      November 30, 2013 at 10:20 AM

      You are right, the pin 13 isn’t use

      • grw

        December 2, 2013 at 10:21 PM

        thank you, I was able to incorporate the LED code into this code by the way.

  16. Jyothi VBN

    February 24, 2014 at 10:07 AM

    Hi can you tell me the dimensions of the fish u have done
    can you please give me BOM
    if diving operation has to be done how is it possible….

    • BorisLandoni

      February 27, 2014 at 12:33 PM


      our prototipe is about 40 cm.

      In the Electrical connections you can find the materials we used.

  17. Rae

    December 5, 2014 at 3:51 PM

    Can we have the complete list of the materials needed same with the procedure and the correct code please? We will be doing this as our final requirement and we are really new to this thing. Thank you so much and Godbless!

  18. kae

    December 10, 2014 at 4:29 PM

    Can we have the complete list of the materials needed same with the procedure and the correct code please? We will be doing this as our final requirement and we are really new to this thing. Thank you so much and Godbless!

    • BorisLandoni

      December 16, 2014 at 2:06 PM

      I fixed the code.
      You need un arduino some servos and IR sensor

      • kae

        January 12, 2015 at 3:56 PM

        Thank you! Can I ask where din you purchase the IR sensors? and is there an alternative model for that?

        • BorisLandoni

          January 14, 2015 at 9:45 PM


          • kae

            January 23, 2015 at 4:30 PM

            Is it ok if we will use an IR sensors having an analog output? And what should be the specific range of the sensors?

          • BorisLandoni

            March 9, 2015 at 9:50 PM

            yes no problem. The range is ok <5cm

          • kae

            January 23, 2015 at 4:36 PM

            We just want to clear the specs of the IR sensors because we cannot find the exact product here in our country so we are looking for the alternative model, can we ask which of these models can be use as an alternative? thanks for your immediate response :)

  19. droid

    March 13, 2015 at 8:48 PM

    hai Boris Landoni this is dexter I want to do this project the imformation U gave is good but can U give any more Info about parts and circuit clearly

    • BorisLandoni

      April 10, 2015 at 7:27 AM

      We use normal servo motor and Sharp IR sensor, but you can use also other sensor

      • Shubham Rane'zz

        March 25, 2017 at 12:48 PM

        IR sensors work on reflection light right???but light get refracted (changes angle) in water…..then how did you get proper signals???

        • Matt Faraday

          May 26, 2017 at 2:33 PM

          refraction doesn’t matter too much. enough of the light will make it back to the sensor for it to measure the distance.

  20. gaurav kandpal

    March 28, 2015 at 9:09 AM

    Can you plz provide the code for Arduino?

    • BorisLandoni

      April 10, 2015 at 7:27 AM

      The code is in the post

      • gaurav kandpal

        May 1, 2015 at 4:38 PM

        Did u faced any difficulty for the fish to float….as there where 3 servo motors inside it..wasnt the fish heavy???

        • BorisLandoni

          May 27, 2015 at 10:40 PM

          The body is foam

  21. Nguyễn Tuấn Kiệt

    June 25, 2015 at 11:22 AM

    hello , you can tell me sx and dx , which is left and which is right the fish? ( Looking directly at the fish head )
    tks u so much :)

    • BorisLandoni

      July 18, 2015 at 4:30 PM

      SX = left
      DX = right

  22. Harikishore K A

    August 17, 2015 at 5:02 PM

    How can i mold the fish body?

  23. Jen27

    October 20, 2015 at 8:54 PM

    Please can U help me do a soft robotic fish ? :)

  24. shef

    January 10, 2016 at 2:27 PM

    how did you made the fish body.??/

    • BorisLandoni

      March 3, 2016 at 9:44 AM

      It is pressed sponge

  25. Faseeh Tauqeer

    January 19, 2016 at 1:21 PM

    Can you please tell me How does the fish move? I mean what is the mechanism????

    • BorisLandoni

      March 3, 2016 at 9:44 AM

      The Servo create the movement of a normal fish

  26. ravella aravind

    January 31, 2016 at 6:49 AM

    Can we add a water proof camera to this robot??

    • BorisLandoni

      March 3, 2016 at 9:45 AM

      why not?

  27. Suraj

    February 18, 2016 at 3:58 AM

    Is it possible to get the ready made kit for this?

    • BorisLandoni

      March 3, 2016 at 9:45 AM

      this is only a project

  28. Pingback: 19 Awesome Robots You Can Build With an Arduino | Random Nerd Tutorials

  29. Swaggers Group

    April 21, 2016 at 2:21 PM

    from where you buy fish body

    • BorisLandoni

      May 8, 2016 at 10:51 AM

      We do it ourself

  30. Pingback: Model And Implementation Of Robotic Arm Using Android | world Best

  31. Lol Lol(heron)

    May 28, 2016 at 8:06 AM

    hello,greetings.I build a diffrent kind.i try to build using a hc 05 module so i can control it using a phone.The problem is i dont know what pin from arduino to servo(which pin to which).In this pic i use a few pins like gnd,5v,rx,and tx for the hc 05 module.Thx i hope you reply soon(im still very young very impatient roftl 13 years old )

  32. Anupam Karn

    July 13, 2016 at 6:08 PM

    I want to ask in what condition you have put that servos for such motion?

  33. abraham

    August 2, 2016 at 2:18 PM

    what kind of servo motors did you use and what are the pin connection to the board

  34. Lee Ying

    August 26, 2016 at 10:12 AM


    where do you get the pressed sponge?
    Is it the normal household sponge, high density pressed sponge or the art and craft type that is use for flowers arrangement? Thank you.

  35. jelin

    September 3, 2016 at 10:34 AM

    can u plz tell me the total cost for this project

  36. Vidit Gautam

    September 10, 2016 at 4:26 PM

    where do you get pink styrofoam?
    Also, what are the changes in the code for new ide version?
    Also, great work, one of the best simple arduino projects on the internet!

  37. axel frost

    January 4, 2017 at 4:13 PM

    can I use that coding for arduino uno?

    • diksha adtani

      January 16, 2017 at 7:02 PM


  38. Pingback: Happy new year from best posts in 2016, thank you! | Open Electronics

  39. axel frost

    January 14, 2017 at 11:29 AM

    Actually, my group want do this project for Final Year Project at our university, but we want to change its function. Our group want to make this robot fish control by a controller like rc car. It is possible to do that?

  40. diksha adtani

    January 16, 2017 at 7:01 PM

    From where we can get this fish body?

  41. Shubham Rane'zz

    March 25, 2017 at 12:42 PM

    how did you made servo motors waterproof???

    • BorisLandoni

      March 26, 2017 at 1:04 PM

      With cellophane

  42. ario

    April 13, 2017 at 1:38 AM

    can you tell me what materials need about robofish?

  43. waitwait

    June 5, 2017 at 3:48 PM

    actualTime should be a unsigned long type of variable or the program will bug since millis to Int type sometimes passes negative values.

  44. Pingback: Robot Projeleri | CCS C ile Pic Programlama

  45. Pingback: 200+ Arduino Projects List for Final Year Students- Engineering Solutions

  46. Noah Marques

    December 11, 2017 at 6:13 PM

    codes does not working… I tried all version of ide. too many errors (79 errors) in codes. which version of IDE did you use?

  47. Jishnu Raj

    January 11, 2018 at 3:23 AM

    what are the sensors used here

  48. Pingback: 100+ Robotics Projects for Final Year Engineering Students » The Shastra Phoenixbioinfosys

Leave a Reply