Interface a Photoresistor
Interface a photoresistor and turn ON and OFF an LED based on light intensity.
Components and supplies
1
Arduino UNO
Apps and platforms
1
Arduino IDE
Project description
Code
Photoresistor Ex. 1
arduino
1//Interface a photoresistor and turn ON and OFF LED based on light intensity 2 3/*Copyright 4 (c) 2019, ProteShea LLC 5All rights reserved. 6 7Redistribution and use in 8 source and binary forms, with or without 9modification, are permitted provided 10 that the following conditions are met: 111. Redistributions of source code must 12 retain the above copyright 13notice, this list of conditions and the following 14 disclaimer. 152. Redistributions in binary form must reproduce the above copyright 16notice, 17 this list of conditions and the following disclaimer in the 18documentation and/or 19 other materials provided with the distribution. 203. Neither the name of the copyright 21 holders nor the 22names of its contributors may be used to endorse or promote products 23derived 24 from this software without specific prior written permission. 25 26THIS SOFTWARE 27 IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY 28EXPRESS OR IMPLIED WARRANTIES, 29 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 30WARRANTIES OF MERCHANTABILITY AND 31 FITNESS FOR A PARTICULAR PURPOSE ARE 32DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 33 HOLDER BE LIABLE FOR ANY 34DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 35 CONSEQUENTIAL DAMAGES 36(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 37 GOODS OR SERVICES; 38LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 39 CAUSED AND 40ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 41 OR TORT 42(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 43 OF THIS 44SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 45*/ 46 47//variable 48 declarations 49int A0value = 0; 50 51void setup() { 52 53 pinMode(9, OUTPUT); 54 //LED output pin 55 digitalWrite(9, LOW); //turn LED OFF 56} 57 58void 59 loop() { 60 61 A0value = analogRead(A0); 62 if (A0value > 90){ //high 63 light intensity 64 digitalWrite(9, LOW); //turn LED OFF 65 } 66 else{ 67 //low light intensity 68 digitalWrite(9, HIGH); //turn 69 LED ON 70 } 71}
Photoresistor Ex. 2
arduino
1//Interface a photoresistor and vary LED brightness based on surrounding light intensity 2 3/*Copyright (c) 2019, ProteShea LLC 4All rights reserved. 5 6Redistribution and use in source and binary forms, with or without 7modification, are permitted provided that the following conditions are met: 81. Redistributions of source code must retain the above copyright 9notice, this list of conditions and the following disclaimer. 102. Redistributions in binary form must reproduce the above copyright 11notice, this list of conditions and the following disclaimer in the 12documentation and/or other materials provided with the distribution. 133. Neither the name of the copyright holders nor the 14names of its contributors may be used to endorse or promote products 15derived from this software without specific prior written permission. 16 17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY 18EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY 21DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27*/ 28 29//variable declarations 30int A0value = 0; 31 32void setup() { 33 pinMode(9, OUTPUT); //LED output pin 34} 35 36void loop() { 37 38 A0value = map(analogRead(A0),0,1023,0,255); //map 10-bit value to an 8-bit value 39 analogWrite(9, 255 - A0value); //we want the LED to dim as the brightness increases so have to do 255 - A0value 40 41}
Photoresistor Ex. 1
arduino
1//Interface a photoresistor and turn ON and OFF LED based on light intensity 2 3/*Copyright (c) 2019, ProteShea LLC 4All rights reserved. 5 6Redistribution and use in source and binary forms, with or without 7modification, are permitted provided that the following conditions are met: 81. Redistributions of source code must retain the above copyright 9notice, this list of conditions and the following disclaimer. 102. Redistributions in binary form must reproduce the above copyright 11notice, this list of conditions and the following disclaimer in the 12documentation and/or other materials provided with the distribution. 133. Neither the name of the copyright holders nor the 14names of its contributors may be used to endorse or promote products 15derived from this software without specific prior written permission. 16 17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY 18EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY 21DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27*/ 28 29//variable declarations 30int A0value = 0; 31 32void setup() { 33 34 pinMode(9, OUTPUT); //LED output pin 35 digitalWrite(9, LOW); //turn LED OFF 36} 37 38void loop() { 39 40 A0value = analogRead(A0); 41 if (A0value > 90){ //high light intensity 42 digitalWrite(9, LOW); //turn LED OFF 43 } 44 else{ //low light intensity 45 digitalWrite(9, HIGH); //turn LED ON 46 } 47}
Comments
Only logged in users can leave comments