Skills Dev II: Working with Inputs and Sensors

Made by Alp Erbug

Given the amount of light in the environment, adjust the brightness of the light using a potentiometer, an LED, and a photoresistor.

Created: November 9th, 2022

0

Outcome

The circuit is composed of a photoresistor, an LED light, and a potentiometer along with the corresponding resistors and wires. The photoresistor detects the light in an environment, and if the light is below a specific level, using the potentiometer, the user is able to adjust the brightness of the LED by turning the knob.

0

Process

I first planned to use a temperature sensor for this project, but as I was setting up my circuit and reading about the sensors, I realized working with a temperature sensor during the second week of class might be more challenging, and that it might be harder to control the temperature as opposed to light to test my code and wiring. I therefore decided to switch to photoresistor. Wiring was not challenging, but it took me some time to get my code working, especially calibrating the photoresistor measurement and the potentiometer reading, and mapping out these values to see the effects on the LEDs I used. 

0

Reflection

It was fun to connect two different sensors in this project. It was also exciting to see instant effect of running the code and testing if everything is working as intended. The photoresistor required some fine tuning, and at first I wasn't able to observe the effects of light or turning the potentiometer knob. It is therefore important to make sure your components work as intended before trying to complicate the code or the circuit.

0
// Defining pins
// Define a pin that we'll place the photo cell on
int photoCellPin = A0;
// Define a pin that we'll place the potentiometer on
int potPin = A5;
// Define a pin that we'll place the LED on
int ledPin = D2;

// Defining variables
// Create a variable to hold the light reading
int photoCellReading = 0;
// Create a variable to hold the pot reading
int potReading = 0;
// Create a variable to hold the LED reading
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);
    // Create a cloud variable of type integer called 'potReading'
    Particle.variable("potReading", potReading);

}

void loop() {
    // Use analogRead to read the photo cell reading, value from 0 to 4095
    photoCellReading = analogRead(photoCellPin);
    // Use analogRead to read the potentiometer reading
    potReading = map(analogRead(potPin), 0,4095, 0, 255);
    // Map this value into the PWM range (0-255)
    ledBrightness = map(photoCellReading, 0, 4095, 0, 255);
    // Fade the LED to the desired brightness
        if(photoCellReading < 2000) {
            analogWrite(ledPin, potReading);
        } else {
            digitalWrite(ledPin, LOW);
        }
    delay(100);
}
Click to Expand
0
Working with Inputs and Sensors
Alp Erbug - DIoT - https://www.youtube.com/shorts/4doKJ58tvXs
x
Share this Project

Courses

About

Given the amount of light in the environment, adjust the brightness of the light using a potentiometer, an LED, and a photoresistor.