How to Make a Laser With a Joystick on Arduino

In this project, we are creating a joystick controlled laser by connecting two servos to the joystick and using this setup as a pan and tilt controller for the laser pointer.

The following is an excerpt from the Arduino Project Handbook: 25 Practical Projects to Get You Started , a beginner-friendly collection of electronics projects using an inexpensive Arduino board.

Required parts

Here’s what you need to complete this project:

  • Arduino
  • Bread board
  • Jumper wires
  • 2 Servo Motors Tower Pro SG90 9g
  • Analog 5-pin, 2-axis joystick module
  • Swivel-tilt housing module

How it works

Servos are small motors that can accurately rotate their levers from 0 to 180 degrees. In this project, we will place the servos in a pan / tilt bracket.

The swivel mount is a worthy investment as it makes attaching the laser to the servo much easier. Here we operate the laser, but you can easily replace the laser with a webcam or other small device. We use two servos, one for moving left and right, and the other for moving up and down. As you may recall, servomotors have three wires, shown in Figure 10-1: positive power (red), negative power or ground (black or brown), and signal (usually yellow, orange, or white).

Before we start building, you need to know a little about how the joystick works. The joystick shown in fig. 10-2 is essentially two potentiometers and a button that allow us to measure stick movement in two dimensions.

Potentiometers are variable resistors and act like sensors that give us a voltage that changes as the device rotates around its shaft. Thus, as you move the joystick around its center, its resistance – and therefore its power – changes. The outputs of the potentiometers are analog, so they can only have a value between 0 and 1023 when read by the analog pin of the Arduino. This number sends a pulse to the Arduino, which in turn tells the servos how far to go.

A joystick usually has pins: VRx (x-axis signal), VRy (y-axis signal), SW (button that we won’t be using in this project), as well as GND and + 5V for power.

When the x-axis of the joystick is moved left or right, the corresponding servo will move in that direction; when the Y-axis of the joystick moves up or down, the other servo moves up or down.

Assembly

1. Connect the red wires of both servos to the + 5V rail and their brown wires to the breadboard GND.

2. Connect one of the yellow signal wires of the servo directly to pin 9 of the Arduino, and the signal wire of the other servo directly to pin 10 of the Arduino, as shown in the circuit diagram in Figure 10-4.

3. Connect GND from joystick module to Arduino GND rail and + 5V to Arduino + 5V rail. Connect VRx pin directly to Arduino A0 and VRy pin directly to Arduino A1. Again, the SW switch connection is not used in this project.

4. Connect the rails on the breadboard to the Arduino ground and + 5V, then make sure your setup is as shown in Figure 10-3.

Laser mounting

For this project, I attached the servos to the pan and tilt enclosure; You can find this or a similar package at a relatively reasonable price on eBay by searching for “Arduino Pan and Tilt Servo Kit.” You may need to assemble it yourself, but it’s easy to do with the included instructions.

Attach a laser diode to the top of the module; I recommend using a permanent glue gun, but you can use duct tape if you want something more temporary. You can now control the laser with the joystick. The servos will snap into the tilt and pan module as shown in Figure 10-4.

Moving the joystick left and right will move the X-axis servo, and moving the joystick up and down will move the Y-axis servo. The complete assembly is shown in Figure 10-5.

Sketch

The sketch first accesses the servo library and then defines the two servos as tilt and pan. The X-axis of the joystick is attached to the A0 pin of the Arduino, and the Y-axis is attached to the Arduino A1, and this is our INPUT. The x and y axes are then set as variables to move. The tilt servo is attached to pin 9 of the Arduino and the pan is attached to pin 10 of the Arduino and this is our OUTPUT. The Arduino then reads the INPUT from the joystick and changes that voltage to OUTPUT, moving the servos according to the selected direction. Here’s the servo library you’ll need, via Explore Labs :

// Used with kind permission http://learn.explorelabs.com/ // Creative Commons 4.0 Share Alike License (CC by SA 4.0) # include <Servo.h> Servo tilt, pan; // Create a servo object int joyX = A0; // Analog pin connected to the x-axis servo joyY = A1; // Analog pin connected to servo Y axis x, y; // Variables for reading values

empty setting () {tilt.attach (9); // Attach the tilt servo to pin 9 to the servo objectpan.attach (10); // Attach the pan servo to pin 10 to the servo}

void loop () {x = joyX; // Read the value of the x-axis (from 0 to 1023) y = joyY; // Read the Y-axis value (from 0 to 1023) x = map (analogRead (joyX), 0, 1023, 900, 2100); // Scale for use // with servo from // 900 to 2100 // microseconds

y = map (analogRead (joyY), 0, 1023, 900, 2100); tilt.write (x); // Set the servo position to the scaled value pan.write (y); delay (15); // Wait for the servos to move to a new position}

From the Arduino Project Handbook: 25 Practical Projects to Get You Started . Reprinted by permission of No Starch Press.

More…

Leave a Reply