Randomly Controlled Laser Pointer Cat Toy

by Henkeify in Circuits > Raspberry Pi

567 Views, 9 Favorites, 0 Comments

Randomly Controlled Laser Pointer Cat Toy

title.png

Ever since I learned that you can buy laser diodes for next to nothing on the internet I have wanted to use them in a project. Last year we got a cat called Waffles (actually he's called VĂ„fflan which translates to the waffle but that doesn't really roll of the tongue) so I thought I wanted to make a laser pointer toy for him using servos and a microcontroller.

This project is really simple and requires no soldering iron nor any 3D-printed parts. You need no prior knowledge of the raspberry pi pico or programming, just a bit of curiosity and a glue gun!

Supplies

Supplies:

  • Raspberry pi pico (get one with headers already soldered on if you can) Like this
  • Micro USB cable (For power and serial communication) Like this
  • Jumper cables Like these
  • Breadboard Like these (you only need 1)
  • Laser diode Like these (you only need 1)
  • Micro servos Like these (you will need 2 motors in total)
  • Something to attach the motors to, I used a scrap piece of wood.

Tools:

  • Hot glue gun

Connect Pico to Computer

1.jpg
2.jpg

Connect the USB cable to a USB-port of your computer and the other one to your pico. The computer should recognise this as a storage unit and open a file explorer window showing the content of the unit. It will contain two files.

The index file is a link which will take you to the raspberry pi homepage where we will need to go to download and mount MicroPython to the pico.

The INFO_UF2 is a text document containing information of your unit.

Double click the index link (make sure you are connected to the internet) and you will be greeted at the raspberry pi homepage. Click on Micropython box up to the right and then the link raspberry pi pico (unless you have a pico W, the you should click that one).

This will download the MicroPython image, when that is in your downloads folder just drag it to the folder where you had the index and info. The pico will sort the rest out and disconnect itself once MicroPython is installed.

Install Thonny Python Editor

Go to thonny.org and download the code editor. Follow the instructions and open it once it is installed.

If it says Python in the lower right corner you need to click it once and the choose Micropython instead. You may be prompted with instructions on how to install micropython but you have already done that so just click cancel.

Now you should be able to write code and then the pico will run it!

Try something easy like

print("Hello World, from a pico!")

And then save the file. You will be prompted with the choice to save it to either this computer or the pico. Choose the pico and save it as main.py. This will make sure your script starts when the pico gets powered on.

You can then click on the green play button and the script will run and greet you with the phrase you just wrote.


Connect Motors

6.jpg

Depending on the motors you use they may have different color schemas but the ones I use have the following (which is the most common I think)

Brown - GND

Red - Vin

Orange - PWM signal

You may want to use a breadboard and the connect the motor to the pico according to the image, with the signal wires on 2 and 4 on the pico.

Before we glue the motors together we will check if they are functioning as they should.


Coding

Copy this code and paste it into your thonny editor.

from time import sleep
from machine import Pin, PWM

pwm1 = PWM(Pin(1))
pwm2 = PWM(Pin(2))
pwm1.freq(50)
pwm2.freq(50)

pwm1.duty_u16(5000) #5000 corrresponds to the motors middle value, 1000 is 0deg and 9000 corrresponds to 180deg.
pwm2.duty_u16(5000)

It only checks if the connection is made to the motors and if so, sets them to 90 deg (which is a good default).

Save the code as anything.py and then click the green play button.

If it works you can go ahead and assemble the tower, but first you can save the real script but don't run it just yet. Copy this code and save it as main.py on the pico.

from time import sleep
from machine import Pin, PWM
import random

pwm1 = PWM(Pin(1))
pwm2 = PWM(Pin(2))
pwm1.freq(50)
pwm2.freq(50)

pwm1.duty_u16(4500)
pwm2.duty_u16(4500)


half_range = 1000
lower_range = 5000 - half_range
upper_range = 5000 + half_range
while True:
    number1 = random.randrange(lower_range,upper_range,50)
    number2 = random.randrange(lower_range,upper_range,50)
    print(number1)
    print(number2)
    pwm1.duty_u16(number1)
    pwm2.duty_u16(number2)
    sleep(1)

The line half_range determines how much from the middle the motor will turn, if you have a higher value the motors will point the laser all over over the room. If you have a smaller value (like 1000), the laser will stay within reachable distance from your cat.


Assemble

IMG_5780.jpg
IMG_5783.jpg
laser diode.jpg
7.jpg

Start by having some base plate, I used a bit of wood. Place a dollop of hot glue on it, near the edge and the place a motor in it, with the shaft pointing upwards.

Place another dollop of glue on the wing of the first motor and then place the second motor on it sideways, with this you have achieved motion in two degrees of freedom for your robot.

Glue the laser diode to the wing of the second motor and power it either using a battery or by connecting the red wire of the diode to 5V on the breadboard and the black wire to the GND of the breadboard.

Adjust Tilt and Runt the Program

I had to tilt the board a bit so the laser could reach the floor OK but then you should be good to go and click play on that script!

Introduce it to your cat and hopefully it will find it amusing for hours (no guarantee though, our cat usually get bored really quick).


Thanks for reading and please leave a question or comment below if you have any.