Introduction: Baby Night Light

Often times, parents complain that they or their children forget to turn off night lights in the morning.  This is both unsafe and a waste of power.
To fix this problem while still keeping the traditional nighttime child comfort device in tact,  I decided to create a night light that functions specifically to comfort a baby or small child who is scared at night.  The Baby Night Light will only turn on if the following conditions are both true: the room is dark, and the child is crying.  In addition to causing a light to go on, this will trigger a speaker to play the tune to Twinkle Twinkle Little Star.  The night light will only stay on for a set period of time, and will then automatically turn and stay off unless the darkness and crying are again present.  

This project will teach you to make a small-scale Baby Night Light, which uses a photoresistor and a microphone as inputs/sensors, which detect the darkness of the room and the sound of the baby crying.  An LED and a Piezo speaker act as the outputs.  

The first step is to acquire the necessary parts:

Arduino (I used an Arduino Uno)
Breadboard
LED
Piezo speaker
photoresistor
microphone
power source
jumper wires
10kΩ resistor
470Ω resistor

Step 1: Assembly

The first step is to assemble the Arduino and breadboard. To do this, you must make circuts for the LED, the Piezo speaker, the microphone, and the photoresistor.

What I did fist was set one side row to be the Ground, and one to be 5V as these are used for multiple circuts
Next, set up each circut:

LED: negative end goes to Ground, positive end goes to one end of 470Ω resistor. The other end of the 470Ω resistor goes to pin 13

Piezo: negative goes to Ground, positive goes to Pin 9

Photoresistor: one side goes to 5V, other side goes to one side of a 10kΩ resistor, and also to analog pin 0. The other end of the 10kΩ resistor goes to Ground

Microphone: VCC goes to 5V, GND goes to Ground, OUT goes to analog pin 1


Step 2: Begin Coding

Once your arduino and breadboard have been assembled physically, it is time to tell the arduino, the brain of your nightlight, what you want it to do.
The first step to this is to declare all variables.
"int" is used to declare most variables, however you will notice that one is "unsigned long". This is basically an extended size variable for number storage.

Step 3: Finishing the Code

Now that all variables have been declared, it is time to tell your arduino what to do with these variables. The goal is to have your photoresistor and microphone as input sensors that cause the LED to go on and the piezo to play a tune.

Looking at the picture, the
if (lightSensorValue < 700 && soundSensorValue > 800)

tells your arduino that if the amount of light sensed is under 700 and the sound detected is over 800 then the light will turn on

digitalWrite(ledPin, HIGH)

At the same time the next part of the code is telling your arduino to play the melody.

for (int thisNote = 0; thisNote < length; thisNote++) {
 
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000/noteDurations[thisNote];
tone(9, melody[thisNote],noteDuration);
if (millis() > lastTimechecked + sleepduration) {
break;
}
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(9);



*Your completed code is this:*

int lightSensorPin = 0; 
int lightSensorValue = 0; 

int ledPin = 13; 

int soundSensorPin = 1;
int soundSensorValue = 0; 

int speakerPin = 9;

int length = 42; 

//523=C, 784=G, 880=A, 698=F, 659=E, 587=D
int melody[] = {
523, 523, 784, 784, 880, 880, 784,
698, 698, 659, 659, 587, 587, 523,
784, 784, 698, 698, 659, 659, 587,
784, 784, 698, 698, 659, 659, 587,
523, 523, 784, 784, 880, 880, 784,
698, 698, 659, 659, 587, 587, 523
};


// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
  4, 4, 4, 4, 4, 4, 2,
  4, 4, 4, 4, 4, 4, 2,
  4, 4, 4, 4, 4, 4, 2,
  4, 4, 4, 4, 4, 4, 2,
  4, 4, 4, 4, 4, 4, 2,
  4, 4, 4, 4, 4, 4, 2
};

unsigned long lastTimechecked = 0;
int sleepduration = 18000;//how long music plays for

void setup () {
  Serial.begin (9600);//serial monitor
  pinMode (ledPin, OUTPUT);
}

void loop ()  {
  lightSensorValue = analogRead (lightSensorPin);//read light sensor value
  soundSensorValue = analogRead (soundSensorPin);//read microphone sensor value
  Serial.println (soundSensorValue);

  if (lightSensorValue < 700 && soundSensorValue > 800)  {
 
    lastTimechecked = millis ();
   
    digitalWrite(ledPin, HIGH); //turn on LED
   
    for (int thisNote = 0; thisNote < length; thisNote++) {

 
    int noteDuration = 1000/noteDurations[thisNote];
    tone(9, melody[thisNote],noteDuration);
     if (millis() > lastTimechecked + sleepduration) {
        break;
     }
   
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);

    noTone(9);
  }
    delay (1000);
  }
  else  {
    digitalWrite(ledPin, LOW); 

  }

}

Step 4: The Finished Product

The final piece to creating a Baby Night Light is attaching your arduino to your computer and uploading the code. If you want to test it out without crying into the microphone, try blowing into it, as this gives a high enough frequency to activate it. Enjoy!