You are on page 1of 6

Food Living Outside Play Technology Workshop

Lego Spybotics with Arduino


by MrShambles on November 22, 2013

Table of Contents

Lego Spybotics with Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

Intro: Lego Spybotics with Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

Step 1: Open up! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

Step 2: Motor control (with the infamous H-Bridge!) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

Step 3: Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

Related Instructables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

Advertisements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

http://www.instructables.com/id/Lego-Spybotics-with-Arduino/
Author:MrShambles
I'm currently studying a BSc in Geomatics at the University of Cape Town. I love making stuff and being outside, too!

Intro: Lego Spybotics with Arduino


A friend gave me his old Lego Spybotics robot, however, the programming environment that goes along with it is a bit too orientated to the younger user.

So why not control it using an Arduino?

Step 1: Open up!


In order to get this robot moving, we need to borrow its motors.

There are two DC motors in the robot. This is great, because we can control them independently, allowing forward, backwards and turning motion.

Start by unscrewing the lid off the top of the robot. The screws are located on the bottom. Four by the battery bay and two in the front.

Locate the two DC motors near the back, then switch on your soldering iron.

De-solder the motors from the contact points. This can be done by simply applying a little upwards pressure on the motor whilst heating the solder - the contact should
come unstuck.

Once detached, remove the motors by just wiggling them out. Solder some wire (about 10cm or more) onto the contacts, taking care not to damage the rather fragile
motor contacts.

Before putting the motors back, cover the original contacts with some electrician's tape, just to isolate the old board from any current we send the motors' way. Then just
slot the motors back in.

There are two buttons on the top of the lid. Pull them out. This will create two holes. Route the cables through those holes and affix the lid back on.

Image Notes
1. DC motor
2. motor contact

http://www.instructables.com/id/Lego-Spybotics-with-Arduino/
Image Notes
1. cables routed through button holes
2. these structures will be used to hold the arduino

Image Notes
1. electrician's tape to cover old contacts

Step 2: Motor control (with the infamous H-Bridge!)


Now that we have access to the motors, we need to drive them. I'm using a L293D quad half H-bridge driver chip. It cost me R27 ($3), and because of it being quad half,
we can control both motors off one chip.

Refer to the image as to how to connect the H-bridge.

The reason for the quadruple grounds is to sink heat (and of course ground the chip), so be sure to connect all of them.

http://www.instructables.com/id/Lego-Spybotics-with-Arduino/
Step 3: Arduino
I'm using a Arduino Nano, which plugs directly into a breadboard, so the H-Bridge and the arduino sit on the same breadboard.

Connect the Arduino up as stated in the previous step and in the code. 3 AA batteries work well for the motors (you can borrow the existing battery bay to house them).

Here's the code. It simply moves the bot forward, turns right, left, then reverses and repeats.

/*
Spybotics+Arduino
Nic Shackle
20/11/13
*/

int LED = 13; //using built in LED


int Eleft = 6; //enable left motor. Must be a PWM pin
int Eright = 5; //enable right motor. Must be a PWM pin
int Rforward = 12; //right motor forward
int Rbackwards = 11; //right motor back
int Lforward = 9; //left motor forward
int Lbackwards = 10; //left motor back

void setup()
{
//initialise pins as outputs
pinMode(LED, OUTPUT);
pinMode(Eleft, OUTPUT);
pinMode(Eright, OUTPUT);
pinMode(Rforward, OUTPUT);
pinMode(Rbackwards, OUTPUT);
pinMode(Lforward, OUTPUT);
pinMode(Lbackwards, OUTPUT);

void loop()
{
int x=200; //this value sets the speed of all movements
go(true, 255, x); //go forward at speed 255 for x milliseconds
delay(20); //wait a bit
turn(true, 255,x); //turn right at speed 255 for x milliseconds
delay(20);
turn(false, 255,x);
delay(20);
go(false, 255, x);
delay(20);

void go(boolean dir, int spd, int time) //function that moves the robot forward or back
{

if(dir) //if direction is TRUE, go forward


{
digitalWrite(Rbackwards, LOW);
digitalWrite(Rforward, HIGH);
digitalWrite(Lbackwards, LOW);
digitalWrite(Lforward, HIGH);
}

http://www.instructables.com/id/Lego-Spybotics-with-Arduino/
else //else if direction is FAlSE, go back
{
digitalWrite(Rbackwards, HIGH);
digitalWrite(Rforward, LOW);
digitalWrite(Lbackwards, HIGH);
digitalWrite(Lforward, LOW);
}

analogWrite(Eright,spd); //enable right motor at given speed


analogWrite(Eleft,spd); //enable left motor at given speed
delay(time); //carry on turning for given amount of time
analogWrite(Eright,0); //stop right motor
analogWrite(Eleft,0); //stop left motor

void turn(boolean left, int spd, int time) //function that makes robot turn
{
if(left) //if left is TRUE, turn left
{
digitalWrite(Rbackwards, LOW);
digitalWrite(Rforward, HIGH);
digitalWrite(Lbackwards, HIGH);
digitalWrite(Lforward, LOW);
}
else //else if left is FALSE, turn right
{
digitalWrite(Rbackwards, HIGH);
digitalWrite(Rforward, LOW);
digitalWrite(Lbackwards, LOW);
digitalWrite(Lforward, HIGH);
}

analogWrite(Eright,spd);//enable right motor at given speed


analogWrite(Eleft,spd); //enable left motor at given speed
delay(time); //carry on turning for given amount of time
analogWrite(Eright,0); //stop right motor
analogWrite(Eleft,0); //stop left motor

(Excuse the sometimes sloppy code)

Image Notes
1. ignore the resistor...
2. The H-bridge is hidden in there somewhere
3. arduino nano
4. wires from motor

http://www.instructables.com/id/Lego-Spybotics-with-Arduino/
Related Instructables

myRobot - DIY
DIY: How to Lego quick and Robot (video) by SPROT, my LINUSBot - Line
Build a WiFi easy Spybot by aneophyte SimPleROboT Follower Robot
Robot Spybot michaelgohjs Quick and Easy by sbateson by BIGDOG1971
by AppleGuy Arduino
Compatible H-
bridge Shield by
bud_weiser

Advertisements

http://www.instructables.com/id/Lego-Spybotics-with-Arduino/

You might also like