The design of this robot focused around using the book "Getting Started with Arduino" by Massimo Banzi and published with [makezine.com Make]. I also employed code for running the servos from a project titled: How to Make an Arduino Controlled Servo Robot (SERB).
The Arduino Mothbot is in total a pretty quick robot to build. Assuming you start with all of the parts and don't have to improvise, the project in total should take maybe an hour to build. That is if you follow the instructions and copy the code. However, if you build only one feature at a time and test along the way then this project could take significantly longer. The advantage of the longer track is that you'll probably learn a lot more and have some fun along the way.
Remove these ads by
Signing UpStep 1Gather your Parts and Tools
Arduino Mothbot Parts List
Now you may want to get some tools. Since this project employs a solderless breadboard you can do without a lot of fancy electronics equipment. Hopefully you can find the rest of the things you need in a garage:
1. Needle nose pliers
2. Wire Cutters
3. Flat head screw driver
4. Small Phillips (4-sided) screw driver
5. Adjustable wrench or 11/32" hex wrench
6. Drill
7. 1/16", 5/32" and 7/32" drill bits
8. Saw (optional)
9. Safety Goggles
Please use safe practices when using any power tools.
| « Previous Step | Download PDFView All Steps | Next Step » |













































* Arduino Mothbot
*
* Digital Pin Wiring:
* pin 7 - Pushbutton Signal
* pin 11 - Right Servo Signal
* pin 3 - Left Servo Signal
*
* Analog Pin Wiring:
* pin 0 - Left Light Sensor Signal
* pin 1 - Right Light Sensor Signal
*
* License: This work is licenced under the Creative Commons
* Attribution-Share Alike 3.0 Unported License. To
* view a copy of this licence, visit
* http://creativecommons.org/licenses/by-sa/3.0/
* or send a letter to Creative Commons, 171 Second
* Street, Suite 300, San Francisco, California 94105,
* USA.
*/
//------------------------------------------------------------------------
// START OF ARDUINO MOTHBOT SETUP
//--- Library
#include
//--- Pin Definitions
#define LEFTSERVOPIN 3 //The digital pin that the left servo is connected to
#define RIGHTSERVOPIN 11 //The digital pin that the right servo is connected to
#define LEFTLIGHTSENSOR 0 //The analog pin the left light sensor is connected to
#define RIGHTLIGHTSENSOR 1 //The analog pin the right light sensor is connected to
#define PUSHBUTTON 7 //The digital pin that the pushbutton sensor is connected to
#define LED 13 //The LED on the arduino
//--- Servo Setup
Servo leftServo;
Servo rightServo;
//--- Speed Setup
int robotSpeed = 75; //set the speed of the robot
int rightSpeed = 50;
int leftSpeed = 50;
//--- Sensor Values
int leftLightSensorVal = 0;
int rightLightSensorVal = 0;
int lightSensorThreshold = 200;
//--- On/Off values
int pushButtonVal = 0;
int old_pushButtonVal = 0;
int pushButtonState = 0;
//--- Delay Threshold
int delayParam = 10; //Supported Times - 0 - 255 (0 to 25.5 Seconds) value * 100 milliseconds
// END OF ARDUINO MOTHBOT SETUP
//------------------------------------------------------------------------
//------------------------------------------------------------------------
//START OF ARDUINO MOTHBOT PROGRAM
//--- The program setup
void setup()
{
Serial.begin(9600); //Starts the serial port
robotSetup(); //sets the state of all neccesary
//pins and adds servos to your sketch
}
//--- The main program code
void loop()
{
//--- Get information from the light sensor
leftLightSensorVal = analogRead(LEFTLIGHTSENSOR);
rightLightSensorVal = analogRead(RIGHTLIGHTSENSOR);
//--- Get information from the pushbutton sensor
pushButtonVal = digitalRead(PUSHBUTTON);
//--- Set the state of the pushbutton
if((pushButtonVal == HIGH) && (old_pushButtonVal == LOW))
{
Serial.println("Button Pushed");
pushButtonState = 1 - pushButtonState;
delay(10); // Simple de-bouncing
}
//--- Reset the state of the push button
old_pushButtonVal = pushButtonVal;
//--- If pushbutton state HIGH then turn on the robot and
// listen to the light sensor information
if(pushButtonState == 1)
{
digitalWrite(LED, HIGH); // turn LED ON
if(leftLightSensorVal - rightLightSensorVal >= lightSensorThreshold)
{
Serial.println("Left");
goLeft();
delay(delayParam * 100);
goStop();
}
else if(rightLightSensorVal - leftLightSensorVal >= lightSensorThreshold)
{
Serial.println("Right");
goRight();
delay(delayParam * 100);
goStop();
}
else
{
Serial.println("Forward");
goForward();
delay(delayParam * 100);
goStop();
}
}
else
{
Serial.println("Stop");
digitalWrite(LED, LOW); // turn LED OFF
goStop();
}
}
//END OF ARDUINO MOTHBOT PROGRAM
//------------------------------------------------------------------------
//------------------------------------------------------------------------
//START OF ARDUINO MOTHBOT FUNCTIONS
//--- The setup for the robot
void robotSetup()
{
//--- Set the speed of the robot
setSpeed(robotSpeed);
//--- Set up the servos
pinMode(LEFTSERVOPIN, OUTPUT); //sets the left servo signal pin
//to output
pinMode(RIGHTSERVOPIN, OUTPUT); //sets the right servo signal pin
//to output
leftServo.attach(LEFTSERVOPIN); //attaches left servo
rightServo.attach(RIGHTSERVOPIN); //attaches right servo
//--- Set up the pushbutton
pinMode(PUSHBUTTON, INPUT); //sets the pushbutton sensor pin to input
//--- Set up the LED to see robot state
pinMode(LED, OUTPUT); //sets the led pin as output
//--- Tell the robot to stop the servos
goStop();
}
//--- Set the speed of the robot between 0-(stopped) and 100-(full speed)
void setSpeed(int newSpeed)
{
setSpeedLeft(newSpeed); //sets left speed
setSpeedRight(newSpeed); //sets right speed
}
//--- Set the speed of the left wheel
void setSpeedLeft(int newSpeed)
{
if(newSpeed >= 100) {newSpeed = 100;} //if speed is greater than 100
//make it 100
if(newSpeed <= 0) {newSpeed = 0;} //if speed is less than 0 make
//it 0
leftSpeed = newSpeed * 0.9; //between 0 and 90
}
//--- Set the speed of the right wheel
void setSpeedRight(int newSpeed)
{
if(newSpeed >= 100) {newSpeed = 100;} //if speed is greater than 100
//make it 100
if(newSpeed <= 0) {newSpeed = 0;} //if speed is less than 0 make
//it 0
rightSpeed = newSpeed * 0.9; //scales the speed to be
}
//--- Move the robot forward
void goForward()
{
leftServo.write(90 + leftSpeed);
rightServo.write(90 - rightSpeed);
}
//--- Move the robot backward
void goBackward()
{
leftServo.write(90 - leftSpeed);
rightServo.write(90 + rightSpeed);
}
//--- Move the robot right
void goRight()
{
leftServo.write(90 + leftSpeed);
rightServo.write(90 + rightSpeed);
}
//--- Move the robot left
void goLeft()
{
leftServo.write(90 - leftSpeed);
rightServo.write(90 - rightSpeed);
}
//--- Stop the robot
void goStop()
{
leftServo.write(90);
rightServo.write(90);
}
//END OF ARDUINO MOTHBOT FUNCTIONS
//------------------------------------------------------------------------
#include Servo.h
The jerky motion of a robot that only moves straight or pivots tends to bug me (although it is a "moth" bot), so one possible extension of the mothbot is to make it turn, rather than pivot. To smooth out your mothbot, instead of making both wheels turn the same speed, try varying the speed based on the relative readings from the light sensors. For example:
--If the reading from the two sensors is the same (within a reasonable threshold) put both motors full ahead.
--If the reading from the right sensor is twice that of the left, set the left motor twice as fast as the right motor.
--Fiddle with the parameters until you get something you like. You may want to turn down your delay so you get faster updates and smoother performance.
Another possible extension that can help make your robot more robust to different environments is to add a potentiometer (variable resistor) to change the threshold you use on your light sensors on-the-go without having to reprogram your bot.
Thanks for the good project!