top of page

How to Build a Pet Robot V.1



Welcome to your first simple robot Tutorial!

You should have gone through the lessons here on the basics of electronics and robotics, if not make sure you do because those fundamentals and concepts make this lesson a lot easier to understand.

This easy simple robot uses recyclable materials and some electronic components.

It is based on the simple concept of moving a servo clockwise and counterclockwise to make an arm move.  The code is simple but utilizes Pulse Width Modulation, PWM, which is difficult for beginners to understand. If you need to review PWM, go ahead and do that now.

The build will use a battery pack and an ON/OFF switch to save power when not in use.


  • Servo rotates according to code in loop (Max ~180◦ both ways.)

  • Servo Arm pushes platform up and down.

  • Spring toy on platform shakes head around.



as cheap as possible. Used materials for container, minimal components, ON/OFF switch, hot glue to hold it all together.


Pet Robot Code 1.PNG

We can predefine the values we need to turn the servo clockwise and counterclockwise.  We give them these values which match the resolution that our prescaler will give the timer attached to our servo pin.


We make up a name for our servo timer. The atmega328p chip has two 8 bit timers called Timer0 and Timer2 and one 16 bit timer called Timer1. We use the 16 bit timer and set it to fast PWM mode so we can communicate as clearly and accurately as possible with the servo.

We set the prescaler to divide the chip's internal clock by 8.

We set a maximum value using the ICR1 slot in the chip's timer1 as the frequency of the servo.

This allows us to communicate at a speed and frequency that the servo does, 20milliseconds.  Without the prescaler this value would be different.

When Timer1 counts through up to the value we set in ICR1 it will trigger a PWM electrical pulse out on OC1A which is connected to the already PWM enabled PB1.

We then make up a name for the actual movement the servo will do.

PB1 uses values stored in OCR1A in order to send PWM pulses. Here we can set the actual number value to turn the servo clockwise 2000 or counter clockwise 1000 and even to the center using 1500.  You can see here that we can also use the names we gave these values which we defined at the top of the code. You can see I use both to show that it is your choice which to use.


The servo interprets these pulses via its own chip inside the motor and moves the servo accordingly.

Pet Robot Code 2.PNG

Next, we set the stage for our code's performance in MAIN by setting the servo to the center, you don't have to I just decided to do this.

We call our 16 bit Timer1 we outlined a minute ago by writing the name we gave it and including the empty ( ) followed by a semicolon which marks the end of statements in C code so get used to it.

Next, We go into Data Direction Register B (DDRB) which is the street that all of the house numbers that start with PB live on (PB0, PB1, PB2...etc.) and we bit shift the PB0 pin. In other words the PB0 pin in the DDRB block is set to output by turning the bit inside the chip from 0 to 1. 0 means off(input) 1 means ON(Output). This sets the pin up to allow electrical pulse to shoot out from it.

Last comes our LOOP.  You can see we call on the actual movement of the servo which we gave a name to followed by the ( );

We also go tell PORTB which is inside our DDRB block of pins and tell PB0 to turn on by itself.

No other PB pins will be turned on.

When the power is turned on via the power switch the battery's electricity will flow into the atmega328p chip and go through MAIN once.

After that it will go through the LOOP and when it reaches the end of the LOOP it will loop back to the beginning of the LOOP and repeat until the end of time.





  • Putting the pet robot together inside the case was challenging.

  • Hot glue worked fantastically but was overused. Would be nice to use less glue and more screws etc for a cleaner build and look.

  • Soldering the board was much easier using the 22 Gauge solid core, get some!

  • After dealing with faulty AA battery packs and their flimsy, cheap wires we will be using rechargeable Li-ion and Li-po batteries for future builds. Solar panels would also be a good addition for future robots.

  • Photoresistors to detect light blocked by hand movements would add more character to the pet robot

  • Buttons to interact with the pet robot would make it more interesting (behavior modes to switch between)


ATMEGA328P Microcontroller Pin Diagram

bottom of page