Back to Parent

Outcome


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. 

Screen shot 2021 11 10 at 8.39.45 pm.thumb
Show Advanced Options

Here's how it came out! : 

Skills dev 2 pic.thumb
Show Advanced Options

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. 

Show Advanced Options

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:

Show Advanced Options
Show Advanced Options

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. 


Screen shot 2021 11 10 at 11.29.09 pm.thumb
Show Advanced Options
With switch.thumb
Show Advanced Options

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 :)) 

Show Advanced Options
Show Advanced Options

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. 

Show Advanced Options
Show Advanced Options
Drop files here or click to select

You can upload files of up to 20MB using this form.