About two years ago, I’ve built this toy for my dog where a laser is being controlled with two servos to give it a random movement where the laser dot escapes and he can chase it.
The laser worked perfectly but during my recent move it got damaged so in this Instructable we’ll fix it and I’ll explain the principles of operation.
Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.
Step 1: Materials
The project consists of 4 parts: an Arduino board, 2 micro servos and a laser module. They are all housed in a plastic container from sour cream where the Arduino board is glued to the bottom while the servos and the laser are on the top.
Below is a list of buying links where you can get what you need:
Step 2: Construction
Previously I had the servos glued to the lid with some hot glue but it didn’t hold up. To fix this, I’ll use some double sided foam tape as this proved to be very durable where I’ve used it. The alignment on the lid is not that critical as it can always be adjusted by rotating.
The other servo is directly attached to the first one so they can together move the laser in two directions. The first one moves it left and right, while the second one moves it up and down. The laser module is attached to the arm of the second servo with some hot glue that seems to be still holding up.
Step 3: Wiring
The electrical wiring of the project is relatively easy. Servo motors have three wires: power, ground, and signal. The power wire is typically red, and should be connected to the 5V pin on the Arduino board. The ground wire is typically black or brown and should be connected to a ground pin on the Arduino board. The signal pin is typically yellow, orange or white and should be connected to a digital pin on the Arduino board. In our case we use digital pins 9 and 11.
Note that servos draw considerable power, so if you need to drive more than one or two, you'll probably need to power them from a separate supply and not the +5V pin on your Arduino.
The laser module has three pins but only two of them are being used. The one on the left side is the positive connection and it is typically marked with uppercase S, while the negative connection is on the right and it is marked with a minus. You can get the link to the full schematic in the video description.
The full schematic is available on EasyEda:
Step 4: Principles of Operation
Now that the repair is done, let’s have a look at the code and how this device operates. A typical 9g micro servo has a movement of 180 degrees and it is controlled by a PWM signal. This signal is generated from the Arduino with the help of the Servo library.
The library provides a convenient method called “write” that writes a value to the servo, controlling the shaft accordingly. On a standard servo, this will set the angle of the shaft (in degrees), moving the shaft to that orientation.
In our case we want to limit this movement to a maximum of around 50 to 70 degrees in each direction so the area where the dog runs is not to big so he does not get tired too soon. Additionally, I’ve used these limits to set the perimeter of the laser so it does not exit the area that my dog has to move. They are being set at the beginning of the sketch together with the definitions for the two servos and the variables that we gonna use for their position.
Step 5: Arduino Code
In the setup function, we first initialize the servos and attach them to the correct pins on the Arduino. This will tell the library to output the PWM signal on that pin.
Next we write the initial position to both servos and for us to be able to properly place the toy, I’ve added some code that will move the servos in a circular pattern for 3 times on the edges of the limit that is set. This way you can see where the dot moves and adjust the placing of the toy accordingly.
In the loop function, we first generate two numbers, representing the next angles of the two servos and we provide the limits that we previously set. The random function in Arduino, can accept the minimum and the maximum value that it needs to generate. This values are then sent to the servos, one at a time, with a delay of half a second in between.
The full code can be found on my GitHub page: