Skills Dev II: Working with Inputs and Sensors

Made by Ponn Thanasathit

The objective is to create a working LED circuit that is controlled by 3 inputs, a photoresistor, a potentiometer, and a switch. This circuit is a simplified version of a street lamp, which will be automatically turned on when the light is out. In addition to the basic street lamp feature, I've added a switch to turn it on and off and a potentiometer to adjust the brightness.

Created: November 5th, 2022

0

Outcome

    • Please find the short video via this link: https://drive.google.com/file/d/1z_O_D3Y6GcYzHkcbgDPCEPr97wdsbxfn/view?usp=sharing

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)

My approach to this project was to modularize each component and tried to make sure that they work on their own before adding in the next component. This was proven to be successful because it was very easy to debug my code.


0

Reflection

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


This was a very fun project and I learned a lot. I could have spent more time reinventing the finished product but I tried to reutilize all the components that were used in the prior exercises.

0
// Exercise 2

// Define a pin that we'll place the pot on
int potPin = A5;

// Create a variable to hold the pot reading
int potReading = 0;

// Define a pin we'll place an LED on
int ledPin = D3;

// Create a variable to store the LED brightness.
int ledBrightness = 0;

// Our button wired to D3
// We wire D0 to the middle terminal on the switch
// And any of the two other terminals to ground
int switchPin = D4;

// 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;

//
void setup(){

  // Set up the LED for output
  pinMode(ledPin, OUTPUT);
  
  // sets pin as input
  pinMode(switchPin , INPUT_PULLUP); 

  // Create a cloud variable of type integer
  // called 'light' mapped to photoCellReading
  Particle.variable("pot", potReading );
  
  // 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
  int buttonState = digitalRead( switchPin );
  photoCellReading = analogRead(photoCellPin);
  ledBrightness = map(photoCellReading, 0, 4095, 0, 255);
  
  // fade the LED to the desired brightness
  //   analogWrite(ledPin, ledBrightness);

  if( buttonState == LOW )
  {  
    
  // turn it on or off when the light reaches certain levels.
  if( photoCellReading <= 600 )
  {
    // turn the LED On
    digitalWrite( ledPin, HIGH);
      // Use analogRead to read the potentiometer reading
      // This gives us a value from 0 to 4095
      potReading = analogRead(potPin);
    
      // Map this value into the PWM range (0-255)
      // and store as the led brightness
      ledBrightness = map(potReading, 0, 4095, 0, 255);
    
      // fade the LED to the desired brightness
      analogWrite(ledPin, ledBrightness);    
    
  }else{
    // otherwise
    // turn the LED Off
    digitalWrite( ledPin, LOW);
  }
  }else{
    // otherwise
    // turn the LED Off
    digitalWrite( ledPin, LOW);
  }
}
Click to Expand
x
Share this Project

Courses

About

The objective is to create a working LED circuit that is controlled by 3 inputs, a photoresistor, a potentiometer, and a switch. This circuit is a simplified version of a street lamp, which will be automatically turned on when the light is out. In addition to the basic street lamp feature, I've added a switch to turn it on and off and a potentiometer to adjust the brightness.