Introduction: Smartphone Controlled Cat Laser
I've been wanting to start exploring the world of microcontrollers and programming for some time now, so I came up with this simple project which uses an app called "Blynk" to allow you to wirelessly control an Arduino board through your wifi. Now obviously the end result isn't the most practical device you'll ever build, but this was a great way for me to learn the fundamentals of programming Arduino and it can help you to learn too.
I've tried to arrange this project in an easy-to-follow format so it's ideal for someone who knows nothing about Arduino, but there's a lot of room for tweaking and improvement in my code that would allow more advanced users to get some use out of this project too.
This is a project that's been done before a few times, but I wanted to add something new by giving the system remote control functionality.
Step 1: Components Needed
This project can be completed for a very low price, I found a starter kit for Arduino that had everything you'll need for this project for £20/$25 excluding one servo.
- Any Arduino board that can support 2 servos (I believe any Arduino board will work, just check it has 2 or more PWM pins)
- 2 x Micro Servos (I used tower pros, I bought 30 for about £30)
- A 3V-5V Laser Module
- NPN Transistor
- A few lengths of jumper wire, preferably in different colours to help you distinguish them
- A smartphone
- An enclosure - I used a yoghurt pot because at the right angle it's totally disguised
- A very small breadboard would be useful for wiring if your laser is only going to be a temporary thing
- A Wifi Shield would allow you to use the laser while it's not connected to a computer, but it would also require a battery
Step 2: Wiring
The diagram I've attached should be all you need to wire up the Arduino.
The servos both need a positive and negative power supply to move, as well as a signal wire which goes into one of the digital PWM pins. The Arduino drives the servos by outputting a signal that oscillates between a high and low voltage (5V and 0V), It tells the servos the exact position they need to be in via the pulse width of the signal, meaning that the time between the signal being turned on and off corresponds to a position that the servo can move to. Arduino boards can easily do this using the Servo.h library, and all that we need to specify is the angle in degrees that the servo should move to.
The transistor is used simply because the laser may draw a lot of power depending on what module you choose. If we were to wire the positive end of the laser directly to the digital output pin on the Arduino, it may work but it also may result in that pin being overloaded with too high a current. The 3.3V pin on the Arduino goes into the collector of the transistor (I would recommend checking the datasheet of your transistor to verify which pin is which - simply search for the product code printed on the transistor and you should find it easily), and the signal pin goes into the base. The emitter is wired directly to the positive terminal of the laser module, and the negative terminal goes to GND on the Arduino board. Note that the laser module does not need a resistor like an LED would, because it already has resistors inside of it. Make sure that your laser module is actually a module, if you've got a simple laser diode it might not have a resistor factored into its design.
The Arduino will need to be plugged into the PC via the USB cable, but if you plan to use a battery then you won't need to (I'll go into this later).
Step 3: Building the Laser Tower
As you've probably already noticed, the actual tower was very simply made just by taping together two servos and the laser. I chose to go with this ultra-lightweight design over something more sturdy so that the servos wouldn't need to work as hard and could be faster and more responsive. One servo must function as the x-axis, sweeping in an arc while staying parallel to the floor. Another must be used as the y-axis, going up and down. I think most people will find this step really easy, and I'd encourage you to experiment to try different configurations, and maybe something a bit more sturdy!
I taped this tower to the lid of the yoghurt pot so that it sat upside-down, and cut a hole in the back of the pot to allow the beam to shine through. I also cut a discrete hole near the base for the Arduino to be plugged in.
Step 4: Programming and Blynk
The programming step should be very simple if you're not going to modify it in any way, simply open it in your Arduino IDE software, insert the authentication code (explained below) and upload it to your board. You'll also need to have the Blynk library installed - comprehensive instructions can be found on their website: http://www.blynk.cc/getting-started/ (it's very easy to install). You'll notice that I've annotated the code so that it's easy for you to understand how it works, I suggest following through it as you read this section so you understand better.
The program takes values from the widgets in the Blynk app and stores them as integer variables, the joystick gives 2 readings - 1 for the x-axis and another for the y-axis. The other two variables are the on/off state of the two buttons, one for the laser and the other for the automatic mode - these just output a 1 or a 0. You may want to adjust the values for the joystick - note that my values go from 200-800 on the x-axis (Pin V0) and 300-700 on the y-axis (Pin V1). If you swap the numbers, the motion of the laser will be inverted - this may be useful for when you flip the tower upside down. You could also make the numbers span over a broader range, I chose those values based on the size of the opening I wanted in my enclosure, but the values can go from 0-1023, just make sure that the halfway point between your 2 values is around 510, so that the laser rests at 90 degrees.
In the void loop() section, the program looks to see if the automatic mode button has been pressed, and if it has not, it uses the values taken from the joystick, converts them to be over a range of 0 to 180 degrees, and tells the servo to move to that angle. If the button has been pressed, the randomMode function is called. This simply finds a random number between 0 and 180 every half a second and tells the servo to move to that position. Here is an area where the code could be tweaked, it would be easy to alter the parameters of the random mode by changing the delay (and as a result the speed), or changing the range of the random values. There are a lot of options here so this mode may be a great way to experiment with coding.
The Blynk app will need to be downloaded to your phone. You should find it quite intuitive, all you need to do is to create a project with a joystick and two buttons and change the values to the ones I have in the images for this step. You'll also need to go into the settings of the app and select the option to email your auth token - simply take the token and paste it into your code right near the top (it should be clearly labelled). The only other thing you need to do is to set up the Blynk server - I'd recommend looking on their website for a more comprehensive guide, but the instructions they give are at the following address: https://codebender.cc/example/BlynkSimpleEthernet/...
It's possible to connect over wifi without using a USB connection using a wifi shield, with only minimal alteration to your code - simply find the example sketch "Arduino_WiFi_Shield" and follow the instructions, replacing the top parts in my code with what's in the example sketch. It's also possible to connect over Bluetooth - explore the example section of the Blynk library and you'll find several ways to connect.
Step 5: Enjoy!
Simply run the server as explained in the previous step, and open the app to control your Arduino laser!
mjrovai made it!