Pages: [1]   Go Down
Author Topic: Nightlight  (Read 483 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Trying to build a night light but there seems to be no reaction to light or dark from the photosensor.

I'm printing what I think is the value of the photo resistor but it only prints values between 94 and 96 regardless of how light or dark it is.  Any help would be great.

Code:
/*
 * A simple programme that will change the intensity of
 * an LED based  * on the amount of light incident on
 * the photo resistor.
 *
 */

//PhotoResistor Pin
int lightPin = 0; //the analog pin the photoresistor is
                  //connected to
                  //the photoresistor is not calibrated to any units so
                  //this is simply a raw sensor value (relative light)
//LED Pin
int ledPin = 9;   //the pin the LED is connected to
                  //we are controlling brightness so
                  //we use one of the PWM (pulse width
                  // modulation pins)
void setup()
{
  pinMode(ledPin, OUTPUT); //sets the led pin to output
  Serial.begin(9600);
}
 /*
 * loop() - this function will start after setup
 * finishes and then repeat
 */
void loop()
{
 int lightLevel = analogRead(lightPin); //Read the
                                        // lightlevel
 lightLevel = map(lightLevel, 0, 900, 0, 255);
         //adjust the value 0 to 900 to
         //span 0 to 255



 lightLevel = constrain(lightLevel, 0, 255);//make sure the
                                           //value is betwween
                                           //0 and 255
 analogWrite(ledPin, lightLevel);  //write the value
   Serial.println(lightLevel);
}


* photo.JPG (889.6 KB, 2048x1536 - viewed 34 times.)
Logged

0
Offline Offline
Newbie
*
Karma: 1
Posts: 38
Turn it off!!!!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It looks like you plugged the photoresistor into the first digital pin.  Only pins A0-A5 can be used to read an analog sensor.  analogRead(0) will read from the pin marked "A0," so you can simply move the wire to that pin and your program should work.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Such a good feeling when it works.  Thank you smiley
Logged

Pages: [1]   Go Up
Jump to: