Control a Laser with Joystick and Servos Using Arduino

control a laser with joystick and servos using Arduino control a laser with joystick and servos using Arduino

In this tutorial, you’ll learn how to build an interactive control system that lets you aim and move a laser using a joystick, servos, and Arduino. Whether you’re looking to create a laser pointer setup for fun projects, interactive light displays, or precision-based games, this guide will walk you through every step.

In this project, we are using an Arduino Nano as it is ideal due to its small size and powerful functionality. It reads input from the joystick, processes the signals, and controls the servos accordingly, allowing precise movement of the laser. With its multiple PWM-capable digital pins, the Nano seamlessly manages both the X and Y axes of the servos, making it possible to aim the laser in any direction. Its compact form factor makes it easy to integrate into the project while providing reliable control and flexibility.

What You’ll Learn:

  • How to connect and control servos with Arduino.
  • Synchronizing joystick inputs to move a laser in any direction.
  • Writing simple yet effective Arduino code to control the servos.

Components Needed

  • Arduino board (e.g., Uno)
  • 2 Servos (for X and Y axis control)
  • Joystick module
  • Laser pointer module
  • Connecting Wires
  • External power supply (if needed for the servos)
  • 3d Printed brackets : https://www.thingiverse.com/thing:501701(or you can DIY)

Joystick module:

joystick module pinout

A joystick module is an input device used to control movement in two directions, typically along the X and Y axes. It consists of two potentiometers, one for each axis, which measure the position of the joystick as it is tilted. The module outputs analog signals that correspond to the joystick’s position, allowing precise control of devices such as servos, robots, or games. Many joystick modules also include a push button that can be activated by pressing the joystick down. It’s widely used in Arduino projects for intuitive control interfaces.

  • The joystick’s X and Y axes are mapped to control the position of the two servos. Moving the joystick adjusts the servos, changing the direction of the laser.
  • The servoX and servoY positions are updated continuously, enabling smooth movement as you tilt the joystick.
  • A button on the joystick (if present) can be used to turn the laser on and off.

Circuit Diagram

Control a Laser with Joystick and Servos Using Arduino circuit diagram

As you can see from the above circuit diagram Joystick module is connected to Arduino as power pins VCC and GND are connected to 5V and GND of Arduino respectively. VRx and VRy are connected to A0 and A1 of Arduino Respectively, SW pin is connected to D2 of Arduino.

Servo motors doesn’t work good if we connect them to Arduino power, so we are using an external 5v power supply. The power pins of both servos VCC and GND are connected to 5v external power supply. And signal pins of X axis servo motor and Y axis servo motor are connected to D10 and D11 of Arduino respectively.

Laser module power pins VCC and GND are connected to 5v External power supply and Signal pin of Laser module is connected to D12 of Arduino.

Uploading Code to Arduino

After connecting all the components now its time to upload the code to Arduino through Arduino IDE on PC.

Copy the below code and upload it to the ArduinoNANO to which we connected all the components.

//Control a Laser with Joystick and Servos Using Arduino
#include<Servo.h>
 Servo s1,s2;  
 int p1=90,p2=90;   // initial position of both servos in degree
void setup() {
  s1.attach(10); //defining the pins for the servo's
  s2.attach(11);
  pinMode(12,OUTPUT); //Laser Output
  pinMode(2,INPUT);   //SW pin status
  digitalWrite(2,HIGH);
  Serial.begin(9600);  //Start Serial Monitor
}

void loop() 
{
  int t1=0,t2=0;  //rate of increment/decrement of angle
  int a=analogRead(A0); // reads analog x-y readings of joystick
  int b=analogRead(A1);

  //when joystick is moved away from the center
  if(a<=450 or a>=550)
  {
    t1=map(a,0,1023,10,-10);
    p1=change(p1,t1);  //change the servo's current position
  }
  if(b<=450 or b>=550)
  {
    t2=map(b,0,1023,-10,10);
    p2=change(p2,t2);  //change the servo's current position
  }
   s1.write(p1); // rotate the servo's if the joystick is moved
   s2.write(p2);
  if(digitalRead(2)==LOW)  //reads the status of joystick's SW pin
  digitalWrite(12,HIGH);
  else
  digitalWrite(12,LOW);
  
  //print the analog readings of joystick and servo degree on serial monitor
  Serial.print("s1:");
  Serial.print(a);
  Serial.print("\t");
  Serial.print("s2:");
  Serial.print(b);
  Serial.print("\t");
  Serial.print("t1:");
  Serial.print(t1);
  Serial.print("\t");
  Serial.print("t2:");
  Serial.print(t2);
  Serial.print("\t");
  Serial.print("pos1: ");
  Serial.print(p1);
  Serial.print("pos2: ");
  Serial.println(p2);
  delay(70);          // for Stability
}

int change(int pos,int t)
{
    pos=pos+t;                   // Increment/decrement the Servo angle
    if (pos>180)                //  maximum anlgle of servo is 180 degree
    pos=180;
    if(pos<0)                  // minimum angle of servo is 0 degree
    pos=0;
    return(pos);               //return the change of position
}

After uploading the code to the Arduino Nano the system starts functioning by reading inputs from the joystick module. Here’s how it works:

  1. Joystick Movement: As you move the joystick, it changes the voltage levels on the X and Y axes, which the Arduino reads as analog inputs.
  2. Signal Processing: The Arduino processes these inputs and maps the joystick position to corresponding servo angles, determining how much each servo should rotate.
  3. Servo Movement: The two servos, connected to the laser, move based on the joystick’s X and Y directions. This allows precise control of the laser’s position in both horizontal and vertical axes.
  4. Laser Aiming: The laser follows the servos’ movements, allowing you to aim it by simply tilting the joystick in the desired direction. You can use the button of joystick to turn on/off the laser.

Applications

  • Interactive Light Shows: Sync the joystick-controlled laser to music or other light effects for stunning visuals.
  • Precision Games: Use the laser to aim at targets, incorporating it into a challenge or game.
  • Educational Projects: Demonstrate concepts like servos, joystick inputs, and control systems for classroom use.

Video demo:

Manually controlling Laser Pan/Tilt using Joystick and servo motors

Add a comment

Leave a Reply

Your email address will not be published. Required fields are marked *