Skills Dev II: Working with Inputs and Sensors

Made by Everett Jiaxi Gu · UNLISTED (SHOWN IN POOLS)

The sensor allows the LED brightness to be adjusted with the ambient light brightness.

Created: November 10th, 2022

0

Outcome

    • After practicing the control of a single sensor, an attempt was made to combine photoresistors, LEDs and potentiometers to achieve an LED light that can be automatically turned on when the ambient light is very low, and the user can also adjust the LED brightness through a potentiometer after it is turned on.

0

Process

Describe the process you underwent to reach the outcome (problems encountered, how you resolved them, as well as, experiments, hacks, tests, refinments, iterations, failures):

I first tried to control a single sensor and potentiometer according to the tutorial on the website, and then combined them to achieve a more complete control mode. During the process I adjusted the ambient light intensity threshold for the LED to turn on or off automatically and made sure that the light was on when the ambient light was dim enough.

0

Initial Practice of Sensors

0
int potPin = A5;
int potReading = 0;
int ledPin = D2;
int ledBrightness = 0;
int switchPin = D4;


void setup() {
    Particle.variable("pot", potReading);
    pinMode(ledPin, OUTPUT);
    pinMode(switchPin, INPUT_PULLUP);

}

void loop() {
    int buttonState = digitalRead(switchPin);
    
    if(buttonState == LOW) {
        digitalWrite(ledPin, HIGH);
        potReading = analogRead(potPin);
        ledBrightness = map(potReading, 0, 4095, 0, 255);
        analogWrite(ledPin, ledBrightness);
        delay(100);
    } else {
        digitalWrite(ledPin, LOW);
    }
    
}
Click to Expand
0
int led = D2;
int switch = D3;
int lightPin = A3;
int lightNum = 0;
int ledBrightness = 0;

void setup() {
    pinMode(switchPin, INPUT_PULLUP);
    pinMode(led, OUTPUT);
    Particle.variable("light", lightNum);

}

void loop() {
    int buttonState = digitalRead(switch);
    
    if(buttonState == LOW){
        digitalWrite(led, HIGH);
        lightReading = analogRead(lightSensorPin);
        ledBrightness = map(lightReading, 4095, 2000, 0, 255);
        analogWrite(led, ledBrightness);
        delay(500);
    }
    else{
        digitalWrite(led, LOW);
    }
}
Click to Expand
0

Reflection

Q: Reflect on the process of making this project. What did you learn? What would you do differently?

There are still many flaws from a design perspective. It is not easy to manually adjust the potentiometer when the ambient light is too low. In addition the threshold value for automatic on or off can be further adjusted.

Next I need to try to conceptualize other types of sensors for application scenarios that are not limited to using ambient light data. I also want to try more output devices, such as other types of LED light sources, and more complex sensor combination patterns.

0

Code for Skills Dev II:

0
//Define the pin for photo cell and resistor is added
int phoCellPin = A0;
//Variable to store data of light
int phoCellReading = 0;
//Define the pot pin
int potPin = A5;
//Variable for pot reading
int potReading = 0;
//Define LED pin
int ledPin = D2;
//Variable to stor data of LED
int ledBrightness = 0;

void setup() {
    //Led outpou setup
    pinMode(ledPin, OUTPUT);
    //Cloud variable mapped to phoCellReading
    Particle.variable("LightData", &phoCellReading, INT);
    //Cloud variable mapped to potReading
    Particle.variable("PotData", potReading);
}

void loop() {
    //Read photo cell reading data
    phoCellReading = analogRead(phoCellPin);
    //Read pot reading data
    potReading = analogRead(potPin);
    //Turn light on when interior light is insufficient
    //Light intensity adjustable through potentiometer
    if(phoCellReading > 500) {
        digitalWrite(ledPin, LOW);
    } else {
        digitalWrite(ledPin, HIGH);
        ledBrightness = map(potReading, 0, 4095, 0, 255);
        analogWrite(ledPin, ledBrightness);
    }
    //Wait time between each loop
    delay(300);
}
Click to Expand
0
x
Share this Project

This project is only listed in this pool. Be considerate and think twice before sharing.


Courses

About

The sensor allows the LED brightness to be adjusted with the ambient light brightness.