Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
Jacob SmithSharon MulaparthiKevin Inman
Published © GPL3+

Motion and Sound Activated Light

LED strips are cool, but you know what's even cooler? Being able to turn them on just by waving your hand or making a sound!

IntermediateFull instructions providedOver 10 days287
Motion and Sound Activated Light

Things used in this project

Hardware components

Argon
Particle Argon
×3
DAOKI High Sensitivity Sound Microphone Sensor Detection Module
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Breadboard (generic)
Breadboard (generic)
×3
LED Strip 3.3 ft
×1
HC-SR501 PIR Infrared Sensor Motion Module
×1
HiLetgo TTP223 Capacitive Touch Sensor
×1
5V DC Power Supply with DC Terminal Connector Tip
×1

Software apps and online services

Particle Build Web IDE
Particle Build Web IDE
ThingSpeak API
ThingSpeak API

Story

Read more

Schematics

Flow Chart

Argon 1

Argon 2

Argon 3

Code

Argon 3 Sound Sensor

C/C++
#include "Particle.h"

// Pin definitions
const int soundPin = A2;
const int ledPin = D7;

// Declares the outputs and inputs
void setup() {
  pinMode(soundPin, INPUT);
  pinMode(ledPin, OUTPUT);
 
}

// runs continuosly until sound above the value of 1000 has been detected
// when sound has been detected, it will publish the event to Argon 1
void loop() {
  if (analogRead(soundPin) > 1000) { // Adjust the threshold as needed
    digitalWrite(ledPin, HIGH);
    Particle.publish("sound_event", "Sound has been detected!", PRIVATE);
    delay(2000);
    digitalWrite(ledPin, LOW);
  }
}

Argon 1 LED Strip Subscriber Code

C/C++
// This #include statement was automatically added by the Particle IDE.
#include <HttpClient.h>

// This #include statement was automatically added by the Particle IDE.
#include <ThingSpeak.h>

// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>

#include "Particle.h"


// Pin definitions
const int LED_STRIP_PIN = D5;
const int NUM_PIXELS = 60; // The number of pixels in the LED Strip
const int MOTION_LED_PIN = A5;
const int TOUCH_LED_PIN = A1;
const int SOUND_LED_PIN = A4;

const char* THINGSPEAK_API_KEY = "API_KEY"; //hidden for security reasons
unsigned long lastPublish = 0;
int motionCount = 0;
int touchCount = 0;
int soundCount = 0;

// NeoPixel strip object
Adafruit_NeoPixel strip(NUM_PIXELS, LED_STRIP_PIN, WS2812B);

// Declares the HttpClient instance and configures the request and response
HttpClient http;
http_request_t request;
http_response_t response;

// Function prototypes
void motionHandler(const char *event, const char *data);
void touchHandler(const char *event, const char *data);
void soundHandler(const char *event, const char *data);

// Turns off all lights of the LED Strip at the beginning
void turnOffAllPixels() {
  for (int i = 0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, 0, 0, 0); // Off
  }
  strip.show();
}


// Declares the outputs and inputs

void setup() {
  strip.begin();
  strip.show(); 

  // Initialize LEDs connected to A5 and A1
  pinMode(MOTION_LED_PIN, OUTPUT);
  pinMode(TOUCH_LED_PIN, OUTPUT);
  pinMode(SOUND_LED_PIN, OUTPUT);

  // Turn off all pixels
  turnOffAllPixels();
  
   // Configures request for ThingSpeak
  request.hostname = "api.thingspeak.com";
  request.port = 80;
  


  // Subscribes to motion (argon 2), touch (argon 2), and sound (argon 3) events
  Particle.subscribe("motion_event", motionHandler, MY_DEVICES);
  Particle.subscribe("touch_event", touchHandler, MY_DEVICES);
  Particle.subscribe("sound_event", soundHandler, MY_DEVICES);
}

// Publishes the data to ThingSpeak every 20 seconds
void loop() {
  unsigned long now = millis();
  if (now - lastPublish > 20000) { 
    request.path = String::format("/update?api_key=%s&field1=%d&field2=%d&field3=%d", THINGSPEAK_API_KEY, motionCount, touchCount, soundCount).c_str();
    request.body = "";
    
    http.get(request, response, NULL);

    // Reset counts after the 20 seconds
    motionCount = 0;
    touchCount = 0;
    soundCount = 0;

    lastPublish = now;
  }
}

// Declares motionHandler Function
void motionHandler(const char *event, const char *data) {
    
  // Lights up all pixels in the strip with the color red
  for (int i = 0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, 255, 0, 0); // Red color
  }
  strip.show();

  // Lights up LED (motion LED pin) connected to A5
  digitalWrite(MOTION_LED_PIN, HIGH);
  delay(2000);

  // Turns off the LED Strip pixels after 2 seconds
  for (int i = 0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, strip.Color(0, 0, 0)); // Off
  }
  strip.show();

  // Turns off LED LED (motion LED pin) connected to A5
  digitalWrite(MOTION_LED_PIN, LOW);
  
  //Adds the motion count
  motionCount++;
}

// Declares touchHandler Function
void touchHandler(const char *event, const char *data) {
    
  // Lights up all pixels in the strip with the color green
  for (int i = 0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, 0, 255, 0); // Green color
  }
  strip.show();

  // Lights up LED (touch LED pin) connected to A1
  digitalWrite(TOUCH_LED_PIN, HIGH);
  delay(2000);

  // Turns off the pixels after 2 seconds
  for (int i = 0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, strip.Color(0, 0, 0)); // Off
  }
  strip.show();

  // Turn off LED connected to A1
  digitalWrite(TOUCH_LED_PIN, LOW);
  
   //Adds the touch count
  touchCount++;
}

// Declares soundHandler Function
void soundHandler(const char *event, const char *data) {
    
  // Lights up all pixels in the strip with the color blue
  for (int i = 0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, 0, 0, 255); // Blue color
  }
  strip.show();
    
  // Lights up LED (sound LED pin) connected to A1
  digitalWrite(SOUND_LED_PIN, HIGH);
  delay(2000);
  
  // Turns off the pixels after 2 seconds
  for (int i = 0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, strip.Color(0, 0, 0)); // Off
  }
  strip.show();

  // Turns off LED connected to A4
  digitalWrite(SOUND_LED_PIN, LOW);
  
    //Adds the sound count
  soundCount++;
}

Argon 2 Motion/Touch Sensor

C/C++
// Publisher - Argon with PIR sensor and touch sensor

#include "Particle.h"

// Pin definitions
const int PIR_PIN = D2;
const int ARGON_LED = D7;
const int TOUCH_PIN = D3;
const int MOTION_LED_PIN = A5;
const int TOUCH_LED_PIN = A4;

// Variables
bool motionDetected = false;
bool touchDetected = false;
unsigned long lastCheck = 0;
const unsigned long CHECK_INTERVAL = 2000;

// Function prototypes
void checkMotion();
void checkTouch();


// Declares the outputs and inputs

void setup() {
    
  // Sets up the pins
  pinMode(PIR_PIN, INPUT);
  pinMode(ARGON_LED, OUTPUT);
  pinMode(TOUCH_PIN, INPUT); 
  pinMode(MOTION_LED_PIN, OUTPUT);
  pinMode(TOUCH_LED_PIN, OUTPUT);

  // Turn off the LEDs initially
  digitalWrite(MOTION_LED_PIN, LOW);
  digitalWrite(TOUCH_LED_PIN, LOW);
}

// Checks if either motion or touch has been detected every 2 seconds
void loop() {
    
  // Checks for motion and touch every 2 seconds
  if (millis() - lastCheck >= CHECK_INTERVAL) {
    lastCheck = millis();
    checkMotion();
    checkTouch();
  }

  // Lights up the corresponding LED and publishes an event to Argon 1 if motion or touch is detected
  if (motionDetected) {
    digitalWrite(MOTION_LED_PIN, HIGH);
    digitalWrite(ARGON_LED, HIGH);
    Particle.publish("motion_event", "Motion has been detected!", PRIVATE);
    delay(2000);
    digitalWrite(MOTION_LED_PIN, LOW);
    digitalWrite(ARGON_LED, LOW);
    motionDetected = false;
  }

  if (touchDetected) {
    digitalWrite(TOUCH_LED_PIN, HIGH);
    digitalWrite(ARGON_LED, HIGH);
    Particle.publish("touch_event", "Touch has been detected!", PRIVATE);
    delay(2000);
    digitalWrite(TOUCH_LED_PIN, LOW);
    digitalWrite(ARGON_LED, LOW);
    touchDetected = false;
  }
}

void checkMotion() {
  motionDetected = digitalRead(PIR_PIN);
}

void checkTouch() {
  touchDetected = digitalRead(TOUCH_PIN);
}

Credits

Jacob Smith
1 project • 2 followers
Sharon Mulaparthi
1 project • 2 followers
Kevin Inman
1 project • 2 followers
I don't know what to put here

Comments