Grace Skills Dev II: Button & Photoresistor

Made by Grace Zhu

The goal is to practice making a 2-in (sensors or inputs) 1-out (LED) device.

Created: November 6th, 2024

0

Intention

The goal of the project is to learn about sensors and practice making a 2-in  1-out device. I selected to learn about using push button and photoresistor to control an LED . 

0

Process

I first practiced making a circuit using the photoresistor to control the brightness of an LED. Then I practiced making another one using push button to control the on/off of the LED. After following those tutorials, I tried to put both controls together in a circuit.

0

Product

I created a circuit where you can turn on and off of the LED with a push button. When the LED is on, it will adjust to the brightness of the environment sensed by the photoresistor. If the environment is brighter, the LED is dimmer, and when it's darker, the LED is brighter.

0

Reflection

It took me a long time to make the photoresistor work on its own. After receiving help, I learned ways to adjust the range of the brightness mapping and to use the terminal to monitor the reading of the sensor, in order to see the changes in LED brightness better.

0
int ledPin = D1;

// Our button wired to D0
int buttonPin = D3;

int photoCellPin = A0;
int photoCellReading = 0;
int ledBrightness = 0;

void setup()
{

  // For input, we define the
  // pushbutton as an input-pullup
  // this uses an internal pullup resistor
  // to manage consistent reads from the device

  pinMode( buttonPin , INPUT_PULLUP); // sets pin as input

  // We also want to use the LED

  pinMode( ledPin , OUTPUT ); // sets pin as output

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

void loop()
{
   // find out if the button is pushed
   // or not by reading from it.
   int buttonState = digitalRead( buttonPin );
   Serial.println(buttonState);

  // remember that we have wired the pushbutton to
  // ground and are using a pulldown resistor
  // that means, when the button is pushed,
  // we will get a LOW signal
  // when the button is not pushed we'll get a HIGH

  // let's use that to set our LED on or off

  if( buttonState == LOW )
  {
    // turn the LED On
    digitalWrite( ledPin, HIGH);
    
    // Use analogRead to read the photo cell reading
    // This gives us a value from 0 to 4095
    photoCellReading = analogRead(photoCellPin);
    
    // Map this value into the PWM range (0-255)
    // and store as the led brightness
    ledBrightness = map(photoCellReading, 2000, 4095, 0, 255);
    
    // fade the LED to the desired brightness
    analogWrite(ledPin, 255 - ledBrightness);
    
    // wait 1/10th of a second and then loop
    delay(100);
    

  }else{
    // otherwise
    // turn the LED Off
    digitalWrite( ledPin, LOW);

  }


}
Click to Expand
0
Button Photoresistor Circuit
Grace Z - https://youtu.be/aIiIZNB8zT0
x
Share this Project


Focused on
About

The goal is to practice making a 2-in (sensors or inputs) 1-out (LED) device.