Introduction: Artoo [R2] (ATtiny2313 Wall Avoiding Robot)

Picture of Artoo [R2] (ATtiny2313 Wall Avoiding Robot)
Since I had a lot of ATtiny2313s lying around I wanted to do something cool with them, I thought for a while and then decided to make a simple wall avoiding robot with the ATtiny2313, an ultrasonic sensor and 2 motors.

Its an easy to make and simple robot which runs on a very small amount of code so as to support and work with the ATtiny2313.

For those of you out there who want to make a small robot with the Arduino processing, but you are not able to as the Arduino is too big for a small robot, then this is the perfect robot for you!

Since the ATtiny2313 chip is much smaller compared to the Arduino, it fits in almost anywhere, which allows you to make a small simple robot which evades walls.


Note: This is by no means related to the Star Wars charecter R2-D2, its just my own robot :)

Step 1: Materials/Tools Required

Picture of Materials/Tools Required
Materials Needed;
  1. ATtiny2313
  2. Arduino (For programming the ATtiny2313)
  3. 2 Motors (Gear or normal, both seem to work for me)
  4. 2 NPN Transistors
  5. 2 10MicroFarad Capacitors
  6. 1 5V Voltage Regulator
  7. 2 9V batteries (Of 250~330mA current capacity each)
  8. 1 Ultrasonic Range Finder
  9. A piece of Protoboard
  10. A few headers (Male)
  11. 2 Wheels for the motors or some bottle caps might work
  12. 1 Wheel which is attached to an axis (For attaching it on the back of the robot to give it support)
  13. A few jumper cables or just some plain wire
  14. An ATtiny2313 Programming shield (Optional)(
  15. 2 9V battery cases or connectors.
  16. A switch

Tools Needed;
  1. Soldering Iron
  2. Solder
  3. Soldering Flux
  4. Glue Gun
  5. Small cutting knife
  6. Scissors

Step 2: Adding the Voltage Regulator to the Battery Holder

Picture of Adding the Voltage Regulator to the Battery Holder

Depending on which 5V voltage regulator you are using, wire it up to the battery case using its 9V and Gnd lines, so that you can take out a 5V and another Gnd line.

These 2 (5V and Gnd) wires will be used to power the ATtiny2313.

Now connect the 5V wire to a switch, so that you can turn on and off the robot as required.

For most 5V voltage regulator, the middle pin is Gnd, the left is Input and right is output, so you can wire it accordingly to get the required Voltage output.

Note: Beware on the voltage regulator you use as a lot of heat is generated by them, and its usually a good idea to add a heat sink to it if you have got one.
(Heat Generated = (Vin-Vout)Iout Joules), Iout is the current output or used by the circuit.
Using the formula, for my circuit I generated about 1kJ so I got away without a heat-sink but if its more than that then you need to be sure to add a heat-sink.

Step 3: Connecting the Ultrasonic Sensor

Picture of Connecting the Ultrasonic Sensor

The Ultrasonic sensor has 5 pins on it whereas the Ping Sensor has only 3 Pins.

In the Ping Sensor, you only have 5V, Gnd and Signal pins where the Signal pin is connected to the arduino to tell us the distance of the object in front of it.

The Ultrasonic Sensor however has 5 pins out of which 4 are used, the 5V and Gnd pins are in common, but instead of the Signal pin you have the Trigger and Echo pins.

Therefore, we have to use these 2 pins in the coding instead of just a Signal pin, so the coding for the Ping Sensor and the Ultrasonic Sensor are different.

In this robot I used an Ultrasonic Sensor as they are usually cheaper than Ping Sensors, I got mine for 9$ at Ebay.

Now, you connect the 5V and Gnd pins of the Ultrasonic Sensor to the 5V and Gnd wires that you got from the battery before using the 5V Voltage Regulator. The Trig pin connects to pin 14 (D11) of the ATtiny2313 and the Echo pin connects to pin 13 (D10) of the ATtiny2313.

Step 4: Connecting the Motors and Transistors

Picture of Connecting the Motors and Transistors

The Motors are connected to 1 NPN transistor each so that the ATtiny2313 can control when they turn on and off even when they are based on a 9V circuit and not dependent on the power the ATtiny2313 I/O pins provide. (As the ATtiny2313 pins do not provide enough power to make the motors work)

The Transistors are connected in series with another 9V battery which does not have a 5V Voltage Regulator as the motors I used work off a 9V operating voltage. So I used a 2nd 9V battery to power the motors independent of the rest of the circuit to ensure they get all the power they need to work.

The wiring schematic for this is included in the pictures.

Step 5: Connecting the ATtiny2313 to the Rest of the Circuit

Picture of Connecting the ATtiny2313 to the Rest of the Circuit

Pin 10 of the ATtiny2313 is the Ground pin of the board, so it is connected to the Ground that we get from the battery after it has been Voltage Regulated.

Pin 20 of the ATtiny2313 is the Voltage pin where we connect the 5V output from the Voltage regulator.

We also need to connect pins 9(D7) and 12(D9) to the center pins of the transistors. (I did not need a resistor as my transistor specifier did not mention any but for majority of transistors we need to add a 1K Ohm resistor to the center pin which then connects to the ATtiny2313)

After doing this you should only have 6 pins on the ATtiny2313 connected and the rest need not be used for this project.
(2 LEDs can be added to 2 pins on the ATtiny2313 if you want to and they can be powered directly by the I/O pins)

Step 6: Assembly of the Robot

Picture of Assembly of the Robot

The 9V battery case will act as the body of the robot.

Attach the 2 motors on either side of the battery case. Attach the Ultrasonic Sensor on top of the battery case and then finally attach the wheel with the axis on the back of the battery case (As shown in the images).

As you can see in the images, I first used small non gear motors but they didn't work very well and the bot moved quite slowly, so I replaced them with 2 gear motors which run on a very low current (The yellow motors).

The 2nd 9V battery used to power the motors can either be stuck on the front with some hot glue or just left trailing behind.

I decided to leave mine trailing as I attached a small cardboard box around it making it look like a suitcase and the robot has just shifted its home and is searching for a new area to live with its luggage :D

Step 7: Coding the ATtiny2313

Picture of Coding the ATtiny2313
To code the ATtiny2313 you can either wire it up manually or use a simple shield that you can build, instructions are given in this instructable:

If you already know how to upload code onto the ATtiny2313 then you are set to go if not, then have a look at the above instructable and you will be ready to go.

Make sure you add the Ultrasonic library to the code before uploading the code or it will not work.

This is the code used by the Artoo[R2]:

#include <Ultrasonic.h>

int MOTOR = 9;  // RELAY Pin.
int MOTOR2 = 7;
int TRIG = 11; // Trigger Pin
int ECHO = 10; // Echo Pin
int Range; // The range of the object from Ping Sensor
int Dist; // The Distance value

Ultrasonic ultrasonic(TRIG,ECHO); // Create and initialize the Ultrasonic object.

void setup() {
  pinMode(MOTOR, OUTPUT); //To the relay via the transistor
  pinMode(MOTOR2, OUTPUT);
  Dist = 10; //The distance in inches. Change this for increasted or dicreasted range.

void loop() {
  static int sensorCount = 0;

  Range = ultrasonic.Ranging(INC); // Range is calculated in Inches.
  if (Range > Dist) {
    digitalWrite(MOTOR, HIGH);
    digitalWrite(MOTOR2, HIGH);
    sensorCount = 0; 
  } else {
    if (sensorCount == 5)      // turn if we sense a wall 5 times in a row
      digitalWrite(MOTOR, LOW);
      digitalWrite(MOTOR2, HIGH);
      sensorCount = 0;
       delay(20);   // wait 20ms before testing the range again

You might say that I am leaving MOTOR2 always on so why not just connect it to the battery directly instead of wasting a transistor and using up more memory of the ATtiny2313, but the reason I did that was because if you connect it directly, the amount of current it gets is for some reason more than it does when its added to a transistor, so to make it move at the same speed as MOTOR1, I attached it to another digital pin.

The sensorCount function makes sure that after the sensor has "sensed" 5 times only then the if function is executed to make the robot turn. Each sense count has a duration gap of 20ms.

Another reason why I couldn't do anything more precise and better with the code was because the ATtiny2313 has a limited data of 2k, and anything more than that will not be accepted by the ATtiny2313 so make sure that your code is a small one.

Step 8: Explanation of the Code

Picture of Explanation of the Code

#include <Ultrasonic.h>
This adds the ultrasonic.h library to the sketch

int MOTOR = 9;  // RELAY Pin. This is the pin where the motor1 is connected
int MOTOR2 = 7; This is the pin where the motor2 is connected
int TRIG = 11; // Trigger Pin, This is the Trigger pin from the ultrasonic sensor
int ECHO = 10; // Echo Pin, This is the echo pin from the ultrasonic sensor
int Range; // The range of the object from Ping Sensor
int Dist; // The Distance value, The amount of inches you set before which the motor turns ie detects the wall

Ultrasonic ultrasonic(TRIG,ECHO); // Create and initialize the Ultrasonic object.

void setup() {
  pinMode(MOTOR, OUTPUT); This is setting the 1st motor as an output
  pinMode(MOTOR2, OUTPUT); This is setting the 2nd motor as an output
  Dist = 10; //The distance in inches. Change this for increased or decreased range.

void loop() {
  static int sensorCount = 0; This basically counts the sensor input to 0 initially

  Range = ultrasonic.Ranging(INC); // Range is calculated in Inches.

  if (Range > Dist) {
    digitalWrite(MOTOR, HIGH);
    digitalWrite(MOTOR2, HIGH);
    sensorCount = 0;
  } else {
    if (sensorCount = 5)    If the range is not > distance then the sensor checks the range 5 times in a row with a gap of 20ms and if they are all showing that range < distance, then the else function is executed which turns one motor off so that the robot changes direction
      digitalWrite(MOTOR, LOW);
      digitalWrite(MOTOR2, HIGH);
      sensorCount = 0; Set the initial value of sensor readings back to 0
       delay(20);   // wait 20ms before the sensor senses again in order to get 5 values of range < dist to stop motor1

Step 9: Finishing Up and Casing

Picture of Finishing Up and Casing

To make sure all the connections are secure and there are no loose connections, you can add heat-shrink tubing wherever needed.

For the case, I decided to just block up some parts with cardboard and leave the rest open and then get some prints and stick them on there.

If you have a 3D printer, then you can design a case and print it for it, but unfortunately I don't :( so I will just have to leave it with the cardboard.

If you make any cool cases or if you make this robot yourself, post pictures in the comments! I would like to see what kinds of cool things others made!

Step 10: Further Thoughts and How to Improve

Picture of Further Thoughts and How to Improve
Since there were a lot of pins left, after finishing the project, if I got a chance to do it again, I would add 2 LEDs and make them the robots eyes and then a micro servo as I figured out that the 2 batteries had enough juice to power a servo along with 2 motors.

I would also attach a small laser on top of the servo to make it point at stuff and examine walls and obstacles it comes across
(Going all Sci-Fi here :D)

One last thing I would do is change these 9V batteries with a very light weight toy helicopter battery as it gives a 7V with about 650mAh which is almost equivalent with the 2 9V batteries but its much lighter than these are.

An Improvement I did was I added the battery which was trailing behind to the motors in front so that it doesn't sweep the floor all the time, as you can see in the pictures.



WalidM (author)2016-02-28

Wow... nice..........

Marethno (author)2016-01-17

Is This still Alive??? i like to buil this bot, cause i m a having a bunch of attinys2313 here! i would like to know which transistors u used? npn yes , can i use BC 337? i will try it anyway!

Mic100 (author)2015-09-13

Wall-E demo walks and avoid the walls

Mic100 (author)2015-06-08

Good Job Thank You :)

I made a small ATTINY13A soicavec with a led infraeouge to avoid obstacles you can see a video at the end of this instructable:

miniscientist (author)2014-06-23

can you please send me the library for mac

Maki_mx (author)2014-06-10

Thanks for clarifiying the connections :), yet the object I am triying to figure out is the one after the voltage regulator. Is it another button?

vishalapr (author)Maki_mx2014-06-11

Yes its a switch :D!

Maki_mx (author)2014-06-09


I am having a very hard time matching the information with the actual schematic. Any tip? Is it right? (I really dont mean to be disrespectful)

Another question: What objet it is from left to right.

Thank you! :)

vishalapr (author)Maki_mx2014-06-10

Can you tell me which parts are hard to understand? The schematic is correct as far as I remember so go ahead with the schematic. Im not sure what the 2nd question means?

Maki_mx (author)vishalapr2014-06-10

Here, for example, you say "9 and 7 to the center pins of the transistors", but that is were the sensor is connected

Sorry, my question was not clear, What is the object in the schematic going from left to right?

vishalapr (author)Maki_mx2014-06-10

If you make this robot be sure to post a pic of it here! The object in the schematic is the ultrasonic sensor if you are talking about the small black square.

vishalapr (author)Maki_mx2014-06-10

Im sorry for the confusion I should have stuck to the pins on the attiny itself but I kept swapping between the attiny pins and the digital pins, I said digital pins 9 and 7 to the center pins of the transistors which are pins 9 and 12 on the attiny2313, have a look at this image.

rvyas3 (author)2014-01-06

there is an error in code for Ultrasonic ultrasonic(TRIG,ECHO); // Create and initialize the Ultrasonic object.line. i am making a same robot but i can't because of this code. all i need is a code.

vishalapr (author)rvyas32014-05-31

What does the error say?

Im not really sure because the code worked perfectly for me

harishqwe (author)2013-07-07

can any 1 give me the full code for attiny 2313 with arduino ??

vishalapr (author)harishqwe2013-07-09

Its on step number 6

vishalapr (author)vishalapr2013-07-09


ybsocurious (author)2013-07-08

can you give me your email? i have a problem. thanks

vishalapr (author)ybsocurious2013-07-09

I don't use my email that much, you can send me a message on instructables or just post the problem in the comments here and I can help solve it as it would help others with the same problem as well :)

Edgar (author)2013-06-13

With a Hemispherical Hull, this could make a cool Cat Toy, methinks... :D
Voted, and went to the Blog:

vishalapr (author)Edgar2013-06-13

Thanks! Yea It could act as a cat toy if the hull was spherical and was made of plastic instead of cardboard, as its a lot stronger, but unfortunately I don't have any pets :(

Edgar (author)vishalapr2013-06-14

Lots of people do, and so, you can use some friend's cat for the experiment.

vishalapr (author)Edgar2013-06-14

yea I know friends who have cats ;) I will be sure to try it!

alcurb (author)2013-06-06

Nice design. Have you considered making it self-balancing? I'm not sure if one could make such a tiny self-balancing robot.

vishalapr (author)alcurb2013-06-07

Do you mean like a different self balancing robot or modifying this to make it self balancing? Thanks alcurb!

alcurb (author)vishalapr2013-06-07

Either. I just wonder if a robot of the same height can self balance.

vishalapr (author)alcurb2013-06-08

Hmm it might be possible...but I'm not really good with all the weights and measurements and stuff as I haven't got good tools to make very precise and accurate things, if I get a couple of tools and I get to use wood, then the first thing I'm going to make will be a quadrocopter as right now no matter what I do its always off balance or too heavy or too weak etc....and this might be something I can experiment with as well :)

mprzymusiński (author)2013-06-07

haha nice tail! it looks cute ;)

vishalapr (author)mprzymusiński2013-06-07

Thanks! ;)

Transmisso (author)2013-06-04

This project is so simple to do and the results seem amazing!

vishalapr (author)Transmisso2013-06-05

Thanks Transmisso!

bajablue (author)2013-06-04

A fabulous Contest trifecta!!! Excellent work, vishalapr!!!

vishalapr (author)bajablue2013-06-05

Thanks! Its awesome u liked it!
(I had to google trifecta btw XD)

cdragos george (author)2013-06-03

simple robot that can be a good example for students who want to get into robotics

vishalapr (author)cdragos george2013-06-04

Thanks! Glad you liked it

hisashime (author)2013-06-02

Just love following with your projects. Makes life easy with minimal parts.

vishalapr (author)hisashime2013-06-03

That's awesome :)
Glad you like my projects!
The reason why I barely use any complicated tools or parts is because I don't have any tools apart from a glue gun, soldering iron and the leather-man.

Helicopterz (author)2013-06-02

Great project and documentation, may I ask why you swapped the original motors though and may I also know what company the batteries are of because all the batteries 9V (rechargeable and Non-rechargeable never had 330mAh capacity)

vishalapr (author)Helicopterz2013-06-02

Glad you liked the project Helicopterz ;)

The battery I used was similar to this one:

I swapped the original motors with the geared ones as the original ones weren't able to move the robot at a good speed, it moved very slowly, but with these new motors, it moves at a nice speed when the batteries are fully charged!

RRoobboott (author)2013-06-02

That looks so cute :D, I am planning on making this and do you know any place online where I can buy the ATtiny2313 chips in bulk? like maybe 20 or 30,

vishalapr (author)RRoobboott2013-06-02

You should try checking on ebay as I remember seeing many sellers sell 1 chip for 2.5-3.5$ and in bulk (10 or more) for about 1.8-2.5$ each, so it would be a good deal

Once you make this be sure to upload pics of it :)

Lensquare (author)2013-06-01

Such a great design! Thanks you for the contribution! Where would you recommend to buy the parts that are needed? Online?

vishalapr (author)Lensquare 2013-06-01

For the ATtiny2313 however I would recommend this:

If you make this post pictures of it :D

vishalapr (author)Lensquare 2013-06-01

Glad you liked it! I bought all the parts I used for it from Ebay or if you have got an electronics store nearby then that would be fine as well.

freepancho (author)2013-06-01

You should totally make either a sled or cart for the battery!

vishalapr (author)freepancho2013-06-01

Actually a cart for it would be quite cool as Kiteman suggested but for some reason I was reluctant on leaving the battery on the floor as its kind of dangeorus so I decided to just place it in the front, but yea the cart would make it look as if the robot was carrying its luggage :)

fadnessmad (author)2013-06-01

Another Project for me to make in my spare time :D
Great pictures!

vishalapr (author)fadnessmad2013-06-01

Thx! Post pictures of it once you are done :D

alienswamp (author)2013-06-01

That tiny thing is awesome, I think it will even fit in my cargo pants to carry around!

vishalapr (author)alienswamp2013-06-01

Its portable all right :D, it fits in my palm and its super easy to carry around
Glad you liked it!

About This Instructable




Bio: A programming enthusiast. I tinker around with everything I see and love to game.
More by vishalapr:Diffused Glue Stick LampATtiny85 POV DisplayArtoo [R2] (ATtiny2313 Wall Avoiding Robot)
Add instructable to: