Skills Dev II: Working with Inputs and Sensors

Made by Weiqing Wang

An energy-saving light control device with 2-in (sensors or inputs) 1-out (LED) This device is created using a photoresistor, a potentiometer and an LED. If the surrounding lightness is below a certain point, the LED will be turned on and you can control its lightness as you wish. If it is light enough, the LED will automatically be turn off to save energy.

Created: November 12th, 2022

0

Outcome

0

Process

I first get the photoresistor and LED circuit workd and then add the potentiometer in. The circuit I built is a bit complex with many wires since I got each item connect to + and - seperately.

The first problem I encountered is forgetting to use 10k resistor with photoresistor. This makes my first circuit not function.

The second problem is about the code. I excluded the lines to control lightness with potentiometer out of the if statement at the start. This makes these lines cover the if statement since our outputs are all read to the same ledPin. 

0

Reflection

Other sensors and inputs can be further tried when we learn other outputs like sound. 

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;
// Define a pin that we'll place the pot on
int potPin = A5;
// Create a variable to hold the pot reading
int potReading = 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 'light' mapped to photoCellReading
  Particle.variable("pot", potReading );
}

void loop() {
// Use analogRead to read the photo cell reading
 // This gives us a value from 0 to 4095
  photoCellReading = analogRead(photoCellPin);
   // Use analogRead to read the potentiometer reading
  // This gives us a value from 0 to 4095
  potReading = analogRead(potPin);
  
  // Turn the light off when there's enough natural light; when light is on, can control strength
if( photoCellReading < 2500 ){
    digitalWrite( ledPin, HIGH);
    //   Map lightness into the PWM range (0-255)
    ledBrightness = map(potReading, 0, 4095, 0, 255);
  // fade the LED to the desired brightness
    analogWrite(ledPin, ledBrightness);
}else{
    digitalWrite( ledPin, LOW);
    }
//   wait 1/10th of a second and then loop
  delay(100);
}
Click to Expand
0
48675 - Design for IoT - Skill Exercise 2 - Working with Inputs and Sensors
Weiqing Wang - https://youtube.com/shorts/AYLalj3egXk?feature=share
x
Share this Project

Courses

About

An energy-saving light control device with 2-in (sensors or inputs) 1-out (LED)

This device is created using a photoresistor, a potentiometer and an LED. If the surrounding lightness is below a certain point, the LED will be turned on and you can control its lightness as you wish. If it is light enough, the LED will automatically be turn off to save energy.