DIY Double Clap Rainbow Light

by a886a in Circuits > LEDs

308 Views, 0 Favorites, 0 Comments

DIY Double Clap Rainbow Light

Double clap light demonstration

Did you ever dream about turning lights on and off by clapping your hands? If so, this project is for you!

This project uses a sound sensor to control a NeoPixel LED strip that creates a rainbow!

Supplies

  • Arduino Uno
  • KY-037 Sound Sensor
  • 1k ohm resistor
  • 1000 uF capacitor
  • Breadboard
  • Jumper wires
  • Male/female jumper wires
  • NeoPixel LED strip
  • 5v AC/DC power supply
  • Female DC socket to screw terminal block adaptor
  • Small cardboard box (optional)
  • Craft wires (optional)

Wire the Components

Double clap light schematic_bb.jpg

Be sure to gather your supplies first!

The following diagram shows the completed wiring diagram once the code is uploaded. Do not plug in the Arduino to the computer and the external supply at the same time. When uploading the code to the Arduino, do not connect the red wire that goes to the 5v pin on the Arduino and the black wire that goes to the GND pin on the Arduino.

Be sure to test the sound sensor so that it correctly picks up sounds - there's usually a screw that can be turned.

The external power supply is not pictured. The external power supply should 1) be connected to the Female DC Socket to Screw Terminal Block Adaptor 2) be enough to power all the components (Arduino, LED strip, sound sensor).

Install the FastLED Library

FastLED Library

  1. Open Arduino IDE
  2. Navigate to Tools > Manage Libraries... The Library Manager will open and you will find a list of libraries that are already installed or ready for installation
  3. Search for FastLED. You should see the target version of FastLED (version 3.4.0) listed
  4. Click on install and wait for the IDE to install FastLED. You should now see FastLED available in the Sketch > Include Library menu

Upload the Code to Arduino

#include <FastLED.h>

#define NUM_LEDS 19 // How many leds are in the strip?
#define signalToLEDPin 5 // LED pin #define soundSensorPin 4 // sound sensor pin // sound sensor variables int lastSoundValue; int soundValue; long lastNoiseTime = 0; long currentNoiseTime = 0; long lastLightChange = 0; // LED variables boolean lightOn = false; uint8_t gHue = 0; // rotating "base color" used by many of the patterns CRGB leds[NUM_LEDS]; // This is an array of leds. One item for each led in your strip // This function sets up the leds and tells the controller about them void setup() { delay(2000); // sanity check delay - allows reprogramming if accidently blowing power w/leds FastLED.addLeds(leds, NUM_LEDS); pinMode(soundSensorPin,INPUT); pinMode(signalToLEDPin, OUTPUT); LEDS.setBrightness(84); } // This function runs over and over, and is where you do the magic to light your leds. void loop() { readSoundSensor(); } void fadeall() { for(int i = 0; i < NUM_LEDS; i++) { leds[i].nscale8(250); } } // LED pattern void cyclon(){ static uint8_t hue = 0; // First slide the led in one direction for(int i = 0; i < NUM_LEDS; i++) { // Set the i'th led to red leds[i] = CHSV(hue++, 255, 255); // Show the leds FastLED.show(); // now that we've shown the leds, reset the i'th led to black // leds[i] = CRGB::Black; fadeall(); // Wait a little bit before we loop around and do it again delay(80); } // Now go in the other direction. for(int i = (NUM_LEDS)-1; i >= 0; i--) { // Set the i'th led to red leds[i] = CHSV(hue++, 255, 255); // Show the leds FastLED.show(); // now that we've shown the leds, reset the i'th led to black // leds[i] = CRGB::Black; fadeall(); // Wait a little bit before we loop around and do it again delay(30); } // End with a rainbow fill_rainbow( leds, NUM_LEDS, 0, 255/NUM_LEDS ); FastLED.show(); } // Switches LED off or display pattern void updateLEDState(){ if (!lightOn) { fill_solid(leds, NUM_LEDS, CRGB::Black); FastLED.show(); } else { cyclon(); } } // Sound sensor code void readSoundSensor(){ soundValue = digitalRead(soundSensorPin); currentNoiseTime = millis(); if (soundValue == 1) { // if there is currently a noise if ((currentNoiseTime > lastNoiseTime + 200) && // to debounce a sound occurring in more than a loop cycle as a single noise (lastSoundValue == 0) && // if it was silent before (currentNoiseTime < lastNoiseTime + 800) && // if current clap is less than 0.8 seconds after the first clap (currentNoiseTime > lastLightChange + 1000)) { // to avoid taking a third clap as part of a pattern lightOn = !lightOn; updateLEDState(); lastLightChange = currentNoiseTime; } lastNoiseTime = currentNoiseTime; } lastSoundValue = soundValue; }

Create Your Lamp

205846372_174857297954725_1128322442441954977_n.jpg
206421063_475529600414914_6295621545429587559_n.jpg

The lamp was put together using craft wires and uses a cardboard box to hide the components, but feel free to customize this part!