








{"id":87,"date":"2020-10-12T21:09:05","date_gmt":"2020-10-12T21:09:05","guid":{"rendered":"https:\/\/osoyoo.info\/?p=87"},"modified":"2020-10-13T22:08:03","modified_gmt":"2020-10-13T22:08:03","slug":"arduino-basic-lesson-13-photoresistor","status":"publish","type":"post","link":"https:\/\/osoyoo.info\/index.php\/2020\/10\/12\/arduino-basic-lesson-13-photoresistor\/","title":{"rendered":"Hardware Programming with Arduino IDE &#8211; Lesson 13: Photoresistor"},"content":{"rendered":"<p><a href=\"https:\/\/osoyoo.info\/index.php\/2020\/10\/12\/arduino-basic-lesson-12-buzzer\/\"><img loading=\"lazy\" class=\"alignnone\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/Previous.png\" alt=\"\" width=\"206\" height=\"59\" \/><\/a>\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<a href=\"https:\/\/osoyoo.info\/index.php\/2020\/10\/11\/arduino-basic-tutorial-directory\/\"><img loading=\"lazy\" class=\"alignnone\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/Tutorial.png\" alt=\"\" width=\"206\" height=\"59\" \/><\/a>\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<a href=\"https:\/\/osoyoo.info\/index.php\/2020\/10\/12\/arduino-basic-lesson-14-potentiometer\/\"><img loading=\"lazy\" class=\"alignright\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/Next.png\" alt=\"\" width=\"206\" height=\"59\" \/><\/a><\/p>\n<div align=\"center\">\n<div align=\"center\"><img class=\"aligncenter\" src=\"https:\/\/osoyoo.com\/picture\/icon\/authorized_retailers.png\" \/><\/div>\n<div align=\"center\"><\/div>\n<table>\n<tbody>\n<tr>\n<td>Buy from US<\/td>\n<td>Buy from UK<\/td>\n<td>Buy from DE<\/td>\n<td>Buy from IT<\/td>\n<td>Buy from FR<\/td>\n<td>Buy from ES<\/td>\n<td>Buy from JP<\/td>\n<\/tr>\n<tr>\n<td><a href=\"https:\/\/www.amazon.com\/dp\/B08J7DKJK9?ref=myi_title_dp\"><img loading=\"lazy\" class=\"alignnone size-large\" src=\"https:\/\/osoyoo.com\/picture\/icon\/us_buy_now.png\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.amazon.co.uk\/dp\/B08J6BL2P7\"><img loading=\"lazy\" class=\"alignnone\" src=\"https:\/\/osoyoo.com\/picture\/icon\/uk_buy_now.png\" alt=\"\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.amazon.de\/dp\/B08J6BL2P7\"><img loading=\"lazy\" class=\"alignnone size-large\" src=\"https:\/\/osoyoo.com\/picture\/icon\/de_buy_now.png\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.amazon.it\/dp\/B08J6BL2P7\"><img loading=\"lazy\" class=\"alignnone size-large\" src=\"https:\/\/osoyoo.com\/picture\/icon\/it_buy_now.png\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.amazon.fr\/dp\/B08J6BL2P7\"><img loading=\"lazy\" class=\"alignnone size-large\" src=\"https:\/\/osoyoo.com\/picture\/icon\/fr_buy_now.png\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.amazon.es\/dp\/B08J6BL2P7\"><img loading=\"lazy\" class=\"alignnone size-large\" src=\"https:\/\/osoyoo.com\/picture\/icon\/es_buy_now.png\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.amazon.co.jp\/dp\/B08J7TLP48\"><img loading=\"lazy\" class=\"alignnone size-large\" src=\"https:\/\/osoyoo.com\/picture\/icon\/jp_buy_now.png\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h2><strong>Content<\/strong><\/h2>\n<ol>\n<li><a href=\"#1\">Introduction<\/a><\/li>\n<li><a href=\"#2\">Preparations<\/a>\n<ul>\n<li><a href=\"#2.1\">Hardware<\/a><\/li>\n<li><a href=\"#2.2\">Software<\/a><\/li>\n<\/ul>\n<\/li>\n<li><a href=\"#4\">Connection<\/a><\/li>\n<li><a href=\"#5\">Upload Sketch<\/a><\/li>\n<li><a href=\"#6\">Running Result<\/a><\/li>\n<\/ol>\n<h1 id=\"1\">Introduction<\/h1>\n<p><img class=\"aligncenter\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/lesson9\/light-sensor-min.png\" \/><\/p>\n<p>The OSOYOO Light Detect module incorporates a photocell, is a commonly used sensor in a wide variety of applications from DIY projects to industrial automation. Typically, the resistance of the LDR or Photoresistor will decrease when the ambient light intensity increases. This means that the output signal from this module will be HIGH in bright light, and LOW in the dark.<\/p>\n<p>Photocells are sensors that allow you to detect light. They are small, inexpensive, low-power, easy to use and don\u2019t wear out. For that reason they often appear in toys, gadgets and appliances. They are often referred to as CdS cells (they are made of Cadmium-Sulfide), light-dependent resistors (LDR), and photoresistors.<\/p>\n<p class=\"ordinary-output target-output clearfix\"><img class=\"aligncenter\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/lesson9\/photoresistor-min.png\" \/><\/p>\n<p>Photocells are basically a resistor that changes its resistive value (in ohms \u03a9) depending on how much light is shining onto the squiggly face.When it is dark, the resistance of a photoresistor may be as high as a few M\u03a9. When it is light, however, the resistance of a photoresistor may be as low as a few hundred ohms.\u00a0 They are very low cost, easy to get in many sizes and specifications, but are very innacurate. Each photocell sensor will act a little differently than the other, even if they are from the same batch. The variations can be really large, 50% or higher! For this reason, they shouldn\u2019t be used to try to determine precise light levels in lux or millicandela. Instead, you can expect to only be able to determine basic light changes.<\/p>\n<p>This graph indicates approximately the resistance of the sensor at different light levels:<\/p>\n<p><img class=\"aligncenter\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/lesson9\/photoresistorplot-min.png\" \/><\/p>\n<p>Light sensor is commonly seen in our daily life and is mainly used in intelligent switch, also in common electronic design.<\/p>\n<p>To make it more easier and effective, we supply corresponding modules. Light sensor is a semiconductor. It has features of high sensitivity, quick response, spectral characteristic, and R-value consistence, maintaining high stability and reliability in environment extremes such as high temperature, high humidity.<br \/>\nIt\u2019s widely used in automatic control switch fields like cameras, garden solar lights, lawn lamps, money detectors, quartz clocks, music cups, gift boxes, mini night lights, sound and light control switches, etc.<\/p>\n<h1 id=\"2\">Preparations<\/h1>\n<h2 id=\"2.1\">Hardware<\/h2>\n<ul>\n<li>OSOYOO UNO Board (Fully compatible with Arduino UNO rev.3) x 1<\/li>\n<li>OSOYOO Magic I\/O Shield for Arduino x1<\/li>\n<li>Light Detect Module x 1<\/li>\n<li>LED Module x 1<\/li>\n<li>OSOYOO 3-Pin PNP cable x 2<\/li>\n<li>USB Cable x 1<\/li>\n<li>PC x 1<\/li>\n<\/ul>\n<h2 id=\"2.2\">Software<\/h2>\n<p>Arduino IDE (version 1.6.4+)<\/p>\n<h1 id=\"4\">Connection<\/h1>\n<p>First, please plug Osoyoo Magic I\/O shield into UNO board:<\/p>\n<p><img class=\"aligncenter\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/lesson4\/UNO%2BBase-min.png\" \/><\/p>\n<p>Then connect the modules to the ports of the Magic I\/O shield with two 3-pin PNP cables as below:<\/p>\n<p>LED Module \u2013 D9<\/p>\n<p>Light Detect Module \u2013 A0<\/p>\n<p><img loading=\"lazy\" class=\"size-full aligncenter\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/lesson9\/Base-LED-Light-min.png\" width=\"1000\" height=\"600\" \/><\/p>\n<h1 id=\"5\">Upload Sketch<\/h1>\n<p>After above operations are completed, connect the Arduino board to your computer using the USB cable. The green power LED (labelled <strong>PWR<\/strong>) should go on.<\/p>\n<h2>Code Program<\/h2>\n<p>You can <a href=\"https:\/\/osoyoo.com\/picture\/OSOYOO_PnP_Kit_for_Arduino\/code\/Lesson14_Photoresistor.rar\" target=\"_blank\" rel=\"noopener noreferrer\">download the sketch from this link<\/a><\/p>\n<p>In this experiment, we will connect a photoresistor to an Arduino analog input and read the value with the <strong>analogRead()<\/strong> function. Depending on the value the Arduino reads, the program will then set pin 9 HIGH or LOW to turn on or turn off the LED night lights. The threshold value is 512. When the analog value read is less than 512, the Arduino will turn the LEDs on. When the analog value it reads is more than 512, the Arduino will turn the LEDs off.<\/p>\n<h2>Compile and upload<\/h2>\n<p>Open the Arduino IDE and select corresponding board type and port type for your Arduino board.<\/p>\n<p><a href=\"https:\/\/osoyoo.com\/wp-content\/uploads\/2017\/07\/sketch-1.jpg\"><img loading=\"lazy\" class=\"size-full wp-image-8070 aligncenter\" src=\"https:\/\/osoyoo.com\/wp-content\/uploads\/2017\/07\/sketch-1.jpg\" alt=\"\" width=\"486\" height=\"594\" \/><\/a><\/p>\n<p>After compile this sketch, simply click the \u201cUpload\u201d button in the environment. Wait a few seconds \u2013 you should see the RX and TX leds on the board flashing. If the upload is successful, the message \u201cDone uploading.\u201d will appear in the status bar.<\/p>\n<h1 id=\"6\">Running Result<\/h1>\n<p>If the room is lighted, the LEDs should not light. Try getting them to turn on it by covering the photoresistor with your hand. Remove your hand and observe that they turn off again.<\/p>\n<div align=\"center\"><iframe src=\"https:\/\/www.youtube.com\/embed\/Vk2HRAXN5Kg\" width=\"853\" height=\"480\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><span data-mce-type=\"bookmark\" style=\"display: inline-block; width: 0px; overflow: hidden; line-height: 0;\" class=\"mce_SELRES_start\">\ufeff<\/span><\/iframe><\/div>\n<p>In the same time, open the Serial Monitor and you will get the output data as below :<\/p>\n<p><a href=\"https:\/\/osoyoo.com\/wp-content\/uploads\/2017\/07\/photoresistorserial.gif\"><img loading=\"lazy\" class=\"size-full wp-image-8075 aligncenter\" src=\"https:\/\/osoyoo.com\/wp-content\/uploads\/2017\/07\/photoresistorserial.gif\" alt=\"\" width=\"520\" height=\"317\" \/><\/a><\/p>\n<p><span style=\"color: #ff0000;\"><strong>Note:<\/strong><\/span><\/p>\n<p>When you are using the Serial Monitor, please make sure the baudrate setting is same as your sketch definition.<\/p>\n<p><a href=\"https:\/\/osoyoo.info\/index.php\/2020\/10\/12\/arduino-basic-lesson-12-buzzer\/\"><img loading=\"lazy\" class=\"alignnone\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/Previous.png\" alt=\"\" width=\"206\" height=\"59\" \/><\/a>\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<a href=\"https:\/\/osoyoo.info\/index.php\/2020\/10\/11\/arduino-basic-tutorial-directory\/\"><img loading=\"lazy\" class=\"alignnone\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/Tutorial.png\" alt=\"\" width=\"206\" height=\"59\" \/><\/a>\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<a href=\"https:\/\/osoyoo.info\/index.php\/2020\/10\/12\/arduino-basic-lesson-14-potentiometer\/\"><img loading=\"lazy\" class=\"alignright\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/Next.png\" alt=\"\" width=\"206\" height=\"59\" \/><\/a><\/p>\n<div align=\"center\">\n<div align=\"center\"><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0 Buy from US Buy from UK Buy from DE Buy from IT Buy from FR Buy from ES Buy from JP Content Introduction Preparations Hardware Software Connection Upload [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":155,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/posts\/87"}],"collection":[{"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/comments?post=87"}],"version-history":[{"count":2,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/posts\/87\/revisions"}],"predecessor-version":[{"id":93,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/posts\/87\/revisions\/93"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/media\/155"}],"wp:attachment":[{"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/media?parent=87"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/categories?post=87"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/tags?post=87"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}