loading

The purpose of this project is to build an arduino robot that follows light and avoids possible obstacles in its way. It is based on an Arduino Uno microcontroller board and it uses Light Dependent Resistors to find the most bright light source while avoiding any obstacle on its way to the light source, using an Ultrasonic Distance Sensor.

The fun thing is that it has an old android smartphone on it and it broadcasts live onboard video to your computer screen. You can see what it can see.

Step 1: List of Components and Tools

Components

  • Arduino Uno board
  • Breadboard
  • 2 x Continuous Rotation Servo
  • 1 x Micro generic Servo
  • Ultrasonic Distance Sensor (HC-SR04 is cheap and reliable)
  • 2 x Light Dependent Resistors
  • 2 x 10kΩ resistors
  • 2 x 330μF capacitors
  • 1 x 100μF capacitor
  • Robot Chassis (i used the magician chassis, you can use whatever you like, even cardboard)
  • 2 x plastic wheels
  • 9V battery for the servos
  • 4 x 1,5V batteries for the Arduino
  • Holders for the batteries
  • Hook up wires
  • Rubber bands
  • Screws
  • Zip ties
  • Several supports for the components
  • On/Off Switches (optional)
  • Old Android Smartphone (optional)

Tools

  • Screwdriver
  • Wire cutter
  • Duct tape (or electrical tape)
  • Soldering iron (optional)
  • Solder wick (optional)

Step 2: Assembling the Chassis and Motors

The first step is to assembly our chassis and put on the continuous rotation servos. You can use any chassis you want, it may be acrylic, aluminum or even from a cd case. I chose the Magician chassis (you can find it on ebay) because it has so much holes and so it can be very versatile for many projects.

So, first of all, screw the four plastic holders for the servos in the chassis. Make sure to tighten them up properly so they will not move. Next, attach the servo motors and screw the plastic wheels on them. Our wheels are ready!! You can use a rubber band to keep them straight because they may move left or right from the weight of the robot when we finish it.

Next, put the 4 batteries (1.5V each one) in the holder and attach it to the chassis using a rubber band. This is the power for the Arduino.

Step 3: Ultrasonic Sensor

It is time for the distance sensor. In this project i used an ultrasonic distance sensor, the HC-SR04, which is cheap and very reliable. Ultrasonic sensors work like this; they produce an ultrasonic sound which bounce on objects and then they receive it back. The amount of time that the sound needs to go from the sensor to the object and then back to the sensor gives us the distance in cm (or any other unit). We will talk about this in the programming section so don't worry.

What we want is to attach the distance sensor on the micro servo so our robot can check right and left when it encounters an object and choose the clear path to move on. I used a rubber band (what else) to support my sensor and screwed it in a metallic frame i found in my tool box. That way i managed to put it on top of the micro servo so it can move. You can put it directly in the servo with a piece of string to hold it in place, use your imagination and be creative!! This is a unique robot made from you!!

Step 4: Putting It Together

Now it is time to assemble the upper deck of the Magician chassis. Just screw it in place and put the arduino board on it. Next put the 9V battery holder in front of the arduino and the breadboard on top of it. I used blu-tack to "glue" them togoether so they will not move around. Again, you can use whatever you want to "glue" them. After that, put the micro servo with the distance sensor in the front side of the chassis and use use blu-tack to make it stand there. I used a zip tie to make sure it will not fall. Make sure to leave a gap between the breadboard and the servo-sensor "tower" so the sensor can move freely in both directions (left-right). I left a fairly big gap there and i slided my old smartphone between them so i can use the camera. I secured it using a single zip tie. That way it is safe and you can put it there or take it in your hand really fast. In order to have live video from your robot to your computer screen, all you have to do is to install the "IP Webcam" app to your smartphone and follow the instructions to do it.

You can also put an on/off switch in order to power your robot but this is an optional step. If you want to do it, simply cut the power wire from the batteries and solder it in the switch. Do not cut the ground wire.

Step 5: Light Dependent Resistors and Final Wiring

Light Dependent Resistors or just photoresistors, are the eyes of your robot. They are responsible for light detection and they will tell to your robot to move to the brightest light source. Place them to your breadboard, one on each side to determine the left - right orientation, and connect one end of the photoresistor to the power and the other to the ground through a 10kΩ (kilohm) resistor. Connect the left photoresistor to Analog Pin A2 on the Arduino. The right photoresitor goes to Analog Pin A0. If your robot goes to the darkest side of the room instead of the brightest just switch the pins and it will be ok.

Lets move on to the servos. Attach the 3 pin header of each of the servos in the breadboard. The colors of the wires indicate the purpose of each wire. Red is for the power, black goes to the ground and white goes to Arduino. Be careful not to mess with power and ground, or else you can damage the servo. Read carefully the instruction manual of your servo and write down what the color of each wire means. When a servo motor starts to move, it draws more current than if it were already in motion. This will cause a dip in the voltage on your board. By placing a capacitor accross power and ground right next to the male headers you can smooth out any voltage changes that may occur. For the two continuous rotation servos i used 2 x 330μF capacitors and for the micro servo i used 1 x 100μF capacitor. Be very careful to make sure you are connecting the cathode to ground (that's the side with a black stripe down on the side) and the anode to power. If you put the capacitors in backwards, they can explode. Now attach the left servo to Digital Pin 10 on the Arduino and the right servo to Digital Pin 11. Also, attach the micro servo to Digital Pin 6.

Finally, we must connect the distance sensor. Attach the Vcc pin to power, the Gnd pin to ground, the Trig pin to Digital Pin 7 and the Echo pin to Digital Pin 4 on your Arduino. Thats it!

Lastly, we must connect power to our breadboard. As you can see in the image that shows the schematic of our circuit, i have used two different power sources in the same breadboard. One is for the servos and the other for the sensors. Be very careful not to mix the wiring because you will be in trouble. Follow the schematic picture and pay attention!

We just built our robot!!!

But it cant do anything until we programme it. So lets go!!

Step 6: Arduino Code

First of all you need to center your continuous rotation servos. By centering them, i mean to stop them from spinning freely all the time. Type the code below and upload it to your arduino. Now two things are going to happen. Either the servos will not spin, which means that they are already centered, or they will spin at some speed. If they spin you must do something. Continuous servos have a screw in the side where their cables are. Take the screwdriver and while the servo is spinning screw or unscrew the screw a little bit until the servo stops. This is how you center them. The brake (center) is at 90, full throttle left is 0 and full throttle right is 180.

#include

Servo leftMotor;
Servo rightMotor;

void setup() {

rightMotor.attach(11);
leftMotor.attach(10);

}

void loop() {

rightMotor.write(90);

leftMotor.write(90);

}

Now you are ready to programme your arduino robot!! Get that code and upload it to your Arrduino!! Hopefully everything is going to work perfectly!!

#include //include Servo library


#define trigPin 7 //the trig pin from distance sensor

#define echoPin 4 //the echo pin from distance sensor


const int RForward = 120; //the speed of the servo, maximum speed is 180

const int RBackward = 60;

const int LForward = 60;

const int LBackward = 120;

const int RNeutral = 90; //centered position

const int LNeutral = 90;

const int RightLightSensor = 0; //declare the analog pins for the photoresistors

const int LeftLightSensor = 2;

const int collisionThresh = 15; //threshold for obstacles (in cm)


int SensorLeft;

int SensorRight;

int SensorDifference;

int leftDistance, rightDistance; //distances on either side


Servo panMotor; //micro servo with the distance sensor on it

Servo leftMotor; //declare motors

Servo rightMotor;


long duration; //time it takes to recieve PING))) signal


void setup() {

rightMotor.attach(11); //attach motors to proper pins

leftMotor.attach(10);

panMotor.attach(6);

panMotor.write(90); //center the pan servo

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

pinMode(LeftLightSensor, INPUT);

pinMode(RightLightSensor, INPUT);

}


void loop(){

int distance = ping(); //call the ping function to get the distance in front of the robot

SensorLeft = 1023 - analogRead(LeftLightSensor); //read the photoresistors

delay(1);

SensorRight = 1023 - analogRead(RightLightSensor);

delay(1);

SensorDifference = abs(SensorLeft - SensorRight);


if (distance > collisionThresh) //if path is clear be guided from the light

{

if (SensorLeft > SensorRight && SensorDifference > 75) { //left

leftMotor.write(LBackward);

rightMotor.write(RForward);

delay(250);

}

if (SensorLeft < SensorRight && SensorDifference > 75) { //right

leftMotor.write(LForward);

rightMotor.write(RBackward);

delay(250);

}

else if (SensorDifference < 75) { //forward

leftMotor.write(LForward);

rightMotor.write(RForward);

delay(500);

}

}

else //if path is blocked {

leftMotor.write(LNeutral);

rightMotor.write(RNeutral);

panMotor.write(0);

delay(500);

rightDistance = ping(); //scan to the right

delay(500);

panMotor.write(180);

delay(700);

leftDistance = ping(); //scan to the left

delay(500);

panMotor.write(90); //return to center

delay(100);

compareDistance();

}

}

void compareDistance() {

if (leftDistance > rightDistance) //if left is less obstructed

{

leftMotor.write(LBackward);

rightMotor.write(RForward); //turn left

delay(500);

}

else if (rightDistance > leftDistance) //if right is less obstructed

{

leftMotor.write(LForward);

rightMotor.write(RBackward); //turn right

delay(500);

}

else //if they are equally obstructed

{

leftMotor.write(LForward);

rightMotor.write(RForward); //turn 180 degrees

delay(1000);

}

}

long ping() { // Send out PING))) signal pulse

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(5);

digitalWrite(trigPin, LOW);

//Get duration it takes to receive echo

duration = pulseIn(echoPin, HIGH);

//Convert duration into distance

return duration / 29 / 2;

}

Step 7: Conclusion

Well, i hope you enjoyed this instructable.

Give it a shot and create your own robot!! It is easy and you will learn so much things!!

Post your opinion about this instructable in the comment section and show me your creations!!

Have fun and be creative!!

Step 8: Update -February 11 2015-

The first update is here!

I wanted to say something about the servo motors i used. I used continuous rotation servos because i had a pair of them in my tool box. You can use simlpe dc motors if you want, it is going to be the same. The only difference is that servos have higher accuracy but it is not necessary in this project. If you choose to put dc motors, you must change the code a little bit because they will not operate with the servo.write commands. Dc motors operate using the digitalWrite() command.

Also, i want to add a link to my code to GitHub so you can follow my code more easily. So, here it is:

https://github.com/vagelis-chantzis/Arduino-Robot

Thats it for now!! Keep sending me feedback so i can make this instructable better!!

Keep coding!!

Step 9: Update -February 27 2015-

Just a little cable management for today!!

It was a mess up there, so i used the chasis holes wisely to make it look nice and clean.

Also, i replaced the 6 AA batteries with a classic 9V battery.

Have a nice day!

<p>Where do you buy the plastic servo holders?</p>
<p>Hi!! I bought them at my local hardware and electronics shop. It is not something special, you can use whatever comforts you, even 3d print a couple of them. Be creative!</p>
<p>Thanks so much im gonna see what I can do</p>
<p>Can you give me a link to the website or item?</p>
<p><a href="https://www.aliexpress.com/wholesale?catId=0&initiative_id=AS_20170105002408&SearchText=servo+mounting+bracket">https://www.aliexpress.com/wholesale?catId=0&amp;initi...</a></p><p>Here are some mounting brackets. Choose whatever you like.</p>
<p>the code not working help me please</p>
<p>What part of code is not working exactly?</p>
<p>Hey Vagelis, could you please explain me me why you put 75 as value for the sensor difference. Thanks.</p>
<p>Hey SardoC, 75 is a result of trial and error in the coding process. With this value you get more clear readings as of where there is more light, left or right. Try with different values, test it and post your thoughts. </p>
<p>Hey Vagelis! We won the competition! We are now participating in the county competition! We are going to attempt to switch to DC motors. Do you mind sharing the DC motor code with us? If you could that would be very helpful! Thanks!</p>
Nice to hear that you won! Unfortunatelly, i will not be able to help you because i am in the army for nine months.. Hope you can do it!!
<p>Hey Vagelis! We won the competition! We are now participating in the county competition! We are going to attempt to switch to DC motors. Do you mind sharing the DC motor code with us? If you could that would be very helpful! Thanks!</p>
<p>Hey Vagelis! We won the competition! We are now participating in the county competition! We are going to attempt to switch to DC motors. Do you mind sharing the DC motor code with us? If you could that would be very helpful! Thanks!</p>
<p>Vagelis, Great work!</p><p>I am in a robotics class at Rochester High School and I am trying to build your project(this is my first time working with Arduino), but I can't figure out what other type of chassis I can use, and how I can attach the ultra sonic distance range finder. My Robotics teacher told me that I can 3D print the magician chassis but I can't get it done in 14 days. Do you have any recommendations? </p>
<p>Hello,</p><p>You can use anything! You can use a cd case and drill holes for the motors, or just tape them on it. You can use a piece of wood (not too heavy) and carve it out the way you want. You can even use a piece of cardboard, but it will look cheap and it might not be sturdy enough. 3D printing is an excellent option, but it is not necessary. </p><p>As for the ultra sonic sensor, i had the same problem. I used paper clips to mount it on the servo in my first try. It was good enough and it was not moving at all. Then, i found that metallic frame in my toolbox (it was from a servo mounting kit i think) and it was a perfect match for the sensor, so i used it. You basically need something that can be bent in a 90 degrees angle, may it be a piece of wood or a piece of metal like these you use to mount a desk in the wall. Be creative!!</p><p>Hope i helped!! </p>
<p>Thanks. I am 3D printing parts of the magician chassis when I asked my robotics teacher by using Autodesk Inventor Profesional 2016 which is turning out well. Also, my robotics teacher said I can 3D print that metallic frame for the ultra sonic sensor but I haven't gotten to it yet because I am still on one part of the chassis. I have 12 or 13 days until I have to turn in the project. </p>
<p>Good job!! Post a picture of your project when its done!! </p>
<p>Sorry, but I ran out of time to complete it for my class. I didn't have any of the servos, I didn't find some of the wiring, I forgot to get wheels, and I had to use get my chassis cut on a router table instead. Oh, I also don't have a phone so I wasn't able to even get any pictures or something.</p>
<p>Oh, i am sorry to hear that you ran out of time. I hope you will build it eventually, it is a fun and interesting project.</p>
<p>I might be able to continue and finish it at home but I am not sure. I need to figure out what servos to use, figure out some of the wiring, and everything I put in my last comment. I also will need to figure out if my Robotics teacher will let me use the parts that are available in his classroom. I will let you know later if I will try to continue building this project. Oh, I just got a phone from my sister yesterday(I get to keep it) so I might be able to get some pictures of what I have so far later. </p>
<p>Nice to hear that you may continue this project. Good luck and if you need help don't hesitate to post!</p>
<p>Actually, my robotics teacher is allowing me to try and finish it up by the end of this month in class. I now have more time to finish it. I still need to get wheels, a something to hold the ultrasonic distance sensor, and servos (which might not be the same ones you used). Oh, and do you have a schematic or something for the wiring so I can have it looking nice like your update from February? </p>
There is a Fritzing schematic in the instructable that explains the wiring. Haven't you seen it?
<p>Actually, my robotics teacher is allowing me to try and finish it up by the end of this month in class. I now have more time to finish it. I still need to get wheels, a something to hold the ultrasonic distance sensor, and servos (which might not be the same ones you used). Oh, and do you have a schematic or something for the wiring so I can have it looking nice like your update from February? </p>
<p>Hey everybody! If you happened to purchase Parallax brand servos, here is a link to test code and calibration code.</p><p>learn.parallax.com/KickStart/900-00008</p>
<p>Nice!</p>
<p>Hey Vagelis! I tried to build your robot, although when I ran the setup and the loop code, only one servo spun than slowly dies while my micro generic servo only pans right and locks up. I double checked all the circuits according to your schematic and cleared the Arduino multiple times and reloaded the code. We are using two 9v batteries for power. Please help! The project is due Wednesday, December 2. Thanks!</p>
<p>Hello there,</p><p>Use 4 x 1.5V batteries for the Arduino and 6 x 1.5V or 1 x 9V battery for the servos. Power them separately. </p><p>Also, check your grounds, someone sent me a message with a similar problem and he hadn't connected the grounds properly.</p>
<p>Hey Vagelis! </p><p style="margin-left: 20.0px;">Just wanted to update you on my progress. We found a bad Servo so we got the &quot;broken&quot; servos back out and found a good Servo. We are going to run tests for the sensors and see if they are working.</p>
<p>I also changed out all of the servos to eliminate the possibility of the servos being broken. Thank you.</p>
<p>Hello! Thank you so much for commenting quickly! The Arduino official site says to power the Arduino with anything between 7 and 12 volts, so we used one 9v for the Arduino and a separate one for the servos. I am checking the grounds now.</p>
<p>my project is due by !st december</p>
Good luck!
<p>can you please help me i didnt get servo motors so i bought gear motors plese giv me the instructions how to connect the motors</p>
<p>Hi,</p><p>Look a little down in the comments and you will see ThiagoG1 that used dc motors like yours. He put some code, so you can copy it and see if it works.</p>
<p>Vagelis, nice robot</p><p>I am trying to replicate your design but my LDR's are not working with code.</p><p>I am using 0-10k ohm LDR's. Could you please specify what the ohmic value yours are, or should I change the 10k resistor to a different value.</p>
<p>Thanks! </p><p>I am using the same LDRs as you. What do you mean by not working? They don't respond at all or they behave like crazy? Changing the resistor or even the LDR itself might solve your problem.</p>
<p>they do not respond at all.</p>
<p>I will try changing them out after verifying I have them correct in schematic.</p>
<p>They do not have polarity, so they will work anyway you connect them. They are like common resistors. Do your tests and let me know if they worked.</p>
<p>Hey there, Vagelis! Great work!</p><p>I'm making a college work based on your project, but here (Brazil), i couldn't find the Magician Chassis anywhere, everywhere that i searched was outta stock :(</p><p>I was wondering if i could use a 4WD Chassis (average dimensions: 256 x 150 x 65mm), but then i wouldn't be able to use the code that you posted, you could help me to build a code for the 4WD Chassis?</p><p>Since now, i thank you :P</p>
<p>Hey Thiago, thank you! </p><p>You can use whatever chassis you want, it is not necessary to use the magician chassis. You can use a cd case, a piece of cardboard or 3d print your own chassis. Just get creative!!</p><p>Of course, you can use a 4WD chassis but then you have 2 options. Either put the 2 continuous servos on the 4WD chassis, or you have to modify the code to work with the dc motors. The choice is yours! </p><p>I am here to help you with your choice if you need me! </p>
<p>Since is you that is saying, i believe it! :P</p><p>I'll begin the project next week, i guess, with my group. We're going with the 4WD, it is the better option that we've found over here. And we'll try to do using the 2 options that you gave to us.</p><p>But, in your pro opinion, what's the better option to follow with? It will be my second project using Arduino with this complexity. I'm kinda newbie with some things yet.</p><p>Since now, i thank you!</p>
<p>Thanks for the kind words but i am not a pro. We are all makers, no matter our experience.</p><p>There is not a &quot;better&quot; option in my opinion. I used continuous servos because i had them at the time and also i am developing a path backtrack system now and only the servos can give me the information that i need. It would be much harder to do path backtracking with dc motors.</p><p>However, dc motors are so simple and fun! You plug and play them very easy and they are cheaper, so if you burn one, you can replace it with very low cost. If you want a simple project go with the dc motors.</p><p>The choice is yours!! </p><p>P.S. Post a link from the chassis you are going to use to see it.</p>
<p>Hahaha, I'm becoming your fan with every reply you give.</p><p>So, this afternoon, my group and i, reunited to discuss some things about the project, and we decided to keep on with this 2WD model: <a href="http://www.arduinolandia.com.br/kit-chassi-smart-car-2-rodas," rel="nofollow">http://www.arduinolandia.com.br/kit-chassi-smart-c...</a> because of the cost, and that this one got what we need.</p><p>And about the 4WD model that i told you before, would be one of these: <a href="http://www.vilartec.com.br/product.php?id_product=50" rel="nofollow">http://www.vilartec.com.br/product.php?id_product=...</a> or <a href="http://www.arduinolandia.com.br/kit-chassi-robo-para-arduino-4-rodas." rel="nofollow">http://www.arduinolandia.com.br/kit-chassi-robo-pa...</a></p><p>What do you think?</p>
<p>The links from arduinolandia.com do not open, can you send them again??</p><p>The 4WD model from vilartec.com is very nice. It is like the magician but with 4 wheels. I really like it. </p>
<p>Oh, the dot and the comma were added to the link. http://www.arduinolandia.com.br/kit-chassi-smart-car-2-rodas and http://www.arduinolandia.com.br/kit-chassi-robo-para-arduino-4-rodas</p>
<p>Perfect. It is the same as the magician chassis without the upper deck. </p><p>Enjoy your project!!</p>
<p>Thank you, Vagelis!</p><p>When we start to build the whole thing, i'll look for some of your tips again. I wont leave you alone for a good time... hahahah</p><p>Cya, Vagelis, and again, thank you very much. :P</p>
<p>I am here for everything you may need. </p><p>Keep making!</p>

About This Instructable

23,252views

289favorites

License:

More by Vagelis Chantzis:Arduino Useless Box Light Seeking and Obstacle Avoiding Robot 
Add instructable to: