{"id":116,"date":"2019-10-23T07:28:40","date_gmt":"2019-10-23T07:28:40","guid":{"rendered":"https:\/\/starthardware.org\/en\/?p=116"},"modified":"2019-10-23T07:28:41","modified_gmt":"2019-10-23T07:28:41","slug":"unit-17-the-sun-plays-music-the-analog-input","status":"publish","type":"post","link":"https:\/\/starthardware.org\/en\/unit-17-the-sun-plays-music-the-analog-input\/","title":{"rendered":"Unit 17 \u2013 The sun plays music: The analog input."},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Beside the digital input we also can use the analog input. It is great to read potentiometers (knobs) or sensors. Think of the temperature or brightness \u2013\u00a0it is not on or off but has a certain value.<\/p><\/blockquote>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"254\" src=\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/17_2_Photoresistor-1024x254.png\" alt=\"\" class=\"wp-image-118\" srcset=\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/17_2_Photoresistor-1024x254.png 1024w, https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/17_2_Photoresistor-300x75.png 300w, https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/17_2_Photoresistor-768x191.png 768w, https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/17_2_Photoresistor.png 1280w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignright is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/17_3_analogeInputs.png\" alt=\"\" class=\"wp-image-117\" width=\"347\" height=\"248\" srcset=\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/17_3_analogeInputs.png 694w, https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/17_3_analogeInputs-300x214.png 300w\" sizes=\"auto, (max-width: 347px) 100vw, 347px\" \/><\/figure><\/div>\n\n\n\n<p>Brightness can be detected by so called photo-resistors. A photo-resistor consists of a material changing it\u2019s resistance depending on brightness. The more light, the smaller it\u2019s resistance gets.<\/p>\n\n\n\n<p>We can read out this sensor with the Arduino board. It will return a value between 0 and 1023. We need to use the Analog input pins of the Arduino. They are on the on the opposite of the digital pins.<\/p>\n\n\n\n<p>To use the photo-resistor you will need an additional resistor. It is called a reference resistor and Arduino needs it to detect a relative relation between the photo- and the reference-resistor.<\/p>\n\n\n\n<p>The reference resistor should have the resistance of the photo-resistor while average brightness. But you can just try which resistor works best. Start with 100 Kilo-Ohm.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Used parts<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/17_1_parts.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1280\" height=\"535\" src=\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/17_1_parts.png\" alt=\"Arduino Analog input ldr photo resistor parts\" class=\"wp-image-119\" srcset=\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/17_1_parts.png 1280w, https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/17_1_parts-300x125.png 300w, https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/17_1_parts-768x321.png 768w, https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/17_1_parts-1024x428.png 1024w\" sizes=\"auto, (max-width: 1280px) 100vw, 1280px\" \/><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Circuit<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"799\" src=\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/17-arduino-analog-in-ldr-1024x799.jpg\" alt=\"Arduino Analog input ldr photo resistor circuit\" class=\"wp-image-120\" srcset=\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/17-arduino-analog-in-ldr-1024x799.jpg 1024w, https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/17-arduino-analog-in-ldr-300x234.jpg 300w, https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/17-arduino-analog-in-ldr-768x599.jpg 768w, https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/17-arduino-analog-in-ldr.jpg 1349w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Super. Now, we\u2019ve got a reference resistor (100 kOhm) and a photo-resistor. They are connected to the Analog 0 of the Arduino.<\/p>\n\n\n\n<p>The command to read an analog value is<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>analogRead(pin);<\/code><\/pre>\n\n\n\n<p>It returns a value between 0 and 1023. Now, we code a program playing a sound. The tone height should be dependant of the photo-resistor. In other words dependent of the brightness. To do so, we can use the command analogRead(sensorPin) and send the value to the tone() command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int sensorPin = 0;\nint speakerPin = 9;\n\nvoid setup() {\n\n}\n\nvoid loop() {\n  tone(speakerPin,analogRead(sensorPin));\n  delay(20);\n}<\/code><\/pre>\n\n\n\n<p>That\u2019s it! Not bad, no? But what values are actually reached? We just hear the sound but wouldn\u2019t it be good to display the values of the photo-resistor?<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Serial Monitor<\/h3>\n\n\n\n<p>The Serial Monitor of the Arduino software can create a data connection between the computer and the Arduino. When we send data to this so called serial connection, the Serial Monitor can display this values. To use it, we have to put<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Serial.begin(9600);<\/code><\/pre>\n\n\n\n<p>in the setup()-method. The 9600 is the Baud rate \u2013 the speed of transmission .<\/p>\n\n\n\n<p>Now, the serial connection is turned on. To send a value, we write:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Serial.println(value);<\/code><\/pre>\n\n\n\n<p>As value we can use variables, text or even methods providing a returning value, e.g. analogRead(sensorPin). This looks like that:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int sensorPin = 0;\nint speakerPin = 9;\n\nvoid setup() {\n  Serial.begin(9600);\n}\n\nvoid loop() {\n  Serial.println(analogRead(sensorPin));\n  tone(speakerPin,analogRead(sensorPin));\n  delay(20);\n}<\/code><\/pre>\n\n\n\n<p>And where can we see this data? Klick on the magnifying glass top right in the Arduino software (or click on Tools>Serial Monitor in the menue). A new window is opened: The Serial Monitor.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-serial-monitor.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"813\" src=\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-serial-monitor-1024x813.jpg\" alt=\"Arduino serial monitor\" class=\"wp-image-121\" srcset=\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-serial-monitor-1024x813.jpg 1024w, https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-serial-monitor-300x238.jpg 300w, https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-serial-monitor-768x610.jpg 768w, https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-serial-monitor.jpg 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>What values do you get? Do they use the whole range from 0 to 1023? Usually not, but a range of 600 to 900 would be totally OK. You can adjust it by trying different resistors. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Beside the digital input we also can use the analog input. It is great to read potentiometers (knobs) or sensors. Think of the temperature or brightness \u2013\u00a0it is not on or off but has a certain value. Brightness can be detected by so called photo-resistors. A photo-resistor consists of a material changing it\u2019s resistance depending&hellip;&nbsp;<a href=\"https:\/\/starthardware.org\/en\/unit-17-the-sun-plays-music-the-analog-input\/\" class=\"\" rel=\"bookmark\">Read More &raquo;<span class=\"screen-reader-text\">Unit 17 \u2013 The sun plays music: The analog input.<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":123,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","footnotes":""},"categories":[2],"tags":[],"class_list":["post-116","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-arduino-tutorial"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v18.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Unit 17 \u2013 The sun plays music: The analog input. - StartHardware - Tutorials for Arduino<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/starthardware.org\/en\/unit-17-the-sun-plays-music-the-analog-input\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Unit 17 \u2013 The sun plays music: The analog input. - StartHardware - Tutorials for Arduino\" \/>\n<meta property=\"og:description\" content=\"Beside the digital input we also can use the analog input. It is great to read potentiometers (knobs) or sensors. Think of the temperature or brightness \u2013\u00a0it is not on or off but has a certain value. Brightness can be detected by so called photo-resistors. A photo-resistor consists of a material changing it\u2019s resistance depending&hellip;&nbsp;Read More &raquo;Unit 17 \u2013 The sun plays music: The analog input.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/starthardware.org\/en\/unit-17-the-sun-plays-music-the-analog-input\/\" \/>\n<meta property=\"og:site_name\" content=\"StartHardware - Tutorials for Arduino\" \/>\n<meta property=\"article:published_time\" content=\"2019-10-23T07:28:40+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-10-23T07:28:41+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-ldr-analog-input-title-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1416\" \/>\n\t<meta property=\"og:image:height\" content=\"797\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Stefan Hermann\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/starthardware.org\/en\/#website\",\"url\":\"https:\/\/starthardware.org\/en\/\",\"name\":\"StartHardware - Tutorials for Arduino\",\"description\":\"Arduino, Electronics, Fun\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/starthardware.org\/en\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/starthardware.org\/en\/unit-17-the-sun-plays-music-the-analog-input\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-ldr-analog-input-title-1.jpg\",\"contentUrl\":\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-ldr-analog-input-title-1.jpg\",\"width\":1416,\"height\":797,\"caption\":\"Arduino Analog Input Title\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/starthardware.org\/en\/unit-17-the-sun-plays-music-the-analog-input\/#webpage\",\"url\":\"https:\/\/starthardware.org\/en\/unit-17-the-sun-plays-music-the-analog-input\/\",\"name\":\"Unit 17 \u2013 The sun plays music: The analog input. - StartHardware - Tutorials for Arduino\",\"isPartOf\":{\"@id\":\"https:\/\/starthardware.org\/en\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/starthardware.org\/en\/unit-17-the-sun-plays-music-the-analog-input\/#primaryimage\"},\"datePublished\":\"2019-10-23T07:28:40+00:00\",\"dateModified\":\"2019-10-23T07:28:41+00:00\",\"author\":{\"@id\":\"https:\/\/starthardware.org\/en\/#\/schema\/person\/811b16fabcbfeef4210ea79cf0990a59\"},\"breadcrumb\":{\"@id\":\"https:\/\/starthardware.org\/en\/unit-17-the-sun-plays-music-the-analog-input\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/starthardware.org\/en\/unit-17-the-sun-plays-music-the-analog-input\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/starthardware.org\/en\/unit-17-the-sun-plays-music-the-analog-input\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/starthardware.org\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Unit 17 \u2013 The sun plays music: The analog input.\"}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/starthardware.org\/en\/#\/schema\/person\/811b16fabcbfeef4210ea79cf0990a59\",\"name\":\"Stefan Hermann\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/starthardware.org\/en\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/5b5a74ee1d07024fd1eff9b1f7137108089169010a93afaee907b9325ee579a6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/5b5a74ee1d07024fd1eff9b1f7137108089169010a93afaee907b9325ee579a6?s=96&d=mm&r=g\",\"caption\":\"Stefan Hermann\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Unit 17 \u2013 The sun plays music: The analog input. - StartHardware - Tutorials for Arduino","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/starthardware.org\/en\/unit-17-the-sun-plays-music-the-analog-input\/","og_locale":"en_US","og_type":"article","og_title":"Unit 17 \u2013 The sun plays music: The analog input. - StartHardware - Tutorials for Arduino","og_description":"Beside the digital input we also can use the analog input. It is great to read potentiometers (knobs) or sensors. Think of the temperature or brightness \u2013\u00a0it is not on or off but has a certain value. Brightness can be detected by so called photo-resistors. A photo-resistor consists of a material changing it\u2019s resistance depending&hellip;&nbsp;Read More &raquo;Unit 17 \u2013 The sun plays music: The analog input.","og_url":"https:\/\/starthardware.org\/en\/unit-17-the-sun-plays-music-the-analog-input\/","og_site_name":"StartHardware - Tutorials for Arduino","article_published_time":"2019-10-23T07:28:40+00:00","article_modified_time":"2019-10-23T07:28:41+00:00","og_image":[{"width":1416,"height":797,"url":"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-ldr-analog-input-title-1.jpg","type":"image\/jpeg"}],"twitter_card":"summary_large_image","twitter_misc":{"Written by":"Stefan Hermann","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"https:\/\/starthardware.org\/en\/#website","url":"https:\/\/starthardware.org\/en\/","name":"StartHardware - Tutorials for Arduino","description":"Arduino, Electronics, Fun","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/starthardware.org\/en\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https:\/\/starthardware.org\/en\/unit-17-the-sun-plays-music-the-analog-input\/#primaryimage","inLanguage":"en-US","url":"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-ldr-analog-input-title-1.jpg","contentUrl":"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-ldr-analog-input-title-1.jpg","width":1416,"height":797,"caption":"Arduino Analog Input Title"},{"@type":"WebPage","@id":"https:\/\/starthardware.org\/en\/unit-17-the-sun-plays-music-the-analog-input\/#webpage","url":"https:\/\/starthardware.org\/en\/unit-17-the-sun-plays-music-the-analog-input\/","name":"Unit 17 \u2013 The sun plays music: The analog input. - StartHardware - Tutorials for Arduino","isPartOf":{"@id":"https:\/\/starthardware.org\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/starthardware.org\/en\/unit-17-the-sun-plays-music-the-analog-input\/#primaryimage"},"datePublished":"2019-10-23T07:28:40+00:00","dateModified":"2019-10-23T07:28:41+00:00","author":{"@id":"https:\/\/starthardware.org\/en\/#\/schema\/person\/811b16fabcbfeef4210ea79cf0990a59"},"breadcrumb":{"@id":"https:\/\/starthardware.org\/en\/unit-17-the-sun-plays-music-the-analog-input\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/starthardware.org\/en\/unit-17-the-sun-plays-music-the-analog-input\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/starthardware.org\/en\/unit-17-the-sun-plays-music-the-analog-input\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/starthardware.org\/en\/"},{"@type":"ListItem","position":2,"name":"Unit 17 \u2013 The sun plays music: The analog input."}]},{"@type":"Person","@id":"https:\/\/starthardware.org\/en\/#\/schema\/person\/811b16fabcbfeef4210ea79cf0990a59","name":"Stefan Hermann","image":{"@type":"ImageObject","@id":"https:\/\/starthardware.org\/en\/#personlogo","inLanguage":"en-US","url":"https:\/\/secure.gravatar.com\/avatar\/5b5a74ee1d07024fd1eff9b1f7137108089169010a93afaee907b9325ee579a6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5b5a74ee1d07024fd1eff9b1f7137108089169010a93afaee907b9325ee579a6?s=96&d=mm&r=g","caption":"Stefan Hermann"}}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/posts\/116","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/comments?post=116"}],"version-history":[{"count":1,"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/posts\/116\/revisions"}],"predecessor-version":[{"id":124,"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/posts\/116\/revisions\/124"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/media\/123"}],"wp:attachment":[{"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/media?parent=116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/categories?post=116"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/tags?post=116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}