Voice-controlled Light Switch
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
- Arduino Uno
- Breadboard
- Sound Detector Board
- Servo Motor
- Jumper Cables
Connecting the Sound Detector Board and Servo to the Arduino
- Connect the pin labeled as 'envelope' on the sound detector board to the analog pin 0 on the Arduino.
- Connect the pin labeled as 'ground' on the sound detector board to the GND pin on the Arduino.
- Connect the pin labeled as 'VCC' on the sound detector board to the 3.3V pin on the Arduino.
Servo connections
- Connect the brown pin of the servo to the GND pin on the Arduino.
- Connect the orange middle pin of the servo to the 5V pin on the Arduino.
- Connect the beige pin of the servo to the digital pin 9 on the Arduino.
Securing the Servo Motor
Tape the servo to provide enough counter-moment for the turning motion of the control horn.
Coding
//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
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!