header
Hook Up a Speaker
hook up a speaker

Use the code below to make sure you're getting some sound out of your speaker.

// include the library code
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12,11,5,4,3,2);

// initialize our variables
int sensorPin = 0;
int photocellPin = 1;
int speaker = 10;
int tempC, tempF, lightReading;

// some notes to use on the speaker
int G = 392;
int C = 523;

void setup() {
  // set up the LCD's number of columns and rows
  lcd.begin(16, 2);
  
  // set up speaker
  pinMode(speaker, OUTPUT);
  // play some notes to make sure speaker is connected correctly
  tone(speaker, G);
  delay(200);
  tone(speaker, C);
  delay(200);
  tone(speaker, G);
  delay(200);
  noTone(speaker);
}

void loop() {  
  tempC = get_temperature(sensorPin);
  tempF = celsius_to_fahrenheit(tempC);
  lightReading = get_light_reading(photocellPin);
  
  lcd.setCursor(0,0);
  lcd.print(tempF); lcd.print(" "); lcd.print((char)223); lcd.print("F");

  lcd.setCursor(0,1);
  lcd.print("Light: "); lcd.print(lightReading); lcd.print(" lux");
  delay(200);
}

int get_light_reading(int pin) {
  int photocellReading = analogRead(pin);
  return photocellReading;
}

int get_temperature(int pin) {
  // We need to tell the function which pin the sensor is hooked up to. We're using
  // the variable pin for that above
  
  // Read the value on that pin
  int temperature = analogRead(pin);
  
  // Calculate the temperature based on the reading and send that value back
  float voltage = temperature * 5.0;
  voltage = voltage / 1024.0;
  return ((voltage - 0.5) * 100);
}

int celsius_to_fahrenheit(int temp) {
  return (temp * 9 / 5) + 32;
}

After your code is uploaded, you should hear some sounds that play once.

Next: Programming alarm