/Arduino-Nano-Laser-Demo

A very simple example of setting GPIO pins to HIGH and LOW to switch on and off some cheap chinese red lasers.

Primary LanguageC++

Arduino-Nano-Laser-Demo

A simple example of using Arduino basic GPIO pin HIGH and LOW to switch on and off some cheap Chinese red lasers.

This demonstrates the most simple aspect of any microcontroller - being able to turn a pin ON (HIGH) or OFF (LOW).

However, in this sketch we use PWM (Pulse Width Modulation) to do so. If this doesn't make any sense then use google.

This sketch is 'dumb' in that I manually wrote the code to be timed perfectly to the song track - there's no automatic detection of a beat etc. Audio is played from a Windows PC hooked up to the Arduino Nano. A script simply tells the Arduino to start firing the Lasers at the same time the Windows PC plays the music.

What do I need for this Sketch?

  • An Arduino Nano or a clone from Aliexpress
  • 6 x KY-008 red lasers from Aliexpress. You can get 10 of these for about $4.
  • A breadboard at least 10cm wide
  • Bunch of hookup wires

Why an Arduino Nano?

The Arduino Nano was used for this demonstration as it has 5 volt GPIO pins and the lasers are 5 volt as well - so easy to wire up. Also, the lasers consume about 10mw each, which is just under the Arduino Nano's per-GPIO pin tolerance of about 20mW.

For the Arduino Nano GPIO pins 3, 5, 6, 9, 10, 11 are the only PWM capable pins.

How to run

  1. Upload the sketch to the Arduino Nano
  2. Wire up the 'S' pin on each of the KY-008's to a respective PWM GPIO pin on the Arduino Nano (pins 3, 5, 6, 9, 10, 11)
  3. Wire the '-' GND on each KY-008 to a common ground line on the breadboard.
  4. Use windows powershell to run the 'PlayBeat1.ps1' - ensure that the Serial port configured in the script is correct for your Nano.
  5. Also ensure you have a copy of the .wav file in a place and update the powershell script accordingly. Download the example .wav here.

Breadboard example

Understanding the Code

The code is over complicated by the fact that originally I wrote it to change a strip of RGB leds using the FastLED library, then quickly modified it to send PWM to the same pins instead. pwm_pins_update() and pwm_pins_set_all() are the functions that actually does all the work.