skills dev II

Made by Tiffany Han

combining 2 sensors/input (photosensor and a button)

Created: November 8th, 2023

0

1. Outcome

The final work I completed combines input from a photocell sensor as well a button. To complete this, I wired the two inputs/sensors separately and used a digital and analog pin for each. Here is a demonstration of the wiring:


0

Here is a video for it working. The button controls whether the system if on/off. When it is held down, the LED is turned on and the photo sensor will change the brightness of LED (lower lighting would lower the LED brightness). This is the code that controls it:

0
int photoCellPin = A0;
int photoCellReading = 0;
int ledPin = D2;
int ledBrightness = 0;
int buttonPin = D4;

void setup() {
    pinMode(ledPin, OUTPUT);
    Particle.variable("light", &ledBrightness, INT);
    pinMode( buttonPin , INPUT_PULLUP);
    
}

void loop() {
    int buttonState = digitalRead( buttonPin );

     if( buttonState == LOW ){
     photoCellReading = analogRead(photoCellPin);
     ledBrightness = map(photoCellReading, 3000, 4095, 0, 255);
     printf("%d", ledBrightness);
     analogWrite(ledPin, ledBrightness);
     delay(100);
     }
     
    else{
      digitalWrite( ledPin, LOW);
    }
    
    Particle.publish("light");

}
Click to Expand
0

The process of developing is was quite straight forward: I first only added one sensor (the photosensor). At first the brightness didn't change a lot, and after some thinking, I found that changing the input range of the map function to be smaller will make the brightness change more noticeable. After I got this system working, I added a button as a second input, and revised the code to include the button state. 

0

Reflection:

I definitely became a lot more familiar with how to connect input, and make dynamic changes to the system using if statements. I enjoyed this lab because often, the IoTs in daily lives will involve at least one user inputs. One improvement I'd make is perhaps making the brightness of the LED increase (instead of decrease which is what I have done) when it is darker, because that would make more sense in real life. 

x
Share this Project

Courses

48-675 Designing for the Internet of Things 24

· 11 members

A hands-on introductory course exploring the Internet of Things and connected product experiences.


About

combining 2 sensors/input (photosensor and a button)