Skip to content

Instantly share code, notes, and snippets.

@domenicomonaco
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save domenicomonaco/9448751 to your computer and use it in GitHub Desktop.
Save domenicomonaco/9448751 to your computer and use it in GitHub Desktop.
Arduino Photoresistor
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<style>
h1,
h2,
h3,
h4,
h5,
h6,
p,
blockquote {
margin: 0;
padding: 0;
}
body {
font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", Arial, sans-serif;
font-size: 13px;
line-height: 18px;
color: #737373;
background-color: white;
margin: 10px 13px 10px 13px;
}
table {
margin: 10px 0 15px 0;
border-collapse: collapse;
}
td,th {
border: 1px solid #ddd;
padding: 3px 10px;
}
th {
padding: 5px 10px;
}
a {
color: #0069d6;
}
a:hover {
color: #0050a3;
text-decoration: none;
}
a img {
border: none;
}
p {
margin-bottom: 9px;
}
h1,
h2,
h3,
h4,
h5,
h6 {
color: #404040;
line-height: 36px;
}
h1 {
margin-bottom: 18px;
font-size: 30px;
}
h2 {
font-size: 24px;
}
h3 {
font-size: 18px;
}
h4 {
font-size: 16px;
}
h5 {
font-size: 14px;
}
h6 {
font-size: 13px;
}
hr {
margin: 0 0 19px;
border: 0;
border-bottom: 1px solid #ccc;
}
blockquote {
padding: 13px 13px 21px 15px;
margin-bottom: 18px;
font-family:georgia,serif;
font-style: italic;
}
blockquote:before {
content:"\201C";
font-size:40px;
margin-left:-10px;
font-family:georgia,serif;
color:#eee;
}
blockquote p {
font-size: 14px;
font-weight: 300;
line-height: 18px;
margin-bottom: 0;
font-style: italic;
}
code, pre {
font-family: Monaco, Andale Mono, Courier New, monospace;
}
code {
background-color: #fee9cc;
color: rgba(0, 0, 0, 0.75);
padding: 1px 3px;
font-size: 12px;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;
}
pre {
display: block;
padding: 14px;
margin: 0 0 18px;
line-height: 16px;
font-size: 11px;
border: 1px solid #d9d9d9;
white-space: pre-wrap;
word-wrap: break-word;
}
pre code {
background-color: #fff;
color:#737373;
font-size: 11px;
padding: 0;
}
sup {
font-size: 0.83em;
vertical-align: super;
line-height: 0;
}
* {
-webkit-print-color-adjust: exact;
}
@media screen and (min-width: 914px) {
body {
width: 854px;
margin:10px auto;
}
}
@media print {
body,code,pre code,h1,h2,h3,h4,h5,h6 {
color: black;
}
table, pre {
page-break-inside: avoid;
}
}
</style>
<title>Intro </title>
</head>
<body>
<h2>Intro </h2>
<ul>
<li>Contributor: Domenico Monaco <a href="&#109;&#97;&#105;&#x6c;&#116;&#111;&#58;&#100;&#111;&#109;&#101;&#x6e;&#x69;&#x63;&#111;&#46;&#109;&#111;&#110;&#97;&#99;&#111;&#x40;&#x6b;&#105;&#117;&#122;&#x2e;&#105;&#x74;">&#x64;&#111;&#x6d;&#x65;&#x6e;&#x69;&#99;&#111;&#x2e;&#109;&#111;&#x6e;&#x61;&#x63;&#111;&#x40;&#107;&#105;&#x75;&#122;&#x2e;&#x69;&#116;</a></li>
<li>Link: <a href="http://kiuz.it">http://kiuz.it</a></li>
<li>Tags: Arduino, photoresistor, light-dependent, LDR, photocell</li>
<li><p>License: MIT Licence</p></li>
<li><p>The code is partly taken from:</p>
<ul>
<li> Scott C - <a href="http://bildr.org/2012/11/photoresistor-arduino/">http://bildr.org/2012/11/photoresistor-arduino/</a></li>
<li> <a href="http://arduinobasics.blogspot.it/2011/06/arduino-uno-photocell-sensing-light.html">http://arduinobasics.blogspot.it/2011/06/arduino-uno-photocell-sensing-light.html</a></li>
</ul>
</li>
</ul>
<h2>Description</h2>
<p>This is very simple code to use photoresistor (LDR) with Arduino.</p>
<p>In a "void setup()" the code read the the input and creates empirically two level (max and min), where "min" is simply smaller at will.</p>
<p>So in the "void loop()" the code reads the input and compares it with the min and max, if have checked new values of min or max updates them;</p>
<p>Obviously there are some prints into serial output to have a fedback of the changes into code.</p>
<p>Finally I have added a button to close the circuit to configure the code with the real maximum possible value into input, this is optional ad is usefull only for testing.</p>
<h2>Material</h2>
<ul>
<li>10k resistor, or a numbers of resistors to make 10K ohm
<ul>
<li>is not very important that resistor is exactly 10k ohm, but you can change value based to the sensibility you want have. In example if you are in a room with very strong light you can use more of 10k ohm</li>
</ul>
</li>
<li>photoresistor</li>
<li>button (optional)</li>
</ul>
<h2>Scheme</h2>
<h3>Original scheme</h3>
<p>source: <a href="http://bildr.org/2012/11/photoresistor-arduino/">http://bildr.org/2012/11/photoresistor-arduino/</a>
<img src="https://gist.githubusercontent.com/kiuz/9448751/raw/faac36cf7d2d2bb1391190844ebb5c91b2605650/scheme.png" alt="image" /></p>
<h3>My modified scheme</h3>
<p><img src="https://gist.githubusercontent.com/kiuz/9448751/raw/595787f6c1f4f1e6ac8527f37e2eb5b67465629e/Schema.png" alt="image" /></p>
<h2>License</h2>
<p>Copyright (c) 2014 Domenico Monaco <a href="&#109;&#97;&#x69;&#108;&#116;&#111;&#x3a;&#x64;&#x6f;&#x6d;&#101;&#x6e;&#x69;&#99;&#x6f;&#x2e;&#x6d;&#x6f;&#x6e;&#x61;&#99;&#x6f;&#x40;&#x6b;&#x69;&#117;&#x7a;&#x2e;&#x69;&#116;">&#x64;&#x6f;&#x6d;&#101;&#110;&#x69;&#99;&#x6f;&#x2e;&#109;&#111;&#x6e;&#x61;&#x63;&#x6f;&#64;&#107;&#x69;&#x75;&#122;&#46;&#x69;&#x74;</a></p>
<p>Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:</p>
<p>The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.</p>
<p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.</p>
</body>
</html>

Intro ##

Description

This is very simple code to use photoresistor (LDR) with Arduino.

In a "void setup()" the code read the the input and creates empirically two level (max and min), where "min" is simply smaller at will.

So in the "void loop()" the code reads the input and compares it with the min and max, if have checked new values of min or max updates them;

Obviously there are some prints into serial output to have a fedback of the changes into code.

Finally I have added a button to close the circuit to configure the code with the real maximum possible value into input, this is optional ad is usefull only for testing.

Material

  • 10k resistor, or a numbers of resistors to make 10K ohm
    • is not very important that resistor is exactly 10k ohm, but you can change value based to the sensibility you want have. In example if you are in a room with very strong light you can use more of 10k ohm
  • photoresistor
  • button (optional)

Scheme

Original scheme###

source: http://bildr.org/2012/11/photoresistor-arduino/ image

My modified scheme###

image

License

Copyright (c) 2014 Domenico Monaco domenico.monaco@kiuz.it

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Display the source blob
Display the rendered blob
Raw
int photoRPin = A2;
int minLight;
int maxLight;
int lightLevel;
int adjustedLightLevel;
void setup() {
Serial.begin(9600);
//Setup the starting light level limits
lightLevel=analogRead(photoRPin);
minLight=lightLevel-20;
maxLight=lightLevel;
delay(500);
}
void loop(){
//auto-adjust the minimum and maximum limits in real time
lightLevel=analogRead(photoRPin);
if(minLight>lightLevel){
minLight=lightLevel;
Serial.print("new min: ");
Serial.print(minLight);
Serial.println(";");
}
if(maxLight<lightLevel){
maxLight=lightLevel;
Serial.print("new max: ");
Serial.print(maxLight);
Serial.println(";");
}
//Adjust the light level to produce a result between 0 and 100.
adjustedLightLevel = map(lightLevel, minLight, maxLight, 0, 100);
//Send the adjusted Light level result to Serial port (processing)
Serial.println(adjustedLightLevel);
//slow down the transmission for effective Serial communication.
delay(500);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment