Challenge Week 5 Photo Resistor

 1. Project: Photo Resistor: This changes resistance based on the amount of light the sensor receives. 

2. Code: 


3. Picture of the circuit:  


4. Electronic Diagram:


5. Video explanation

Code Explanation


    6. Challenge thinking: Started with adding wires and parts to the board. First the wires to ground to positive and 5v to negative.  Next the photoresistor into f5 and 6, a 10K resistor on one side connecting to i 1 and 5 then a wire to the negative side of the board from the resistor. On the other side of the 10K resistor, I connected  a wire to Analog port A0. The other side of the resistor connects to the negative side of the board with a wire. The LED into h20 positive and h21 negative.  Then a wire connects it to port 9 on the positive side and a 330K resistor to the negative side which connects to the negative side of the board. 

The code: Started with the  const int sensorPin = A1; const int ledPin = 9; and int lightLevel, high = 0, low = 1023;.

Added pinMode(ledPin, OUTPUT); to void setup. Then lightLevel = analogRead(sensorPin); to void loop.  You have to lightLevel = map(lightLevel, 0, 1023, 0, 255); because analogRead does not return a high level of 0 to 1023 this changes the values to 0 to 255. Next, you have to add lightLevel = constraint(lightLevel, 0, 255)This clips the numbers into a given range. If the number is above then it resets to the highest number and if below then it resets to the lowest number. This will make the light not turn off or on completely so we have to add manual tune or autotune. This will alter the light level to cover the range 'from full on to full off.

After that add analogWrite(ledPin, lightLevel); This will brighten the led with the light level then void manualTune. To make the light go completely on or off you have to change the value of the 0 or 1023  by making the large number lower and the low number larger in the light level lineslightLevel = map(lightLevel, 0, 1023, 0, 255); lightLevel = constraint(lightLevel, 0, 255);

Finally the void autoTune() When using autoTune the computer will keep track of the highest and lowest numbers. so I put in if (lightLevel < low)  {low = lightLevel }  and if (lightLevel > high{ high = lightLevel;} Lastly, stick them into the map function by adding lightLevel = map( lightLevel, low+30, high-30, 0, 255);  lightLevel = constrain(lightLevel, 0, 255);

When I first ran the code, the light flashed and then it did not work; so I checked all the wires and the code. The few mistakes I made were: I had the wrong Analog pin written in my code so I  changed it to A0, which was the port my wire was plugged into; I also realized I did not close my last part of my code, so I added in a close bracket at the bottom of my code. Once I fixed all of these, it worked!



7. Final reflection: Last time, in week 3 when I tried to get the photoresistor to work with the RGBLED and the temp sensor, I could not get it to really work. I now realize that I think I left out the part of the code with lightLevel = map(lightLevel, 0, 1023, 0, 255) and lightLevel = constrain(lightLevel, 0, 255) and the autoTune part. With out these, it does not monitor the light level and get a good read  because analog Read does not return a high level. I definitely got my confidence back in completing this challenge, and I might consider trying to add in the RGB LED. I think I might be able to get it to work. After attempting the RGB LED with the photo resistor, I was unsuccessful.


8. Possible Extensions: This could be outside lights that come on when dark, or even night lights that have sensors on them to turn on when the lights go out. My kids have always had to have a night light on. 


Extended Challenge: Back in week 3, I attempted to control a RGB LED with a photo resistor and was unsuccessful, so I really wanted to try again. I added the RGB LED code to my photo resistor code and was successful in adding the code. When I went to try it on the board and attempted to cover the photo resistor or shine a bright light on it, I could not tell any difference in the RGB LED. I had a few mistakes when first putting the board together, such as my wires in the wrong places which caused my RGB LED to only blink in blue. Once I fixed it, and was blinking all colors but still I could not see it change with the photo resistor. 




 Code: 



Videos








Comments