Introduction: Arduino Nano Based Hexbug Scarab Robotic Spider
To build a programmable robotic spider based on Hexbug Scarab, equipped with the following sensor suite:
Ultrasonic distance sensor
- Infrared obstacle sensors/equipment
- Laser pointer
The robot should be able to do a lot of things. Among them:
- Move forward, backward, and rotate.
- Maintain direction
- Detect and avoid obstacles
- Detect being upside-down or on the side
- 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:
- Ultrasound sensor does not detect soft objects (e.g., a teddy bear)
- 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:
- Randomly turn right or left by 90 degrees and walk in that direction for 5 seconds. Then attempt to move in the original direction.
- 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.
- 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:
- Hexbug Scarab XL radio controlled toy: Scarab XL on Amazon
- Arduino Nano microcontroller board: Arduino Compatible Nano v3.0
- GY-521 6DOF MPU6050 3 Axis Gyroscope + Accelerometer Module: Gyroscope + Accelerometer Module
- ZITRADES Infrared IR Sensor Obstacle Avoidance Sensor: Infrared IR Sensor
- SunFounder Ultrasonic Module HC-SR04 Distance Sensor: Ultrasonic Module
- Double Sided Plated-through Holes (PTH) 2"x1.34" 250 hole Epoxy Fiber Pitch 0.1": Plates
- Dual H-Bridge Motor Driver for DC or Steppers - 600mA - L293D: L293D
- 2 x Black Plastic Battery Case Holder Wire 2 x 1.5V AAA: AAA Battery Case Holder
- Black Plastic Battery Case Holder for 9V Batteries: Battery Case Holder
- Light Emitting Diode LED 5mm 3mm Red Green Yellow: LEDs
- (Optional): 5mW Red Laser module: Laser Pointer
- Lots of wires, like these: Wires
- Micro power switch (can re-use the one on the Scarab, or on the Scarab's remote)
- Double-sided adhesive tape: like this
- Soldering iron and supplies
- Glue gun and supplies
- Glue (strong adhesive super-cement type)
- Drill and drill bits
- Assortment of Phillips screwdrivers
- Sharp knife
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:
- 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
- 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.
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.
- I cut off the Arduino pins (using wire cutters, carefully one pin at-a-time)
- Drilled two rows of series of 3 mm holes in the top of the Scarab
- "Connected" the holes with a sharp knife cutting from one to the other
- Smoothed the resulting hole with a file
- Glued Arduino board on top with a double sided tape
Step 6: LEDs
I decided to use three LEDs to indicate Scarab's state:
- Red (fast blinking) - some kind of error, recoverable or not
- Yellow (blinking) - calibrating gyro or obstacle avoidance is in progress
- 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 Adafruit.com website.
Here is how the pins are allocated on my L293D:
- connect to +5V pin from Arduino
- connect to digital pin #6 from Arduino
- connect to pin #1 of motor #1
- connect to ground
- connect to ground
- connect to pin #2 of motor #1
- connect to digital pin #9 from Arduino
- connect to "+" of the motor power supply (the 6V double pack of 2xAAA)
- connect to +5V pin from Arduino
- connect to digital pin #3 from Arduino
- connect to pin #2 of motor #2
- connect to ground
- connect to ground
- connect to pin #1 or motor #2
- connect to digital pin #5 from Arduino
- 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:
- Mark positions of all external power supply cases and drill the wire holes
- Mark position of the IR sensor and drill the hole for its wires
- Attach power supply cases and pull the wires inside the Scarab, connect the AAA wires from 2 cases serially to achieve 6V
- Attach IR sensor and pull its wires inside the Scarab
- Attach the Ultasonic sensor and lay its wires inside the scarab
- Solder L293D onto the vero board, solder wires to all its pins
- Solder +6V to the motor power pin of L239D (via 3-way switch - see step #23)
- Solder -6V to the ground wire of L293D
- Create a "common ground" wire and attach all sensor/arduino ground wires to it, including the -9V power wire
- Attach vero board to the control board place with Phillips screws
- Solder L293D motor control pins to appropriate motor pins
- Drill appropriate holes in the Scarab top cover for Arduino and LEDs
- Attach Gyro/Accelerometer to the inside of the Scarab cover with double sided adhesive tape
- Glue the LEDs to respoective holes
- Solder resistors to LED anodes
- Solder resistor cathodes to a "common ground"
- Solder wires of different colors to the LED resistors
- Mark all the wires that should go to Arduino and pull them through respective sides of the "Arduino" holes in the Scarab top cover
- Pull all the wires through respective sides
- Attach Arduino Nano to the outside of top cover with double sided adhesive tape
- Close Scarab cover and slightly tighten 2 screws diagonally to keep it in place
- Solder all Arduino-bound wires to respective Arduino pins
- 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
- Glue the switch to the Scarab cover with a glue gun
- Pull the excess wires down under the top cover
- Tighten all 4 Phillips screws
Step 10: Sketch
Scarab is currently based on the following libraries:
- Wire - part of Arduino IDE
- Statistic - available on Github
- MPU6050 - available on Github
- I2Cdev - available on Github
- DirectIO - available on Github
Developed by me:
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.