Priya Jain - Skills Dev 2

Made by Priya Jain

The goal of this project is experiment with using sensors to take in information and reflect the output with an LED light. Controls such as switches and buttons are also explored.

Created: November 10th, 2021

0

I started off by using the photoresistor. The photoresistor is a light sensitive resistive sensor which means that as a lot of light hits the top of the sensor, it has low resistance. 

I followed the diagram below create a circuit that lights up an LED when light hits the photoresistor. 

0

Here's how it came out! : 

0

Now for the Code! 

I wrote up the code below. It basically reads the input from the photo-resistor at pin A0 and maps that onto the scale for LED pwm (pulse-width-modulation). That number is stored in a variable called ledBrightness and is reflected when the LED lights up by analogWrite statement. 

0
int photoCellPin = A0;
int photoCellReading = 0;

int ledPin = D2;
int ledBrightness = 0;


void setup() {
    
    pinMode(ledPin, OUTPUT);
    Particle.variable("light", &photoCellReading, INT);

}

void loop() {
    
    photoCellReading = analogRead(photoCellPin);
    ledBrightness = map(photoCellReading, 0, 4095, 0, 255);
    
    analogWrite(ledPin, ledBrightness);
    delay(100);
    
}
Click to Expand
0

This worked! Yay! However, the mapping scale was not making the LED show enough contrast between the dimmest and brightest frequency. Therefore I changed the mapping to the code below:

0
ledBrightness = map(photoCellReading, 1400, 3095, 0, 255);
Click to Expand
0
skillsdev2 part1 (photoresistor)
Priya Jain - https://youtu.be/V0A81Tg8oEw
0

The next exercise I tried was combining a switch button input with the photo-resistor and LED output. Here was the diagram I used to integrate a button into the mix. 


0

Here is the completed circuit with the switch. When the switch is turned on, the photo-resistor takes in a light reading and it displays on the LED (with it being lit up if there is a lot of light). When the switch is turned off, the LED light is off too. 

I had a tough time understanding what was going wrong with my circuit because it was not working at first. I was connected the terminal of the switch to ground, but not to the power. Also in my code, I was only reading the switch input once. I had to move that line into the loop. 

Now I got it working :)) 

0
SkillsDev2 with switch and sensor
Priya Jain - https://www.youtube.com/watch?v=L41PMZ28PtU
0
int photoCellPin = A0;
int photoCellReading = 0;

int ledPin = D2;
int ledBrightness = 0;

int switchPin = D3;
int buttonState = LOW;




void setup() {
    
    Particle.variable("light", &photoCellReading, INT);
    Particle.variable("button", buttonState);
    
    pinMode( switchPin , INPUT_PULLUP); // sets pin as input
    pinMode( ledPin , OUTPUT ); // sets pin as output

}

void loop() {
    
    photoCellReading = analogRead(photoCellPin);
    ledBrightness = map(photoCellReading, 1400, 3095, 0, 255);
    
    // buttonState = HIGH;
    buttonState = digitalRead(switchPin);
    
    if( buttonState == HIGH )
  {
    analogWrite(ledPin, ledBrightness);
    delay(100);
  }else{
    digitalWrite( ledPin, LOW);

  }

}
Click to Expand
0

To me, having the LED light up when the photo-resistor detects less light makes more sense to me. This way the LED is offering more light in a darker environment. To do this, I had to change the mapping for the ledBrightness, which was one line of code. 

0
ledBrightness = map(photoCellReading, 1400, 3095, 255, 0);
Click to Expand
0
SkillsDev2 part 2
Priya Jain - https://youtu.be/6W2zxlyQsLQ
x
Share this Project


About

The goal of this project is experiment with using sensors to take in information and reflect the output with an LED light. Controls such as switches and buttons are also explored.