int laserPin = 10; // Laser diode connected to pin 10
void setup() {
pinMode(laserPin, OUTPUT); // Set pin as output
}
void loop() {
// Turn ON laser for 0.5 seconds
digitalWrite(laserPin, HIGH);
delay(500);
// Turn OFF laser for 0.5 seconds
digitalWrite(laserPin, LOW);
delay(500);
// Gradually increase laser intensity (PWM 0–255)
int i = 0;
while (i <= 255) {
analogWrite(laserPin, i); // Set intensity
delay(50); // Wait for smooth fade
i += 5; // Increment intensity
}
}
Instruction Details
Wiring: Connect laser module VCC to 5V, GND to GND, IN/Signal to Digital Pin 10. For raw diode: anode via 100Ω to Pin 10, cathode to GND.
Library: Not required (built-in).
Upload Code: Tools → Board → Arduino Uno. Tools → Port → select your COM port. Click Upload button.
View Output: Laser turns ON/OFF based on code. Never point at eyes or skin.
How to Use
Connect the required components as per the Pin Configuration above
Open Arduino IDE and create a new sketch
Copy and paste the code above
Select your Arduino board and COM port from Tools menu
Click the Upload button to upload the code to your Arduino
Open Serial Monitor (if applicable) to see the output