How to use a laser module with an Arduino: a cat laser toy
Beginner
Or see every lesson.
- 503parts on the bench
- 44boards running now
- 1.00xreal time, on every board
Wire S to a digital pin and the minus pin to GND, and leave the middle pin alone. Then digitalWrite(pin, HIGH) turns the laser on and LOW turns it off. There is no driver chip on the board: the laser’s current comes straight from the pin, through a small resistor, so the pin is both the switch and the power.
The circuit above is a cat laser toy. The KY-008 sits on two servos, pan and tilt. Press Run and it prints "round 1: laser on", then "dot to" and two angles each time it picks a new spot, and glides the dot there one degree at a time. After a minute it prints "resting: press the button to play again", the laser goes off and the servos come back to the middle. Press the button for another round.
What you need
- An Arduino Uno
- A KY-008 laser module
- Two SG90 servos and a pan and tilt bracket
- A pushbutton
- A half-size breadboard and jumper wires
Wiring
| Part and pin | Goes to | Note |
|---|---|---|
| Laser S | Pin 7 | The pin powers the laser |
| Laser minus (GND) | GND | The middle pin is not connected |
| Pan servo signal | Pin 9 | Red to 5 V, brown to GND |
| Tilt servo signal | Pin 10 | Red to 5 V, brown to GND |
| Button | Pin 2 and GND | INPUT_PULLUP: pressed reads LOW |
The three pins, and the one that does nothing
The KY-008 is a 650 nm red laser diode in a brass barrel with a resistor in series. The pins are S, the middle one, and minus. On most of these boards the middle pin is not connected to anything at all. The diode and its resistor sit between S and minus, so whatever voltage S has runs the laser.
That trips people up. They wire the middle pin to 5 V, as they would on any other module, then S to 5 V too, and the laser is on all the time with nothing for the sketch to switch. Put S on a pin and the sketch is in charge.
Dimming it with analogWrite()
Because the pin is the power, a PWM pin can dim the laser. analogWrite() switches it on and off hundreds of times a second, and the eye sees the average. The sketch below steps a laser on pin 9 through four levels. In the simulator, 255 lit it at full brightness, 128 at half, 32 at about an eighth, and 0 turned it off.
The cat laser does not dim, so it uses pin 7, which is not a PWM pin. If you want a dimmer, pick one of the six PWM pins on an Uno: 3, 5, 6, 9, 10 and 11.
const int LASER = 9;
void setup() {
pinMode(LASER, OUTPUT);
}
void loop() {
analogWrite(LASER, 255);
delay(500);
analogWrite(LASER, 128);
delay(500);
analogWrite(LASER, 32);
delay(500);
analogWrite(LASER, 0);
delay(500);
}How the cat laser moves
Each round picks a random spot with random(): a pan angle between 30 and 150 degrees, and a tilt between 100 and 160. Then it moves both servos one degree at a time with a 15 ms pause, so the dot glides instead of jumping, and waits a random time before the next spot. A round lasts 60 seconds, counted with millis().
The tilt never goes above 60 degrees from straight down, so the dot stays on the floor and out of anyone’s eyes. That limit is the most important line in the sketch.
Laser safety
A real KY-008 is a Class 3R laser. Never point it at anyone’s eyes, a pet’s included, and never look into the barrel. Keep the beam pointing down at the floor or a wall, and make sure your code keeps it there too, the way the tilt limits above do.
What the simulation leaves out
The beam here ends in a red dot that is as bright as the PWM level says. A real diode below its lasing threshold glows dimly instead of lasing, so very low analogWrite() values may look like nothing at all. The beam’s spread and its power are not modeled, and a laser pointed at a light sensor across the room does not reach it in the simulator, so a laser tripwire has to be built on a real bench.
Common mistakes
Wiring the middle pin and S to 5 V. The laser then runs all the time. Put S on a pin.
Thinking the board needs its own power. It does not: the current comes from S.
Using a non-PWM pin with analogWrite(). On pin 7, any value under 128 is off and anything else is fully on. Use a PWM pin to dim.
Powering the two servos from the Arduino’s 5 V pin on a real bench. Two SG90s can pull more current than USB gives when they move together. Give them their own 5 V supply and join the grounds.
Aiming it where people or pets can look into it. Limit the angles in code.
Questions
How do I connect a KY-008 laser module to an Arduino?
S to a digital pin and the minus pin to GND. The middle pin is not connected on most boards. digitalWrite(pin, HIGH) turns it on and LOW turns it off.
Can I control the brightness of a laser module?
Yes, with analogWrite() on a PWM pin. In the simulator 128 gives half brightness and 32 about an eighth. A real diode may stop lasing at very low values.
Does a laser module need a resistor?
The KY-008 has one on the board already, in series with the diode, so you can wire S straight to a pin. A bare laser diode needs its own resistor or driver.
Is a KY-008 laser safe?
It is a Class 3R laser, so it can hurt eyes. Never point it at a face or look into it. Keep the beam on the floor or a wall, and limit the servo angles in code if it moves.
Keep going
Build this for real
Open the editor, change a value and watch the number move with it. Nothing to install, and no account needed.