top of page
Search

Robotic Laser Pointer

  • Writer: VJ
    VJ
  • Jun 2, 2020
  • 7 min read

Updated: Jun 3, 2020






For this project I will create a laser bot that shines a laser around a room. It will be controlled by an Arduino Uno using a grove expansion shield. For usage of Arduino, I have recently started reading Exploring Arduino: Tools and Techniques for Engineering Wizardry by Jeremy Blum. This book is helping me understand farther into more complicated Arduino usages. All of my previous background knowledge of Arduino comes from the first few pages of this book.








Materials necessary:


x1 hollow plastic container

multiple wires


Hardware:

The servos I will be using are lightweight and use less power than conventional servos, which allows me to use the Arduino as the only power source. Usually Servos require 5V each, but these can operate without external power. If using higher power servos, remember to connect power to them using a breadboard.


First, we will need to configure the servos. We will use one for the x-axis, and one for the y-axis, so we can control in two dimensions. Note that we will be using hot glue to secure parts and keep everything together. Take the plastic container and glue the first servo onto the top, so the head is facing up. The take the second servo and glue it onto the head attachment of the first servo, so it is facing sideways. This should create a 90 degree angle. Remember to use the head attachments on both servos.


Next we will need to glue the laser diode onto the second servo. Use any plastic tubing, I used a hollowed out pen. Insert the diode into the tube so the laser is pointing out of the end and hot glue it. Make sure to attach wires on the diode to make it Arduino compatible. attach the laser that we have created onto the y-axis servo head. It doesn't matter the angle, since we can adjust it when necessary.


If you are using a grove expansion shield, we only need to plug the servo wires into D8 (x-axis) and D7 (y-axis). If using a Arduino only, then you will need a breadboard. Use two wires to connect the breadboard ground rail and power rail to the Arduino. Connect both brown wires to the ground rail, and both red wires to the power rail. Then you can connect the yellow wires to any analog pin on the Arduino. The pin will matter when we start coding.


Lastly, we will need to connect the laser diode to the board. If you want to turn the diode on/off at will like me, connect the diode to pin 2 on the Arduino with the red wire. Then connect the blue wire to ground. If you only want a steady laser, connect the red wire to the power rail on the breadboard. You may need extensions for the cables on the laser diode.


If you want to reduce the clutter, then you can cut a hole out of the hollow plastic container and keep all the electronics and wires in there. Just make sure to leave a hole for the wires to go in and out, and also a hole to connect the Arduino to a computer. I also included a handmade hinge to open and close the electronics inside. The hinge is made out of foam that I sewed together.



Setup:


For my project, I am using a Mac Mini with an Intel Core i7 processor. This will be important because using linux with Arduino is a bit of a hassle, and it took me quite a while to set everything up to communicate. Here I will show everything I found for anyone to use. I will be using java RXTX comm API. This will allow us to connect to the serial monitor and send commands to Arduino. For all of the code using RXTX, I highly recommend this resource.


For downloading the RXTX package, you can go here to access the jar file. I also dowloaded the jnlib file directly. You can get the jnlib file easily by just searching for it. All of the linux packages contain .so files, but they do not work as intended. You can import the jar file into your classpath in eclipse, but I will be including the files to my entire computer. We will have to include the jar and the jnlib into our system files to do that. There is a lot of conflicting opinion, but this is what I found that works. For people not familiar with bash commands simply copy and paste these commands into your terminal one by one.

cd /Library/Java/Extensions

I found the my Java files were in Library. It may vary, but this is where it is stored on my pc. It mainly depends on where you save java after first downloading it.


sudo cp *where you stored your jar file*

I had my jar file in my desktop, but that may vary. Just locate your file and drag + drop it into the terminal. This will allow you to see the location of the file. Sudo overrides any restrictions keeping you from copying into the file. Just make sure to enter your password.


sudo cp *where you stored your jnlib file*

The steps are similar to the jar file, just copy the jnlib file into the folder.


Now, we should have RXTX fully installed into the pc. Now we can import the right files and talk to the Arduino. Something important that came up, make sure you create the directory /var/lock. To do this just type the following command:


sudo mkdir var

Make sure that you are in the root when typing this command. To go to the root, type:


cd /

After creating the var directory, you need to create the lock directory. To do this, enter the following command:


sudo mkdir lock

Code:


Now we can finally get into programming the robot. We will need the Arduino studio and Eclipse studio. The reason we are using java is so we can control the robot using our mouse movements. We will be using a java extension called swing, which creates GUIs (graphical user interface). The type of app we will be using is a mouse listener. Create a new project, in the src file create a new java file and delete any existing modules/files. You may need to include the RXTX jar into the classpath. To do that, create a new file called lib in your project. ctrl c + ctrl v the jar file into the lib folder. Right click the jar file and add to build path.


We will be using the code from here, so make sure to take the code referencing RX (sending data). We will essentially track mouse movements and send the data to the Arduino via serial communication. X coordinates will be used by the base X servo, and the Y coordinates for the Y servo.



Dimension sizeOfScreen = new Dimension();


public ListenerImplementation(Serial coord){

addMouseMotionListener(new MouseMotionListener() {

@Override

public void mouseDragged(MouseEvent e) {}

public void mouseMoved(MouseEvent e) {

sizeOfScreen = getSize();

double x1 = e.getX();

double y1 = e.getY();

int x = (int)((x1/sizeOfScreen.getWidth()) * 158);

int y = (int)((y1/sizeOfScreen.getHeight()) * 170) + 10;

x = 158-x + 5;

if(x > 158) {

x -= 10;

}

coord.sendInt(x, y);

}

});


This is the code for the mouse listener. Since servos only go from 0 to 180, the dimension object helps check how large the frame is and accurately converts the coordinates to 0 to 180. This conversion is shown in the code. Lastly, my servos needed some fine tuning, which is why I included the final few if statements. coord.sentInt(x , y); Is using the RXTX code to send the coordinates on the serial monitor.


addMouseListener(new MouseAdapter(){

int i = 0;

@Override

public void mouseClicked(MouseEvent e){

// false = 250; true = 251

if(i % 2 == 1) {

coord.sendInt(250);

}

else {

coord.sendInt(251);

}

i++;

}

});


This code is reading mouse clicks. If you click the mouse, then the laser will turn on, then off if you click the mouse again. Since the mouse motion listener is only sending data from 0 to 180, we can send data values above this to signify on and off. Note: The highest value we can send is 255 since the values are being converted to bytes for serial, and byte max is 256. Sending strings is a hassle, so I used integers for this part.


Full Code:







Arduino Code:


The Arduino code is pretty straight forward, we just have to read the data that we are receiving from the serial monitor and translate it to the servos. Here are the initial declarations in the code:



#include <Servo.h>



int a ;

int i = 0;


Servo serX;

Servo serY;


int laserPin = 2;


All that this is doing is declaring the Servos we will be using. Int a is where the serial value will be stored. Int i is a simple counter. Laser pin represents the pin where the laser is being controlled.



void setup() {

Serial.begin(9600);

Serial.setTimeout(10);

serX.attach(8);

serY.attach(7);

pinMode(laserPin, OUTPUT);

}


In the setup, we start the serial communication at a baud rate of 9600 (standard) and setTimeout reduces lag during serial communication. serX and serY are being attacked to D8 and D7 on the grove module, but if you don't have one, just specify which analog pin you are using instead, and connect the yellow wire to that pin. We also specify that laserPin is going to be output, because we want current to flow into the laser and turn it on.



void loop() {

while(Serial.available() == 0){}

a = Serial.read();

if(a == 250 || a == 251){

if(a == 251){

digitalWrite(laserPin , HIGH);

}

else{

digitalWrite(laserPin , LOW);

}

}

else{

i++;

if(i % 2 == 0){

serY.write(a);

}

else{

serX.write(a);

}

}

}




Finally, we have our loop function. Here we read the serial monitor using int a. The initial while loop helps to avoid reading the monitor when nothing is being sent. Otherwise you will receive a constant supply of "-1". As previously defined, we can activate the laser based on if 250 and 251 are sent from the java code. If the input is something else, i.e. the coordinates of the mouse, then it will write for serY and serX. The reason we have the counter is because x and y are being sent separately, and the counter helps us figure out which one it is. If counter is even it means it is y, since x is being sent first. This way the even send is always y, and odd is always x.


Full Code:










Credits to Micheal Reeves for the design idea. This project uses java instead of C++, and contains a few additional features.

 
 
 

Comments


Post: Blog2_Post
bottom of page