Introduction: Light Seeking and Obstacle Avoiding Robot

Picture of Light Seeking and Obstacle Avoiding Robot

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

Picture of List of Components and Tools


  • 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)


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

Step 2: Assembling the Chassis and Motors

Picture of 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

Picture of 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

Picture of 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

Picture of 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.


Servo leftMotor;
Servo rightMotor;

void setup() {



void loop() {




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



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


SensorRight = 1023 - analogRead(RightLightSensor);


SensorDifference = abs(SensorLeft - SensorRight);

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


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





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





else if (SensorDifference < 75) { //forward






else //if path is blocked {





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




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


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





void compareDistance() {

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



rightMotor.write(RForward); //turn left



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



rightMotor.write(RBackward); //turn right



else //if they are equally obstructed



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




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

digitalWrite(trigPin, LOW);


digitalWrite(trigPin, HIGH);


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:

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

Keep coding!!

Step 9: Update -February 27 2015-

Picture of 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!


sid_g95 (author)2017-09-17

I was just wondering what voltage your capacitors were

Vagelis Chantzis (author)2017-02-02

Click the link in the code step, go to the GitHub page and download the code from there. The file name is Light Seeking and Obstacle Avoiding Robot.ino

Vagelis Chantzis (author)2017-01-25


KhueD1 (author)2016-11-26

Where do you buy the plastic servo holders?

Vagelis Chantzis (author)KhueD12016-11-27

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!

KhueD1 (author)Vagelis Chantzis2017-01-04

Thanks so much im gonna see what I can do

KhueD1 (author)KhueD12017-01-04

Can you give me a link to the website or item?

Vagelis Chantzis (author)KhueD12017-01-05

Here are some mounting brackets. Choose whatever you like.

NinjaG2 (author)2016-03-28

the code not working help me please

Vagelis Chantzis (author)NinjaG22016-11-27

What part of code is not working exactly?

SardoC (author)2016-04-13

Hey Vagelis, could you please explain me me why you put 75 as value for the sensor difference. Thanks.

Vagelis Chantzis (author)SardoC2016-11-27

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.

wchowe (author)2016-01-18

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!

Vagelis Chantzis (author)wchowe2016-01-24

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!!

wchowe (author)2016-01-18

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!

wchowe (author)2016-01-18

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!

HIandKABOOM (author)2015-11-03

Vagelis, Great work!

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?


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.

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!!

Hope i helped!!

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.

Good job!! Post a picture of your project when its done!!

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.

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.

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.

Nice to hear that you may continue this project. Good luck and if you need help don't hesitate to post!

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?

There is a Fritzing schematic in the instructable that explains the wiring. Haven't you seen it?

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?

wchowe (author)2015-11-29

Hey everybody! If you happened to purchase Parallax brand servos, here is a link to test code and calibration code.

Vagelis Chantzis (author)wchowe2015-11-29


wchowe (author)2015-11-28

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!

Vagelis Chantzis (author)wchowe2015-11-28

Hello there,

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.

Also, check your grounds, someone sent me a message with a similar problem and he hadn't connected the grounds properly.

wchowe (author)Vagelis Chantzis2015-11-29

Hey Vagelis!

Just wanted to update you on my progress. We found a bad Servo so we got the "broken" servos back out and found a good Servo. We are going to run tests for the sensors and see if they are working.

wchowe (author)Vagelis Chantzis2015-11-28

I also changed out all of the servos to eliminate the possibility of the servos being broken. Thank you.

wchowe (author)Vagelis Chantzis2015-11-28

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.

DharmaS (author)2015-11-28

my project is due by !st december

Vagelis Chantzis (author)DharmaS2015-11-29

Good luck!

DharmaS (author)2015-11-28

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

Vagelis Chantzis (author)DharmaS2015-11-29


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.

dewo1988 (author)2015-11-03

Vagelis, nice robot

I am trying to replicate your design but my LDR's are not working with code.

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.


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.

they do not respond at all.

dewo1988 (author)dewo19882015-11-04

I will try changing them out after verifying I have them correct in schematic.

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.

ThiagoG1 (author)2015-06-02

Hey there, Vagelis! Great work!

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 :(

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?

Since now, i thank you :P

Hey Thiago, thank you!

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!!

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!

I am here to help you with your choice if you need me!

Since is you that is saying, i believe it! :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.

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.

Since now, i thank you!

Thanks for the kind words but i am not a pro. We are all makers, no matter our experience.

There is not a "better" 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.

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.

The choice is yours!!

P.S. Post a link from the chassis you are going to use to see it.

Hahaha, I'm becoming your fan with every reply you give.

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: because of the cost, and that this one got what we need.

And about the 4WD model that i told you before, would be one of these: or

What do you think?

The links from do not open, can you send them again??

The 4WD model from is very nice. It is like the magician but with 4 wheels. I really like it.

Oh, the dot and the comma were added to the link. and