Voice-controlled Light Switch

by JeanC117 in Circuits > Arduino

8211 Views, 7 Favorites, 0 Comments

Voice-controlled Light Switch

2016-07-22 16.47.21.jpg

Sometimes, I just don't want to get out of the bed to turn off the light. I've always wished I can just control the lights remotely so that it turns off when I command it to. This project accomplishes just that. It gives users control of any light by using a servo motor to control a light switch. Using a sound detector board as an input, the Arduino can move a servo at a given level of sound such that the light switch is toggled on or off.

Tools and Materials

2016-07-22 13.40.37.jpg
  • Arduino Uno
  • Breadboard
  • Sound Detector Board
  • Servo Motor
  • Jumper Cables

Connecting the Sound Detector Board and Servo to the Arduino

2016-07-22 16.47.15.jpg
Sound detector board connections
  1. Connect the pin labeled as 'envelope' on the sound detector board to the analog pin 0 on the Arduino.
  2. Connect the pin labeled as 'ground' on the sound detector board to the GND pin on the Arduino.
  3. Connect the pin labeled as 'VCC' on the sound detector board to the 3.3V pin on the Arduino.

Servo connections

  1. Connect the brown pin of the servo to the GND pin on the Arduino.
  2. Connect the orange middle pin of the servo to the 5V pin on the Arduino.
  3. Connect the beige pin of the servo to the digital pin 9 on the Arduino.

Securing the Servo Motor

2016-07-22 14.18.27 copy.jpg

Tape the servo to provide enough counter-moment for the turning motion of the control horn.

Coding

Screen Shot 2017-07-28 at 1.37.21 AM.png
//include the servo library
#include 
 //pin variable
const int soundPin = 0;
const int servoPin = 9;
int clap_on = 0;
// declare servo
Servo servo1;
//variables for storing raw sound and scaled value
int sound;
void setup()
{
  Serial.begin(9600);
  
  // attach servo to pin 9
  servo1.attach(servoPin);
  // always initialize the servo as light off.
  servo1.write (180);
  servo1.write (150);
  clap_on = 0;
}
void loop()
{
 //read and store the audio from Envelope pin
 sound = analogRead(soundPin);
 //map sound which in a quiet room a clap is 300
 //from 0 to 3 to be used with switch case
 //print values over the serial port for debugging
 Serial.println(sound);
  if (sound > 20){
    if (clap_on == 0){ // check if the servo still hasnt rotated
      servo1.write(150); // turn light on
      clap_on = 1;}  // set a toggle on clap so then next time it
      //happens it will turn the light off
    else if (clap_on == 1){
      servo1.write(180); // turn light off
      clap_on = 0;}  // set toggle again.
  }
  delay (500);
}

Demo

Voice Controlled Light

The sound module is actually not analyzing the commands I give it as information, but rather as sound signals only. That is, I can clap, snap, or make any other sound to turn it on or off. In this case, voice control worked perfectly!