Back to Parent

//Practice exercise: Using a photoresistor

int photoCellPin = A0;
int photoCellReading = 0; //create a variable to hold the light reading

int ledPin = D2;
int ledBrightness = 0; //create a variable to store the LED brightness

void setup() {

    //setup the LED for output
    pinMode(ledPin, OUTPUT);
    Particle.variable("light", &photoCellReading, INT); //create a cloud variable of type integer

void loop() {

    photoCellReading = analogRead(photoCellPin); //read the photo cell reading, 0-4095
    ledBrightness = map(photoCellReading, 0, 4095, 0, 255); //map this value into the PWM range (0-255) and store as led brightness

    analogWrite(ledPin, ledBrightness); //fade the led to the desired brightness
    delay(100); //wait 1/10th of a second and then loop
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!