Introduction: Arduino Nano Based Hexbug Scarab Robotic Spider

About: A maker and electronics enthusiast.

The Idea:

To build a programmable robotic spider based on Hexbug Scarab, equipped with the following sensor suite:

Ultrasonic distance sensor

  1. Infrared obstacle sensors/equipment
  2. Gyroscope
  3. Accelerometer
  4. Laser pointer

The robot should be able to do a lot of things. Among them:

  1. Move forward, backward, and rotate.
  2. Maintain direction
  3. Detect and avoid obstacles
  4. Detect being upside-down or on the side
  5. Flash LEDs, point the direction with a laser beam

Since Scarab has Arduino Nano on board, it could be programmed to performed variety of actions, including serving as a physical platform for executing programmable steps in the equivalent of the Logo programming language.

The decision to have two sensors for obstacle detection comes from the fact that each of the sensors have its limitations:

  1. Ultrasound sensor does not detect soft objects (e.g., a teddy bear)
  2. IR sensor does not detect black objects

(A black teddy-bear is still somewhat a challenge for this Scarab...)

When scarab detects an obstacle, it performs a "dance".
A dance is a sequence of movements Scarab performs to help it decide how to go around an obstacle. Some of the dance moves are just for fun and show-off.
Currently scarab performs 3 different dances, which he chooses randomly in front of each obstacle:

  1. Randomly turn right or left by 90 degrees and walk in that direction for 5 seconds. Then attempt to move in the original direction.
  2. Stop and measure distance to the obstacle at 30 degrees to the right and at 30 degrees to the left. Then try go in the direction of measurement with the highest distance to the obstacle.
  3. Back off a little, then execute dance #1 (random) - this is just a little more engaged than #1 dance.

When Scarab is powered up, it needs to calibrate the gyro. It has to be absolutely still, and on a horizontal surface. When Scarab is calibrating gyro, a yellow light is blinking. If Scarab is moved or shaken during calibration, a red light starts blinking, and the calibration process restarts.

If Scarab is turned upside down, it stops and re-starts the calibration process. This also resets the direction Scarab is trying to maintain.

If Scarab cannot move, either because of some serious obstacle or low batteries, all three lights will blink with a slight relative delay (running lights).

For mobile users: the "promotional video" of this project is on YouTube here.

Step 1: Parts and Tools

Unfortunately I was not taking pictures of every step while building the Scarab. This means that a lot of instructions will be just textual, and are only directional.

One does need to be very familiar with soldering together electronic components, and working with such tools as electrical drill, fret-saw, various small Phillips screwdrivers, etc.


To build a Scarab, one will need:

  1. Hexbug Scarab XL radio controlled toy: Scarab XL on Amazon
  2. Arduino Nano microcontroller board: Arduino Compatible Nano v3.0
  3. GY-521 6DOF MPU6050 3 Axis Gyroscope + Accelerometer Module: Gyroscope + Accelerometer Module
  4. ZITRADES Infrared IR Sensor Obstacle Avoidance Sensor: Infrared IR Sensor
  5. SunFounder Ultrasonic Module HC-SR04 Distance Sensor: Ultrasonic Module
  6. Double Sided Plated-through Holes (PTH) 2"x1.34" 250 hole Epoxy Fiber Pitch 0.1": Plates
  7. Dual H-Bridge Motor Driver for DC or Steppers - 600mA - L293D: L293D
  8. 2 x Black Plastic Battery Case Holder Wire 2 x 1.5V AAA: AAA Battery Case Holder
  9. Black Plastic Battery Case Holder for 9V Batteries: Battery Case Holder
  10. Light Emitting Diode LED 5mm 3mm Red Green Yellow: LEDs
  11. (Optional): 5mW Red Laser module: Laser Pointer
  12. Lots of wires, like these: Wires
  13. Micro power switch (can re-use the one on the Scarab, or on the Scarab's remote)
  14. Double-sided adhesive tape: like this


  1. Soldering iron and supplies
  2. Glue gun and supplies
  3. Glue (strong adhesive super-cement type)
  4. Drill and drill bits
  5. Assortment of Phillips screwdrivers
  6. Sharp knife
  7. Scissors

Step 2: Preparing Hexbug

CAUTION: The procedure below will change your hexbug in a way that it will no longer function as a RC toy.

1. Removing the control board

Open the hexbug by removing 4 Phillips screws from the bottom. The top of the spider should come off and you should have access to the internal electronic board.

Unscrew two Phillips screws holding the board in place, cut off the wires leading from the power supply, and to the motors (cut as close to the board as possible - we will reuse the wires) and remove the board - we will not be using it for this project.

2. Power supply

I tested the Scarab using internal 4.5V power supply an found it too weak (Scarab is a bit slow). So I removed the power supply cover, cut off the plastic border and attached two separate power supplies:

  1. 2 x double AAA battery holders on each side of the lower body of the scarab, connected serially give 6V power supply I use for the motors
  2. 1 x 9V battery holder attached from the bottom (partially occupying the built-in battery space, provides 9V for Arduino board itself.

You will need to drill a couple of 3 mm holes in both sides of Scarab's lower part to let the AAA power wires through. Pick a spot which is between and away from the gears inside, and drill very carefully not to ruin the mechanism.

For the 9V wires, you can re-use the existing wire routes at the back of the Scarab.

Step 3: Ultrasonic Sensor

I chose to install the ultrasonic sensor at the front of the Scarab using the double sided tape (multiple layers to even out the microchips).

You need to snap off the two front "teeth" to free up space for the module. Wires go under the top cover on the right and left side of the scarab (where the "teeth" used to be).

Step 4: Infrared Sensor

I placed IR sensor on the left side of the lower part of Scarab's lower body using double sided tape.

The IR sensor has a range of 15 to 20 cm, so I installed it protruding almost to the level of sonic sensor allowing maximum distance for IR obstacle detection.

You might want to use two IR sensors, as having just one on the left side leaves Scarab "blind" on the right side. I felt one was enough for me.

Tune IR sensor using the on-board potentiometer to achieve maximum range.

Step 5: Arduino, Gyro/Accelerometer and Laser


I taped the Gyro/Accelerometer to the inside of the top cover using double sided tape in such a way to achieve maximum horizontal position. Gyro board is placed upside-down with connection holes facing towards the front of Scarab.


Laser pointer is optional and is just a visual way of Scarab to indicate the direction it is heading. The laser pointer is low energy and can be connected directly to Arduino pins. I simply glued the laser to the top of the cover on the left from the actual Arduino board with a glue gun.

Arduino Board

There is no space for the Arduino board inside the Scarab (and you need access to the usb port), so I decided to mount it on top.

  1. I cut off the Arduino pins (using wire cutters, carefully one pin at-a-time)
  2. Drilled two rows of series of 3 mm holes in the top of the Scarab
  3. "Connected" the holes with a sharp knife cutting from one to the other
  4. Smoothed the resulting hole with a file
  5. Glued Arduino board on top with a double sided tape

Step 6: LEDs

I decided to use three LEDs to indicate Scarab's state:

  1. Red (fast blinking) - some kind of error, recoverable or not
  2. Yellow (blinking) - calibrating gyro or obstacle avoidance is in progress
  3. Green (solid) - everything is good - moving along

To install LEDs I drilled 3 x 3mm holes on the right side of the Scarab and glued the LEDs to the inside of the cover.

REMEMBER that each of the LEDs needs to be connected to Arduino via 200 Ohm resistor.

Step 7: Motor Control Board

Motor control circuit is built on the vero board, cut exactly to the measurments of the toy's internal control board. I drilled the two holes in exactly the same places as original control board to be able to re-use the mounting hardware.

There are plenty of wiring diagrams for L293D H-bridge chip on the internet. I am referring to the one published on the website.

Here is how the pins are allocated on my L293D:

  1. connect to +5V pin from Arduino
  2. connect to digital pin #6 from Arduino
  3. connect to pin #1 of motor #1
  4. connect to ground
  5. connect to ground
  6. connect to pin #2 of motor #1
  7. connect to digital pin #9 from Arduino
  8. connect to "+" of the motor power supply (the 6V double pack of 2xAAA)
  9. connect to +5V pin from Arduino
  10. connect to digital pin #3 from Arduino
  11. connect to pin #2 of motor #2
  12. connect to ground
  13. connect to ground
  14. connect to pin #1 or motor #2
  15. connect to digital pin #5 from Arduino
  16. connect to +5V pin from Arduino

Both motors on the Scarab already have "full capacitor treatment", so you just need to solder the wires from L293D pins 3, 6. 11 and 14 to respective resistors on the motors.

Step 8: Arduino Pin Allocation

Below is the Arduino Nano pin allocation associated with the attached sketch:

Vin - connect to +9V of the 9V power supply (via power switch)
Gnd - connect to -9V, ground wires of the LEDs, Ultrasound and IR sensors, Gyro/Accelerometer, Laser, LED cathodes and L293D
Reset - not connected
5v - connect to VCCs pins of L293D, Ultrasound and IR sensors, Gyro/Accelerometer
A7 - not connected
A6 - not connected
A5 - connect to SCL pin of Gyro/Accelerometer
A4 - connect to SDA pin of Gyro/Accelerometer
A3 - connect to VCC of the laser pointer
A2 - not connected
A1 - not connected
A0 - not connected
REF - not connected
3.3V - not connected
D13 - not connected
D12 - connect to DATA pin of the IR sensor
D11 - connect to TRIGGER pin of the Ultrasound sensor
D10 - connect to ECHO pin of the Ultrasound sensor
D9 - connect to pin #2 of motor #1
D8 - connect to Red LED anode (via resistor)
D7 - connect to Green LED anode(via resistor)
D6 - connect to pin #1 of motor #1
D5 - connect to pin #1 of motor #2
D4 - connect to Yellow LED anode (via resistor)
D3 - connect to pin #2 of motor #2
D2 - connect to INT pin of Gyro/Accelerometer (not currently used by the sketch)
the rest of the pins are not used

Step 9: Assembly

I recommend assembling the hexbug in the following order:

  1. Mark positions of all external power supply cases and drill the wire holes
  2. Mark position of the IR sensor and drill the hole for its wires
  3. Attach power supply cases and pull the wires inside the Scarab, connect the AAA wires from 2 cases serially to achieve 6V
  4. Attach IR sensor and pull its wires inside the Scarab
  5. Attach the Ultasonic sensor and lay its wires inside the scarab
  6. Solder L293D onto the vero board, solder wires to all its pins
  7. Solder +6V to the motor power pin of L239D (via 3-way switch - see step #23)
  8. Solder -6V to the ground wire of L293D
  9. Create a "common ground" wire and attach all sensor/arduino ground wires to it, including the -9V power wire
  10. Attach vero board to the control board place with Phillips screws
  11. Solder L293D motor control pins to appropriate motor pins
  12. Drill appropriate holes in the Scarab top cover for Arduino and LEDs
  13. Attach Gyro/Accelerometer to the inside of the Scarab cover with double sided adhesive tape
  14. Glue the LEDs to respoective holes
  15. Solder resistors to LED anodes
  16. Solder resistor cathodes to a "common ground"
  17. Solder wires of different colors to the LED resistors
  18. Mark all the wires that should go to Arduino and pull them through respective sides of the "Arduino" holes in the Scarab top cover
  19. Pull all the wires through respective sides
  20. Attach Arduino Nano to the outside of top cover with double sided adhesive tape
  21. Close Scarab cover and slightly tighten 2 screws diagonally to keep it in place
  22. Solder all Arduino-bound wires to respective Arduino pins
  23. Use three way switch from hexbug remote as a power switch for both Arduino board and motors: route 9V and 6V positive wires to the switch and then back to VIN pin of Arduino and Vmotor+ pin of L293D respectively
  24. Glue the switch to the Scarab cover with a glue gun
  25. Pull the excess wires down under the top cover
  26. Tighten all 4 Phillips screws

Step 10: Sketch


Scarab is currently based on the following libraries:

  1. Wire - part of Arduino IDE
  2. Statistic - available on Github
  3. MPU6050 - available on Github
  4. I2Cdev - available on Github
  5. DirectIO - available on Github

Developed by me:

  1. AvgFilter - available on Github
  2. TaskScheduler - available on Github


Version 1.7.0 implements a new obstacle avoidance algorithm: available on Github

  • Upon detecting an obstacle check distance to the nearest obstacle at 3 and 9 o'clock, move in the direction where obstacle is further away
  • Make right/left preference from step1 a "preferred" obstacle avoidance direction. Scarab will always try to go right or left (based on the decision from step 1)
  • If the original direction cannot be achieved within N turns, "give up" the direction and start preferring to always turn in the opposite direction, increasing the "give up" count by 2 (N+2). As a result Scarab will tend to go right or left longer every time it "gives up" with a hope to finally hit the original direction
  • Once the original direction is reached and maintained for 5 seconds, the "preferred" turn direction is "forgotten". Next obstacle will be evaluated from step 1 again.
Guerilla Design Contest

Participated in the
Guerilla Design Contest

Explore Science Contest

Participated in the
Explore Science Contest