Diming light with a sensor

Made by Brian Hernandez

The objective is to make the LED light slowly light up when there is not a lot of light and vice versa.

Created: December 6th, 2023

0

Intention

The intention behind this project is to make it so that the LED light turns on when there is no light. This will be very helpful for anyone who needs a nightlight or is scared of the dark.

0
// Define a pin that we'll place the photo cell on
// Remember to add a 10K Ohm pull-down resistor too.
int photoCellPin = A0;

// Create a variable to hold the light reading
int photoCellReading = 0;

// Define a pin we'll place an LED on
int ledPin = D2;

// Create a variable to store the LED brightness.
int ledBrightness = 0;

void setup()
{
  // Set up the LED for output
  pinMode(ledPin, OUTPUT);

  // Create a cloud variable of type integer
  // called 'light' mapped to photoCellReading
  Particle.variable("light", &photoCellReading, INT);

}

void loop() {
  photoCellReading = analogRead(photoCellPin);
  Serial.println("Photo Cell Reading: " + String(photoCellReading));

  ledBrightness = map(photoCellReading, 0, 4095, 0, 255);
  Serial.println("LED Brightness: " + String(ledBrightness));

  analogWrite(ledPin, ledBrightness);
  delay(100);
}
Click to Expand
0

I first started with this code that allowed us to know how much light there is available. (Shown above on the breadboard). I did run into some problems early on. I did not choose the right resistor when I first started. I also made the mistake of not connecting the resistor next to D2. Then I forgot to move the wire to the correct spot after changing the resistor. Then at the end when I thought I had solved all the problems I realized that I forgot to wire the red wire to the (+). 

0

Then I proceeded to modify the code so that I could dim the lights so that there was more light when the lights were dim to off. I also attached a short video demonstrating how the light works. Alongside the code. Changing the code so that it dimmed was pretty simple. It only involved changing the line that talks about ledBrightness.

0
0

Product

I created a product that can light up any area that is in darkness. It would also help save energy if there is light already there because what it would do in that case is dim or turn off. 

I learned that connecting the breadboard is just as important as the code itself. Making sure you connected all the pieces in the correct spots. 

There could be more that I could add. For example adding a button to turn the light on or off. I could use multiple LED lights and make it light up more. 

0
int photoCellPin = A0;

// Create a variable to hold the light reading
int photoCellReading = 0;

// Define a pin we'll place an LED on
int ledPin = D2;

// Create a variable to store the LED brightness.
int ledBrightness = 0;

void setup()
{
  // Set up the LED for output
  pinMode(ledPin, OUTPUT);

  // Create a cloud variable of type integer
  // called 'light' mapped to photoCellReading
  Particle.variable("light", &photoCellReading, INT);

}

void loop() {
  photoCellReading = analogRead(photoCellPin);
  Serial.println("Photo Cell Reading: " + String(photoCellReading));

  ledBrightness = map(photoCellReading, 0, 4095, 255, 0);
  Serial.println("LED Brightness: " + String(ledBrightness));

  analogWrite(ledPin, ledBrightness);
  delay(100);
}
Click to Expand
x
Share this Project

Courses

48-675 Designing for the Internet of Things

· 11 members

A hands-on introductory course exploring the Internet of Things and connected product experiences.


About

The objective is to make the LED light slowly light up when there is not a lot of light and vice versa.