Skill Dev II: Working with Photoresistor

Made by KRITTIKA BHATNAGAR

Created: December 6th, 2022

0

Outcome


0

Process

The process of creating the circuit and the coding was pretty straightforward. I followed the code in the DiOT labs website to help set up the code. Initially, I used the wrong resistor and the response of the LED was better. When I switched out to use a resistor with a higher resistance, then the response of the LED was dimmer which makes sense since the resistance in the circuit increased. 

0

Reflection

This was my first time using a photoresistor. It was interesting to code based on the intensity of the light and it was good to see how sensors can be connected to IoT devices. 

0
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()
{
  // 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, 0, 4095, 0, 255);

  // fade the LED to the desired brightness
  analogWrite(ledPin, ledBrightness);

  // wait 1/10th of a second and then loop
  delay(100);
}
Click to Expand
x