{"id":165,"date":"2022-01-16T08:17:00","date_gmt":"2022-01-16T08:17:00","guid":{"rendered":"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/?post_type=portfolio&#038;p=165"},"modified":"2022-03-11T21:08:42","modified_gmt":"2022-03-11T21:08:42","slug":"march-10-arduino-outputs-actuators","status":"publish","type":"portfolio","link":"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/portfolio\/march-10-arduino-outputs-actuators\/","title":{"rendered":"March 10 \u2013\u00a0Arduino outputs &#038; actuators"},"content":{"rendered":"\r\n<p><strong>Technical Tutorial #4<\/strong><\/p>\r\n<p><a href=\"https:\/\/drive.google.com\/open?id=1DFVRWvDCGQt4lQEi479Qr2_gYE8kyygE\">Recorded video tutorial<\/a>. <a href=\"https:\/\/drive.google.com\/open?id=1DIMnVBs16cFXX_A2ti9gsQidlF7cfF_6\">Searchable transcript with timestamps<\/a>.<\/p>\r\n<p><strong>Review<\/strong><\/p>\r\n<p>Last week we looked at different techniques and components that can operate as <strong>digital and analog inputs<\/strong>. We saw some techniques for how you might connect these <strong>sensors<\/strong> to an <strong>Arduino board<\/strong>, including a <strong>voltage divider<\/strong> to make sure that any inputs are not <strong>floating<\/strong>. We also talked about how Arduino allows you to write computer programs to interact with your circuit, enabling you to modify the behavior of a circuit without changing any wiring or electrical components, by <strong>uploading<\/strong> program code to run on the Arduino<strong> microcontroller<\/strong>. We ended the lesson last week by uploading the &#8220;AnalogInput&#8221; program to our boards and using it so that twisting a potentiometer adjusted the blinking speed of the built-in LED. But I did not go into detail about how that code works. Later on today I&#8217;ll go into depth about how that program operates.<\/p>\r\n<p><em>Please note #1<\/em>: that I have also added an <a href=\"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/index-of-schematic-symbols\/\">index of schematic symbols<\/a> in response to popular demand. You can find this linked from the main menu under &#8220;Technical tutorials.&#8221;<\/p>\r\n<p><em>Please note #2<\/em>: I have posted the Project 1 assignment. You can find it <a href=\"https:\/\/drive.google.com\/file\/d\/1D9LElqFE45bqS62WaSLoZ_wGi0Twl4Bi\/view\">linked here<\/a>, as well as in the main menu under &#8220;Projects&#8221;.<\/p>\r\n<p><strong>Outputs &amp; actuators<\/strong><\/p>\r\n<p>We could sum up last week by saying that we were looking at <strong>inputs<\/strong> <em>to<\/em> your Arduino microcontroller. By contrast, today we\u2019re going to look at some techniques for working with <strong>output<\/strong> <em>from<\/em> your Arduino.<\/p>\r\n<p>In other words: <strong>Last week we looked at how Arduino program code could <em><strong>ask<\/strong> questions<\/em> about the world with sensors, and today we&#8217;re going to look at various techniques for how Arduino can <em>act<\/em> on the world.<\/strong><\/p>\r\n<p>Or to put it in terms of Arduino <strong>code syntax<\/strong>: think about how last week we were using <code>pinMode(X,<span style=\"background-color: yellow;\">INPUT<\/span>)<\/code> and <code>digital<span style=\"background-color: yellow;\">Read<\/span>(X)<\/code> (where <code>X<\/code> is a pin number). By contrast, this week we&#8217;ll focus on using <code>pinMode(X,<span style=\"background-color: yellow;\">OUTPUT<\/span>)<\/code>\u00a0and <code>digital<span style=\"background-color: yellow;\">Write<\/span>(X,VALUE)<\/code> (where <code>VALUE<\/code> is a voltage value like <code>HIGH<\/code> or <code>LOW<\/code>).<\/p>\r\n<figure id=\"attachment_362\" aria-describedby=\"caption-attachment-362\" style=\"width: 1024px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/01\/workshop-4-splash-image-piano-servo-1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-362\" src=\"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/01\/workshop-4-splash-image-piano-servo-1.jpg\" alt=\"\" width=\"1024\" height=\"620\" srcset=\"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/01\/workshop-4-splash-image-piano-servo-1.jpg 1024w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/01\/workshop-4-splash-image-piano-servo-1-300x182.jpg 300w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/01\/workshop-4-splash-image-piano-servo-1-768x465.jpg 768w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/01\/workshop-4-splash-image-piano-servo-1-738x447.jpg 738w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/01\/workshop-4-splash-image-piano-servo-1-996x603.jpg 996w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption id=\"caption-attachment-362\" class=\"wp-caption-text\">Fig 1. Today we&#8217;re going to talk about generating sound, and about creating motion with a servo motor. This image depicts a project that humorously does both of these things at the same time with an Arduino controlling a servo motor to play piano keys. You can read more about this project at <a href=\"https:\/\/www.instructables.com\/Robotic-Piano-Player\/\">instructables.com<\/a>.<\/figcaption><\/figure>\r\n<p>We\u2019ve already experimented with one electrical component that can act as an output from Arduino. Can you think of it? Highlight for the answer: <span style=\"color: #fff;\">LEDs<\/span>. Whenever we saw Arduino program code that said <code>digitalWrite()<\/code> passing a <code>HIGH<\/code> or <code>LOW<\/code> value, we were sending an output value <em>from<\/em> Arduino <em>to<\/em> the circuit, generating an effect in the world. Today we\u2019ll build on that by looking at ways to generate <strong>sound<\/strong> and <strong>movement<\/strong>.<\/p>\r\n<p><strong>Movement<\/strong><\/p>\r\n<p>Anything that generates movement from a circuit can be generally referred to as an <strong>actuator<\/strong>. There are many components that could be described as actuators. (cf this article from <a href=\"https:\/\/www.creativemotioncontrol.com\/types-of-actuators\/\">creativemotioncontrol.com<\/a>.) Often but not always, actuators operate on principles of <strong>electromagnetic force<\/strong>. An interesting relationship exists between magnetism and electrical flows. The movement of magnets in particular ways can generate electrical current, and conversely, the flow of current through particular conductors like wire in a coil can generate momentary magnetic fields. This is the principle behind most <strong>electrical motors<\/strong>: a coil of conductive material is either placed inside or around a magnet, and as current is flowed through the conductor, it causes a shaft to spin, which is used to drive various other components like a wheel or a gear. Another type of actuator is a <strong>solenoid<\/strong>. This operates on a similar principle, but instead of spinning a shaft in coil, the coil induces a linear motion \u2013 similar to what the article linked above calls a \u201clinear actuator\u201d. A solenoid could be used for example as an electrically-controlled bolt to lock a door.<\/p>\r\n<p><strong>Sound<\/strong><\/p>\r\n<p>Before we get to motion, let\u2019s start with a different kind of output: a <strong>buzzer<\/strong>. The simplest material and technique for making sound with electricity is known as a <strong>Piezo buzzer<\/strong>. <strong>Piezoelectricity<\/strong> describes a property of certain materials to give off a small electrical charge in response to a physical movement like shaking or jolting. Conversely, when current is applied to a piezoelectric element, it generates small physical movements or vibrations. This kind of <span style=\"font-family: inherit;\">symmetry is common among many electrical phenomena. It is the latter behavior that is frequently used as a technique for generating sound: current is applied to a Piezo element, generating small motions which can vibrate to produce a sound. This is also a technique frequently used, for example, in the microphone pickup of an electrical guitar: the plucked strings vibrate at specific frequencies, which in turn vibrate small piezoelectric elements, which generate electricity in a way that corresponds to the musical tonality, which is then passed through other circuitry that amplify the current and convert it back to louder sounds played through speakers.<\/span><\/p>\r\n<p>Piezoelectric buzzers are different from regular <strong>speakers<\/strong> though. The typical speaker has a cone-shaped paper wafer that can be made to vibrate at precise frequencies, generating high quality tones, while piezo elements generally produce a more crude vibration, which is why they\u2019re called buzzers, and are not typically used for reproducing sounds like musical instruments or the human voice. But, they are a pretty fascinating material, and we can use them to produce some tones and to experiment with as a technique for generating output from an electrical circuit.<\/p>\r\n<p>You can actually <a href=\"https:\/\/rimstar.org\/materials\/piezo\/how_to_make_rochelle_salt_piezoelectric_crystal.htm\">make a form of piezo crystal known as <strong>Rochelle salt<\/strong><\/a> at home with baking soda and some other materials. I\u2019ve tried this with some success, following the instructions linked there, but I\u2019ve never gotten my homemade piezo crystals to produce much electricity.<\/p>\r\n<p>Find the piezo buzzer that came in your kit, pictured in Figure 2.<\/p>\r\n<figure id=\"attachment_374\" aria-describedby=\"caption-attachment-374\" style=\"width: 480px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-large wp-image-374\" src=\"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-1024x524.jpg\" alt=\"\" width=\"480\" height=\"246\" srcset=\"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-1024x524.jpg 1024w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-300x154.jpg 300w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-768x393.jpg 768w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-1536x786.jpg 1536w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-738x378.jpg 738w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-1458x746.jpg 1458w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-996x510.jpg 996w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-1992x1020.jpg 1992w\" sizes=\"auto, (max-width: 480px) 100vw, 480px\" \/><\/a><figcaption id=\"caption-attachment-374\" class=\"wp-caption-text\">Fig 2. A Piezo buzzer. (Click to enlarge.)<\/figcaption><\/figure>\r\n<p>The schematic symbol for a general Piezo element and a Piezo buzzer are shown in Figure 3.<\/p>\r\n<figure id=\"attachment_373\" aria-describedby=\"caption-attachment-373\" style=\"width: 480px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-schematic.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-373\" src=\"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-schematic.jpg\" alt=\"\" width=\"480\" height=\"397\" srcset=\"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-schematic.jpg 1024w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-schematic-300x248.jpg 300w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-schematic-768x635.jpg 768w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-schematic-738x610.jpg 738w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-schematic-996x823.jpg 996w\" sizes=\"auto, (max-width: 480px) 100vw, 480px\" \/><\/a><figcaption id=\"caption-attachment-373\" class=\"wp-caption-text\">Fig 3. Schematic symbols for a general Piezo element (left) and a Piezo buzzer (right). (Click to enlarge.)<\/figcaption><\/figure>\r\n<p>To get started with this, wire up a circuit for the schematic shown in Figure 4.<\/p>\r\n<figure id=\"attachment_365\" aria-describedby=\"caption-attachment-365\" style=\"width: 480px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-circuit-1-schematic.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-365\" src=\"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-circuit-1-schematic.jpg\" alt=\"\" width=\"480\" height=\"476\" srcset=\"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-circuit-1-schematic.jpg 1024w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-circuit-1-schematic-300x297.jpg 300w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-circuit-1-schematic-150x150.jpg 150w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-circuit-1-schematic-768x761.jpg 768w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-circuit-1-schematic-100x100.jpg 100w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-circuit-1-schematic-738x732.jpg 738w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-circuit-1-schematic-996x987.jpg 996w\" sizes=\"auto, (max-width: 480px) 100vw, 480px\" \/><\/a><figcaption id=\"caption-attachment-365\" class=\"wp-caption-text\">Fig 4. First Piezo circuit schematic. (Click to enlarge.)<\/figcaption><\/figure>\r\n<p>Since the buzzers that came in our kits have an awkward shape that can be somewhat unwieldy on your breadboard, I recommend carefully noting which row numbers you insert it into before pressing it all the way down, then making sure that you use those rows when making connections. In Figure 5 I\u2019m using rows 1 and 5, and then connecting those to GROUND and digital pin 8. After doing that, the circuit for the above schematic should look like the one pictured in Figure 6.<\/p>\r\n<figure id=\"attachment_372\" aria-describedby=\"caption-attachment-372\" style=\"width: 480px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-insert.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-372\" src=\"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-insert.jpg\" alt=\"\" width=\"480\" height=\"430\" srcset=\"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-insert.jpg 1024w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-insert-300x269.jpg 300w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-insert-768x689.jpg 768w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-insert-738x662.jpg 738w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-insert-996x893.jpg 996w\" sizes=\"auto, (max-width: 480px) 100vw, 480px\" \/><\/a><figcaption id=\"caption-attachment-372\" class=\"wp-caption-text\">Fig 5. Inserting a Piezo buzzer while noting rows. (Click to enlarge.)<\/figcaption><\/figure>\r\n<figure id=\"attachment_366\" aria-describedby=\"caption-attachment-366\" style=\"width: 480px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-circuit-1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-366\" src=\"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-circuit-1.jpg\" alt=\"\" width=\"480\" height=\"640\" srcset=\"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-circuit-1.jpg 768w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-circuit-1-225x300.jpg 225w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-circuit-1-738x984.jpg 738w\" sizes=\"auto, (max-width: 480px) 100vw, 480px\" \/><\/a><figcaption id=\"caption-attachment-366\" class=\"wp-caption-text\">Fig 6. First Piezo circuit, implementing the schematic from Figure 4. (Click to enlarge.)<\/figcaption><\/figure>\r\n<p>Once we have that built, let\u2019s use Arduino to do something with this circuit.<\/p>\r\n<p><strong>How does sound work?<\/strong> That may seem like an absurd (or absurdly complicated) question, but before proceeding with this experiment, we have to think for a bit about how tonal sound is generated and perceived. Human hearing works by detecting <strong>audible frequencies<\/strong>, which we usually measure in <strong>hertz (Hz)<\/strong>. <strong>Hertz<\/strong> are a measurement of frequency that quantify <strong>how many times something happens per second<\/strong>. Hertz can be used to measure anything the repeats periodically, for example: sine waves, computer CPU clock speeds, AC electrical current, or wireless radio signals. In the case of sound, we use Hertz to measure the frequency of small vibrations. Think of this as like beats per minute, just like in a pop song, but much faster. If you tap your desk or snap your finger once per second, we would say this is 1 Hz. As you increase the speed (frequency) of snapping, the Hertz value would increase. At some point, if you were hypothetically able to tap or snap\u00a0<em>very<\/em> quickly, human perception would shift from hearing this as separate, discrete clicks, and start hearing them as a vibration. This would happen at around 20 Hz, about the lowest audible frequency. By adjusting this frequency, you can adjust the perceived tone of this vibration. For reference, the middle key on a piano (&#8220;middle C&#8221;) is 261.6 Hz, and the generally highest frequency sound that can be heard is 20,000 Hz, although this changes with age \u2013 a teenager can hear up to 18,000 Hz while an older person won&#8217;t be able to hear any frequencies above 12,000 Hz. You can test the frequencies that you&#8217;re able to hear with <a href=\"https:\/\/www.youtube.com\/watch?v=qNf9nzvnd1k\">an audio signal like this\u00a0<\/a>(via YouTube) \u2013 careful to turn your sound down before hitting play and only adjusting it up to safe levels. whew. End detour into acoustics. But if you&#8217;re interested in learning more about this, consider taking one of these classes with Lang Professor Clara Latham: <a href=\"https:\/\/courses.newschool.edu\/courses\/LMUS2053\/9266\/\">LMUS 2053<\/a>, <a href=\"https:\/\/courses.newschool.edu\/courses\/LMUS3111\/14094\/\">LMUS 3111<\/a>. They will go into much more detail about these concepts.<\/p>\r\n<p>How will we apply this to our Arduino circuit? Well &#8230; does this remind you of anything that we\u2019ve worked with already? What about the \u201cBlink\u201d code? The idea here is that we\u2019re going to start with that basic code that we used to time turning an LED on, but now instead use it to time activating a Piezo. Each time we turn the Piezo on, it makes a small vibration which produces a small click. Many clicks per second will generate an audible frequency, and adjusting that frequency will let us adjust that tone.<\/p>\r\n<p>In your Arduino IDE, open File &gt; Examples &gt; Basics &gt; Blink. Modify the code to look like Figure 7 \u2013 the modified bits are highlighted in yellow. The changes here create a new <strong>variable<\/strong> called <code>piezoPin<\/code>, which is set to <code>8<\/code>, corresponding to the Arduino pin to which we have connected the Piezo. After that addition, the changes here replace all three instances of <code>LED_BUILTIN<\/code> with <code>piezoPin<\/code>. As we discussed when we were working with this code to blink an LED, the variable <code>LED_BUILTIN<\/code> is a placeholder for the number <code>13<\/code>, which corresponds to the pin number of the built-in LED for testing.<\/p>\r\n<figure id=\"attachment_394\" aria-describedby=\"caption-attachment-394\" style=\"width: 700px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/01\/workshop-4-piezo-code-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-394\" src=\"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/01\/workshop-4-piezo-code-1-1024x418.png\" alt=\"\" width=\"700\" height=\"286\" srcset=\"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/01\/workshop-4-piezo-code-1-1024x418.png 1024w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/01\/workshop-4-piezo-code-1-300x123.png 300w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/01\/workshop-4-piezo-code-1-768x314.png 768w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/01\/workshop-4-piezo-code-1-1536x627.png 1536w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/01\/workshop-4-piezo-code-1-738x301.png 738w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/01\/workshop-4-piezo-code-1-1458x595.png 1458w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/01\/workshop-4-piezo-code-1-996x407.png 996w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/01\/workshop-4-piezo-code-1.png 1572w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/a><figcaption id=\"caption-attachment-394\" class=\"wp-caption-text\">Fig 7. Adapting the \u201cBlink\u201d program to control a Piezo buzzer. (Click to enlarge.)<\/figcaption><\/figure>\r\n<p>If you upload that to your Arduino board, you should hear a persistent, slow and steady clicking. Remember how the <code>delay()<\/code> command controls timing. So this program sets pin 8 to <code>HIGH<\/code>, waits one second, sets it to <code>LOW<\/code>, and waits one more second. It repeats this pattern over and over again indefinitely. You could say this is 1 Hz.<\/p>\r\n<p><em>Question:<\/em> Why is it that we set <code>piezoPin<\/code> to <code>HIGH<\/code> and delay for 1 whole second, but the Piezo only makes a momentary click \u2013 why does the Piezo not play for the whole second? The reason is that when we pass current to\/through the Piezo, it only clicks when it&#8217;s first activated, almost like it&#8217;s jolted. So even though we are sending a <code>HIGH<\/code> value to that pin for a one full second, we only hear this click which corresponds to the moment when the <code>HIGH<\/code> signal is first sent.<\/p>\r\n<p>Let&#8217;s play an audible tone \u2013 let&#8217;s say 200 Hz. How could we adjust this code to play a 200 Hz tone? Remember, that means 200 clicks per second. How can we achieve that? Well we\u2019re working with milliseconds here. Remember that 1 second = 1000 milliseconds. And we want to produce 200 clicks per second, so we could play a click every 5 milliseconds, since 1000 ms \/ 200 = 5 ms. However, we have <em>two<\/em> <code>delay()<\/code>s here: one to activate the Piezo (<code>HIGH<\/code>), and another to turn it off (<code>LOW<\/code>) so that it can be activated again. So, we could set the delays to 2.5 ms (5 ms divided by 2). Unfortunately the <code>delay()<\/code> command only takes integers, i.e. whole numbers (this is what the <code>int<\/code> keyword signifies in the first highlighted line above). So, what we can do is delay 1 ms after setting the Piezo to <code>HIGH<\/code>, and then delay 4 ms after setting it to <code>LOW<\/code>. Let&#8217;s try that. That will create a <strong>cycle<\/strong> of 5 ms total (1 + 4) and achieve the desired goal of 200 Hz. Figure 8 demonstrates how to do this with the code changes from Figure 7 highlighted. Note that I did not need to modify the comments (the text that comes after the \/\/) but I did it just so my note-to-self would remain accurate.<\/p>\r\n<figure id=\"attachment_395\" aria-describedby=\"caption-attachment-395\" style=\"width: 700px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/01\/workshop-4-piezo-code-1b.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-395\" src=\"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/01\/workshop-4-piezo-code-1b-1024x419.png\" alt=\"\" width=\"700\" height=\"286\" srcset=\"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/01\/workshop-4-piezo-code-1b-1024x419.png 1024w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/01\/workshop-4-piezo-code-1b-300x123.png 300w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/01\/workshop-4-piezo-code-1b-768x314.png 768w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/01\/workshop-4-piezo-code-1b-738x302.png 738w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/01\/workshop-4-piezo-code-1b-1458x597.png 1458w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/01\/workshop-4-piezo-code-1b-996x408.png 996w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/01\/workshop-4-piezo-code-1b.png 1530w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/a><figcaption id=\"caption-attachment-395\" class=\"wp-caption-text\">Fig 8. Modifications from Figure 7 to create a 200Hz sound. (Click to enlarge.)<\/figcaption><\/figure>\r\n<p>To be honest, this level of precision really doesn&#8217;t matter since we&#8217;re just trying to generate an audible tone. You could use delay values somewhere between 1 and 5. But I&#8217;ve explained this here in the interest of understanding the process. To experiment, try changing the 4 to values like 2, 3, or 5, and re-uploading the code to your Arduino board each time to hear different tones.<\/p>\r\n<p>For anyone curious, Arduino also has a <code>delayMicroseconds()<\/code> command (<a href=\"https:\/\/www.arduino.cc\/reference\/en\/language\/functions\/time\/delaymicroseconds\/\">documentation<\/a>) which accepts microsecond arguments. 1 second = 1,000,000 microseconds. This would give you finer grain control, allowing you to specify shorter delays and thus higher pitched frequencies.<\/p>\r\n<p><strong>Sound with analog input<\/strong><\/p>\r\n<p><em>What if &#8230;<\/em> What if we want to be able to modify our tones in a dynamic way without re-uploading our code to the Arduino board each time. We could use an <strong>analog input<\/strong> with an <strong>analog sensor<\/strong> to do this. The objective here will be to use a potentiometer somehow as a control, so that adjusting the knob will adjust the tone being played by the buzzer. To achieve this we&#8217;ll use the <code>analogRead()<\/code> command which we used last week but did not discuss in detail \u2013 so consider this somewhat of a review and explanation of how to use this command.<\/p>\r\n<p>To get started, let\u2019s add a potentiometer to our Piezo circuit, wired up as indicated by the schematic in Figure 9, which could look something like the photo in Figure 10.<\/p>\r\n<figure id=\"attachment_367\" aria-describedby=\"caption-attachment-367\" style=\"width: 480px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-circuit-2-schematic.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-367\" src=\"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-circuit-2-schematic.jpg\" alt=\"\" width=\"480\" height=\"338\" srcset=\"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-circuit-2-schematic.jpg 1024w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-circuit-2-schematic-300x211.jpg 300w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-circuit-2-schematic-768x541.jpg 768w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-circuit-2-schematic-738x520.jpg 738w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-circuit-2-schematic-996x701.jpg 996w\" sizes=\"auto, (max-width: 480px) 100vw, 480px\" \/><\/a><figcaption id=\"caption-attachment-367\" class=\"wp-caption-text\">Fig 9. Adding a potentiometer to control our Piezo. (Click to enlarge.)<\/figcaption><\/figure>\r\n<figure id=\"attachment_397\" aria-describedby=\"caption-attachment-397\" style=\"width: 480px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/01\/workshop-4-piezo-circuit-2-photo-scaled.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-397 size-large\" src=\"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/01\/workshop-4-piezo-circuit-2-photo-1024x1015.jpg\" alt=\"\" width=\"480\" height=\"476\" srcset=\"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/01\/workshop-4-piezo-circuit-2-photo-1024x1015.jpg 1024w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/01\/workshop-4-piezo-circuit-2-photo-300x297.jpg 300w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/01\/workshop-4-piezo-circuit-2-photo-150x150.jpg 150w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/01\/workshop-4-piezo-circuit-2-photo-768x761.jpg 768w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/01\/workshop-4-piezo-circuit-2-photo-1536x1522.jpg 1536w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/01\/workshop-4-piezo-circuit-2-photo-100x100.jpg 100w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/01\/workshop-4-piezo-circuit-2-photo-738x731.jpg 738w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/01\/workshop-4-piezo-circuit-2-photo-1458x1445.jpg 1458w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/01\/workshop-4-piezo-circuit-2-photo-996x987.jpg 996w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/01\/workshop-4-piezo-circuit-2-photo-1992x1974.jpg 1992w\" sizes=\"auto, (max-width: 480px) 100vw, 480px\" \/><\/a><figcaption id=\"caption-attachment-397\" class=\"wp-caption-text\">Fig 10. Circuit for the schematic in Figure 9. (Click to enlarge.)<\/figcaption><\/figure>\r\n<p>In this circuit I&#8217;ve added a positive power bus on the breadboard rail at the top of the image, and a negative power bus to the rail on the bottom. The Piezo is now connected to the negative power bus with the short blue jumper wire, and to digital pin 8 on the other side. For the potentiometer, on the side you can see in the picture, the left pin is connected to the negative power bus and the right pin is connected to the positive power bus, corresponding to the schematic. Then, the middle pin of the potentiometer (view obstructed) is connected to analog pin 0 with the green wire.<\/p>\r\n<p>The next step will be to modify our Arduino code so the Piezo tone is controlled by the potentiometer. The <code>analogRead()<\/code> command works by reading (or we might say sensing or detecting) the voltage value being received on one of the &#8220;ANALOG IN&#8221; pins. The <code>analogRead()<\/code> command takes one argument, which is a number that corresponds to which &#8220;ANALOG IN&#8221; pin you wish to read \u2013 so, 0, 1, 2, 3, 4, or 5. This command returns a numeric value corresponding to the voltage currently being detected on that pin. It maps this voltage into a numeric range from <code>0<\/code> to <code>1023<\/code>, so that a voltage of <code>LOW<\/code> (or 0V) corresponds to a value of <code>0<\/code> while a voltage of <code>HIGH<\/code> corresponds to a numeric value of <code>1023<\/code>. (By the way, here is the <a href=\"https:\/\/www.arduino.cc\/reference\/en\/language\/functions\/analog-io\/analogread\/\">full documentation for this command<\/a>.) We can then use this number somehow in our program code to affect the behavior of some other part of the circuit. In this particular case, I want to figure out some code so that when <code>analogRead()<\/code> returns <code>0<\/code>, we&#8217;ll play a low-pitched tone, and when it returns <code>1023<\/code>, we&#8217;ll play a higher-pitched tone. And (since this is analog and not digital) as the command returns any value in between, the tone will be adjust proportionally.<\/p>\r\n<p>Calculating the math to convert Hz to <code>delay()<\/code> values as we did above can get pretty tricky. Fortunately, Arduino offers a command to make this much easier for us: <code>tone()<\/code> (<a href=\"https:\/\/www.arduino.cc\/reference\/en\/language\/functions\/advanced-io\/tone\/\">documentation<\/a>). This command take two arguments, a pin number and a Hz value, and it then sets that pin to <code>HIGH<\/code> and <code>LOW<\/code> at a timing to produce the desired frequency.<\/p>\r\n<p>Create a new Arduino program (from the menu select File &gt; New) and type or copy\/paste the following code into it:<\/p>\r\n<pre>const int piezoPin = 8;\r\nconst int potPin = 0;\r\nvoid setup() {\r\n  pinMode(piezoPin, OUTPUT);\r\n}\r\nvoid loop() {\r\n  float potVal = analogRead(potPin);\r\n  tone(piezoPin,potVal);\r\n}\r\n<\/pre>\r\n<p>This sets a variable called <code>piezoPin<\/code> to <code>8<\/code> which corresponds to the digital pin where the Piezo is connected, and sets a variable called <code>potPin<\/code> to <code>0<\/code> which corresponds to the ANALOG IN pin where the potentiometer is connected. In <code>setup()<\/code> we tell Arduino that the <code>piezoPin<\/code> will be used for <code>OUTPUT<\/code>. Then in the main <code>loop()<\/code>, we call <code>analogRead(potPin)<\/code> to get the value on ANALOG IN pin 0, setting that value to a variable called <code>potVal<\/code>, and then we use <code>potVal<\/code> in the <code>tone()<\/code> command to set the Hz of <code>piezoPin<\/code>, pin 8.<\/p>\r\n<p>This won&#8217;t work quite right though. The range of numbers 0 to 1023 does not work so good as Hz values for audible tones. We need to scale this range of numbers into one that works for us. Fortunately Arduino also offers a command to do this: <code>map()<\/code> (<a href=\"https:\/\/www.arduino.cc\/reference\/en\/language\/functions\/math\/map\/\">documentation<\/a>). Make the following two changes to your program:<\/p>\r\n<pre>const int piezoPin = 8;\r\nconst int potPin = 0;\r\nvoid setup() {\r\n  pinMode(piezoPin, OUTPUT);\r\n}\r\nvoid loop() {\r\n  float potVal = analogRead(potPin);\r\n  <span style=\"background-color: yellow;\">int hz = map(potVal, 0,1023, 40,4000);<\/span>\r\n  tone(piezoPin,<span style=\"background-color: yellow;\">hz<\/span>);\r\n}\r\n<\/pre>\r\n<p>The <code>map()<\/code> command takes 5 arguments. The first is a variable with a value in some range \u2013 in this case, <code>potVal<\/code>, which can be any value between 0 and 1023. The next two arguments specify what that range of values is. And the final two arguments specify a new min\/max range, so that whatever that variable value is will be scaled proportionally into the new range. So in this case I&#8217;m mapping from the value range for <code>analogRead()<\/code> into a value range that will work for a good audible tone: 40 to 4000Hz. The <code>map()<\/code> command returns that value, which here we&#8217;re setting into a variable called <code>hz<\/code>. Then we&#8217;re passing <code>hz<\/code> in to the <code>tone()<\/code> command.<\/p>\r\n<p>Uploading this program to your Arduino board, you should be able to twist the potentiometer to control the tone of the Piezo.<\/p>\r\n<p><em>Lab notebook task #1<\/em>: Look at the schematic in Figure 11. This creates a <strong>voltage divider<\/strong> in that the sensor indicated by the red circle goes from 5V to analog input 0, and also connects to a pull-down resistor that goes to GROUND. Wire up this circuit. You can use Figure 10 as a starting point, or start from scratch. For the red circle component, you can use your phototransistor (the thing that looks like a clear LED with a flattop). You should be able to use the same code as above. If you can get that wired correctly, then adjusting the light shining onto the phototransistor should alter the tone of the Piezo in a fun way. Document this and add some notes about the process to your lab notebook.<\/p>\r\n<figure id=\"attachment_369\" aria-describedby=\"caption-attachment-369\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-circuit-3-schematic.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-369\" src=\"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-circuit-3-schematic.jpg\" alt=\"\" width=\"600\" height=\"430\" srcset=\"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-circuit-3-schematic.jpg 1024w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-circuit-3-schematic-300x215.jpg 300w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-circuit-3-schematic-768x551.jpg 768w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-circuit-3-schematic-738x529.jpg 738w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-piezo-circuit-3-schematic-996x714.jpg 996w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-369\" class=\"wp-caption-text\">Fig 11. Schematic for an analog input\/sensor controlling a Piezo. (Click to enlarge.)<\/figcaption><\/figure>\r\n<p><strong>Movement<\/strong><\/p>\r\n<p>Finally, let\u2019s experiment with generating some movement and motion as a form of output. We&#8217;ll do this with a small <strong>DC motor<\/strong> that came in your Arduino Student Kit. A <strong>motor<\/strong> is an electronic component that generates some kind of motion when activated with an electric current. There many different types of electric motors, the two main types being <strong>servo motors<\/strong> and <strong>stepper motors<\/strong>. <a href=\"https:\/\/en.wikipedia.org\/wiki\/Servomotor#Servomotors_vs._stepper_motors\">Wikipedia<\/a> has a good discussion of the differences. Our Arduino Student Kit came with a <strong>servo motor <\/strong>so that is what we&#8217;ll be using.<\/p>\r\n<p>The servo motor in your kit should be easy to find and is pictured in Figure 12. Notice that it has a plastic white connector without any pins. To connect this into a circuit, you will need to affix wires into the three pin holes of the connector. I like to try using red and black to correspond to the red and black wires of the motor. These correspond to the negative (black) and positive (red) connections of the motor. I suggest you do the same. I also like to use a white wire for the middle pin if possible, but any color will do.<\/p>\r\n<figure id=\"attachment_376\" aria-describedby=\"caption-attachment-376\" style=\"width: 480px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-servo-wires.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-376\" src=\"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-servo-wires.jpg\" alt=\"\" width=\"480\" height=\"306\" srcset=\"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-servo-wires.jpg 1024w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-servo-wires-300x191.jpg 300w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-servo-wires-768x489.jpg 768w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-servo-wires-738x470.jpg 738w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-servo-wires-996x634.jpg 996w\" sizes=\"auto, (max-width: 480px) 100vw, 480px\" \/><\/a><figcaption id=\"caption-attachment-376\" class=\"wp-caption-text\">Fig 12. The servo motor that came in our Arduino Kits. (Click to enlarge.)<\/figcaption><\/figure>\r\n<p>The <strong>servo motor<\/strong> has a small white plastic shaft which is the thing that will rotate. It comes with several different attachments which you can choose from depending on the application you are working on. I&#8217;ve attached the single arm pictured in Figure 12. When you\u2019re working with a servo on a project, you may want to screw this part in for greater stability, but for now, leaving it like this will probably make it easier to swap it out later.<\/p>\r\n<p>To get started, let\u2019s wire up this circuit pictured in Figure 13. Don&#8217;t be intimidated by this schematic. It&#8217;s really quite simple. See Figure 14 for a photo of a circuit that implements it.<\/p>\r\n<figure id=\"attachment_375\" aria-describedby=\"caption-attachment-375\" style=\"width: 480px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-servo-schematic.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-375\" src=\"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-servo-schematic.jpg\" alt=\"\" width=\"480\" height=\"375\" srcset=\"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-servo-schematic.jpg 1024w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-servo-schematic-300x235.jpg 300w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-servo-schematic-768x601.jpg 768w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-servo-schematic-738x577.jpg 738w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-servo-schematic-996x779.jpg 996w\" sizes=\"auto, (max-width: 480px) 100vw, 480px\" \/><\/a><figcaption id=\"caption-attachment-375\" class=\"wp-caption-text\">Fig 13. Schematic for wiring up a servo motor. (Click to enlarge.)<\/figcaption><\/figure>\r\n<figure id=\"attachment_377\" aria-describedby=\"caption-attachment-377\" style=\"width: 480px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-servo.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-377\" src=\"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-servo.jpg\" alt=\"\" width=\"480\" height=\"374\" srcset=\"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-servo.jpg 1024w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-servo-300x233.jpg 300w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-servo-768x598.jpg 768w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-servo-738x574.jpg 738w, https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-content\/uploads\/2022\/02\/workshop-4-servo-996x775.jpg 996w\" sizes=\"auto, (max-width: 480px) 100vw, 480px\" \/><\/a><figcaption id=\"caption-attachment-377\" class=\"wp-caption-text\">Fig 14. A circuit implementing the schematic from Figure 13. (Click to enlarge.)<\/figcaption><\/figure>\r\n<p>Walking through that, notice that I have connected both positive and negative power buses to the breadboard rails at the bottom of the picture. (The black wire that extends off-frame to the left is the one connected to GROUND on the Arduino at the bottom of the photo, and similarly, the red wire that extends off to the left connects to 5V on the Arduino.) The servo and the potentiometer are almost separately wired in a way. The potentiometer has one pin going to the negative power bus (the blue jumper wire), one to the positive (the red jumper wire), and the middle pin (green wire, view obstructed) connected to analog pin 0. The servo is similarly wired up to the positive and negative power bus, with the middle pin connecting to digital pin 9. This is the pin that will be used to control the motor.<\/p>\r\n<p>To make this work is very straightforward. Open up File &gt; Examples &gt; Servo &gt; Knob. If you\u2019ve wired up your circuit as above, you shouldn\u2019t need to modify this example code at all. Simply upload it to your board, twist the potentiometer, and see what happens.<\/p>\r\n<p>(A tip on working with your Servo from adafruit.com: \u201c<a href=\"https:\/\/learn.adafruit.com\/adafruit-arduino-lesson-14-servo-motors\/if-the-servo-misbehaves\">If the Servo Misbehaves<\/a>\u201d.)<\/p>\r\n<p><em>Lab notebook task #2<\/em>: Can you work with this circuit as you did in task #1 today? In other words, replace the potentiometer with a different analog sensor (perhaps the phototransistor) and see what happens? Remember your pull-down resistor!<\/p>\r\n<p><em>Lab notebook task #3<\/em>: Building on task #2, think about some non-circuit crafting you can do to make your servo motor into a kind of meter. Affix it to a piece of paper or board so that as you adjust your phototransistor (or other analog sensor), the servo moves a pointer indicating levels of brightness (or whatever your sensor is detecting).<\/p>\r\n<p><strong>Powering your Arduino \u201coff-the-grid\u201d<\/strong><\/p>\r\n<p>Since I think some of you will be thinking about disconnecting your Arduino from USB (\u201cfrom the grid\u201d) for the project and powering it via batteries instead, here are a few blog posts that I found helpful in thinking about this:<\/p>\r\n<ol>\r\n<li>An <a href=\"https:\/\/www.instructables.com\/Powering-Arduino-with-a-Battery\/\">article about how to do this<\/a><\/li>\r\n<li>A cautionary blog post about why the above isn\u2019t a great idea: \u201c<a href=\"https:\/\/www.baldengineer.com\/9v-batteries-suck.html\">9V Batteries suck and why you shouldn\u2019t use them<\/a>\u201d<\/li>\r\n<li>And another harsh reality check: \u201c<a href=\"https:\/\/cybergibbons.com\/arduino\/arduino-misconceptions-6-a-9v-battery-is-a-good-power-source\/\">Arduino misconceptions 6: a 9V battery is a good power source<\/a>\u201d<\/li>\r\n<li>But some reason for hope for the possibility of <a href=\"https:\/\/makecademy.com\/arduino-battery\">a workable solution<\/a><\/li>\r\n<\/ol>\r\n<p>In the end, there is not a great way to power your Arduino without being connected to USB. I\u2019m looking into it. When we start working on projects, feel free to run them off USB, and we can all keep in mind that Arduino could be used in an off-grid context if we wanted to buy some more powerful batteries, or work out solar solutions. The project can still be a good learning experience if it forces us to think about these questions, even if we don\u2019t have a neat and tidy solution to all of them.<\/p>\r\n<p>&nbsp;<\/p>\r\n<p>&nbsp;<\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>Technical Tutorial #4 Recorded video tutorial. Searchable transcript with timestamps. Review Last week we looked at different techniques and components that can operate as digital and analog inputs. We saw some techniques for how you might connect these sensors to an Arduino board, including a voltage divider to make sure that any inputs are not &#8230;<\/p>\n","protected":false},"author":1,"featured_media":362,"comment_status":"open","ping_status":"closed","template":"","meta":{"footnotes":""},"portfolio-category":[7],"portfolio-tag":[],"class_list":["post-165","portfolio","type-portfolio","status-publish","has-post-thumbnail","hentry","portfolio-category-technical-tutorials"],"_links":{"self":[{"href":"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-json\/wp\/v2\/portfolio\/165","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-json\/wp\/v2\/portfolio"}],"about":[{"href":"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-json\/wp\/v2\/types\/portfolio"}],"author":[{"embeddable":true,"href":"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-json\/wp\/v2\/comments?post=165"}],"version-history":[{"count":10,"href":"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-json\/wp\/v2\/portfolio\/165\/revisions"}],"predecessor-version":[{"id":410,"href":"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-json\/wp\/v2\/portfolio\/165\/revisions\/410"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-json\/wp\/v2\/media\/362"}],"wp:attachment":[{"href":"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-json\/wp\/v2\/media?parent=165"}],"wp:term":[{"taxonomy":"portfolio-category","embeddable":true,"href":"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-json\/wp\/v2\/portfolio-category?post=165"},{"taxonomy":"portfolio-tag","embeddable":true,"href":"https:\/\/classes.codeatlang.com\/digital-media-off-the-grid\/2022-spring\/wp-json\/wp\/v2\/portfolio-tag?post=165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}