{"id":812,"date":"2017-06-28T16:46:41","date_gmt":"2017-06-28T20:46:41","guid":{"rendered":"http:\/\/www.circuitcrush.com\/?p=812"},"modified":"2021-06-30T21:00:12","modified_gmt":"2021-07-01T01:00:12","slug":"look-inside-arduino","status":"publish","type":"post","link":"https:\/\/www.circuitcrush.com\/look-inside-arduino\/","title":{"rendered":"A Closer Look Inside the Arduino Uno"},"content":{"rendered":"<p>Since their inception in 2005, Arduino development boards have become a favorite among hobbyists, makers, and experimenters.<\/p>\n<p>Of all the different incarnations, the Uno remains one of the most popular.<\/p>\n<p><!--more--><\/p>\n<p>In fact, if you\u2019ve been dabbling in electronics for more than a few minutes or have worked with microcontrollers, chances are you\u2019ve used it before or have at least heard of it.<\/p>\n<p>A while ago, I wrote an article about using the Arduino vs a stand-alone microcontroller (like the ATmega328 that powers the Uno or a PIC).<\/p>\n<p>They both have their pros and cons, so if you’re interested check out <a href=\"http:\/\/www.circuitcrush.com\/working-directly-with-microcontrollers-vs-arduino\/\" target=\"_blank\" rel=\"noopener\">Getting Naked: Working With Naked Microcontrollers vs Trainers Like Arduino<\/a>.<\/p>\n<p>Though many of us have used the Arduino Uno, most of us seldom think of how it works or what\u2019s under the hood. We\u2019re going to take a closer look inside the Uno and see what makes it tick.<\/p>\n<p>Actually, this will likely be the first of a series of posts about the inner workings of the Uno and the nuances of working with it. I may not write them in order (I may write about other topics before posting part 2), but they\u2019ll eventually come.<\/p>\n<p><strong><em>If you\u2019re looking for the Arduino Uno and ATmega328 pin-out, see figures 1 and 2. <\/em><\/strong><\/p>\n<p>Now, let\u2019s have a closer look inside the Arduino Uno.<\/p>\n<h1><strong>Why is the Arduino so Popular?<br \/>\n<\/strong><\/h1>\n<p>Made in Italy, the Arduino showed up on the scene in 2005. Since then, there have been various incarnations of the board including the Duemilanove (2009 in Italian), the Diecimila (which means 10,000 in Italian to celebrate the making of the 10,000<sup>th<\/sup> one), the Mega 2560, and more.<\/p>\n<p>The boards are very versatile and can handle most common things microcontrollers can do, and the price is low.<\/p>\n<p>Then there\u2019s the fact that the software to program it is free and can be used on Windows, Mac, or even Linux.<\/p>\n<p>The hardware and the software are both open source, so others can take the designs and improve them without worrying about licensing fees.<\/p>\n<p>There are many <em>shields<\/em> that are available which easily attach to the board adding functionality.<\/p>\n<p>And finally, they\u2019re great for experimenting and prototyping projects (though my Getting Naked post explains why you may want to go for a stand-alone micro instead).<\/p>\n<h1><strong>Quick Arduino Overview<br \/>\n<\/strong><\/h1>\n<p>Refer to figure 1, which shows the Arduino Uno.<\/p>\n<p> <\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone wp-image-1665\" src=\"http:\/\/www.circuitcrush.com\/wp-content\/uploads\/arduino_uno_2.jpg\" alt=\"Arduino Uno Board Parts\" width=\"675\" height=\"429\" srcset=\"https:\/\/www.circuitcrush.com\/wp-content\/uploads\/arduino_uno_2.jpg 629w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/arduino_uno_2-600x382.jpg 600w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/arduino_uno_2-150x95.jpg 150w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/arduino_uno_2-300x191.jpg 300w\" sizes=\"(max-width: 675px) 100vw, 675px\" \/><\/p>\n<p><em>Figure 1: the Arduino Uno board<\/em><\/p>\n<p>Obviously, the rectangular thing in the lower right is the brains of the Uno, the ATmega328.<\/p>\n<p>The power jack on the lower left is a 2.1mm center positive barrel connector. Above that, we can see a USB Type B jack for connecting the Arduino to a PC.<\/p>\n<h3 style=\"text-align: center;\">Become the Maker you were born to be. Try <a href=\"https:\/\/learnarduinonow.com\">Arduino Academy<\/a> for FREE!<\/h3>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-4238\" src=\"https:\/\/www.circuitcrush.com\/wp-content\/uploads\/FB_Cover2.png\" alt=\"\" width=\"407\" height=\"155\" srcset=\"https:\/\/www.circuitcrush.com\/wp-content\/uploads\/FB_Cover2.png 828w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/FB_Cover2-300x114.png 300w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/FB_Cover2-150x57.png 150w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/FB_Cover2-768x292.png 768w\" sizes=\"(max-width: 407px) 100vw, 407px\" \/><\/p>\n<p>A series of 28 female pin headers allow you to connect other things to the Arduino and are at the top and bottom. These headers are separated into three groups. The digital pins are at the top. On the bottom left we have the power pins, and the analog pins dwell on the lower right (notice the small gap between them). Out of the 28 pins, 20 are for I\/O.<\/p>\n<p>The six analog pins can also serve as general purpose digital I\/O. Out of the 14-total digital I\/O pins, six can be used to generate PWM (pulse width modulation) signals.<\/p>\n<p>The Arduino Uno also supports basic communications standards like TTL serial, SPI, I2C, and 1-wire.<\/p>\n<p>Two of the inputs support hardware interrupts. They trigger on either a LOW, a rising edge or falling edge, or a change in value via software.<\/p>\n<p>The oval-shaped silver object in the middle left is the 16 MHz crystal.<\/p>\n<p>To the right of the power jack are two 47 \u00b5F capacitors. We\u2019ll take a closer look at these in a future post.<\/p>\n<p>Directly above the jack lies a 5 V low dropout voltage regulator. Directly above the capacitors closest to the microcontroller we can see a 3.3 V low dropout regulator. It lies near two surface mount capacitors.<\/p>\n<p>Powering the Arduino is an often overlooked, but a very important part in the effective use of the device. Many spurious errors are actually power issues. We\u2019ll go into more detail on properly powering the Arduino in a different post.<\/p>\n<h1><strong>A Closer Look at the Arduino Uno<br \/>\n<\/strong><\/h1>\n<p>An Arduino Uno board measures 2 1\/8\u201d x 2 \u00be\u201d. The SMD version is <em>slightly<\/em> different but most people want the ability to easily replace the ATmega328 microcontroller if they accidentally kill it. If you\u2019re new to Arduino, electronics, microcontrollers, or some combination of the above I\u2019d skip the SMD version and grab the one in figure 1.<\/p>\n<p>The operating voltage of the Arduino Uno is 5 V which can come from either a USB cable and your PC or some external source.<\/p>\n<p>The recommended input voltage range is 7 \u2013 12 V. You should try to keep that number closer to 7 V if you can as the small regulator will dissipate any excess energy as heat and can become very hot. We\u2019ll go into more detail on this issue in another post.<\/p>\n<p>The Arduino Uno contains two regulators. The 5 V regulator can theoretically provide up to 800 mA and the 3.3 V version can source 50 mA.<\/p>\n<h2><strong>What Else is Onboard the Arduino Uno Board?<br \/>\n<\/strong><\/h2>\n<p>Aside from the microcontroller, the Uno has a few other main points of interest.<\/p>\n<p>There is an integrated USB-to-serial communications chip for downloading programs or \u201csketches\u201d from your PC and for serial communications back to the PC for debugging and monitoring.<\/p>\n<p>This chip is the small black box above the 16MHz crystal in figure 1.<\/p>\n<p>The USB link sports a 500 mA resettable fuse to guard against any possible damage to your PC. When you plug your Arduino into a USB port, the board takes its power from that port. In USB 2.0, the current a port is capable of sourcing is usually 500mA. USB 3.0 ports can source anywhere from 900 mA to 3 A depending on the type of port.<\/p>\n<p>The analog pins connect to an internal 10-bit analog-to-digital converter (ADC). All the I\/O pins can function as digital outputs and can sink\/source up to 40 mA.<\/p>\n<p>Next to the analog pins lie the power pins which provide access to both the regulated and unregulated power supplies.<\/p>\n<h2><strong>Arduino Pin-Out and Microcontroller<br \/>\n<\/strong><\/h2>\n<p>The ATmega328 chip on the Arduino isn\u2019t empty, it comes pre-loaded with a small bootloader for use with the Arduino integrated development environment (IDE).<\/p>\n<p>One nice thing about the Arduino is that you can use it to program other ATmega328 chips if you were to build a project with the stand-alone microcontroller.<\/p>\n<p>To do this, you\u2019d program the chip, remove it and then insert it into your own circuit. All it needs is 5 V, a 16 MHz crystal, and two 22 pF capacitors.<\/p>\n<p>There are a few more minor details on this and it\u2019s something that I personally have not done, but I know its possible. Perhaps we\u2019ll revisit the topic in another post once I get a chance to play with it.<\/p>\n<p> <\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-1666\" src=\"http:\/\/www.circuitcrush.com\/wp-content\/uploads\/Arduino-To-Atmega8-Pins.png\" alt=\"Arduino pin-out \/ Arduino to ATmega328 pin mapping\" width=\"524\" height=\"341\" srcset=\"https:\/\/www.circuitcrush.com\/wp-content\/uploads\/Arduino-To-Atmega8-Pins.png 524w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/Arduino-To-Atmega8-Pins-150x98.png 150w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/Arduino-To-Atmega8-Pins-300x195.png 300w\" sizes=\"(max-width: 524px) 100vw, 524px\" \/><\/p>\n<p><em>Figure 2: ATmega328 pin-out and Arduino I\/O pin mapping<\/em><\/p>\n<p>Feast your eyes upon figure 2, which shows a pin-out diagram of the ATmega328.<\/p>\n<p>One of the first things to note is that the ATmega328 pin numbers are different from the Arduino Uno pin numbers. This is important!<\/p>\n<p>The labels black labels outside the chip are the primary function names for each of the pins, while the parenthetical labels on the outside are any alternative uses for the pin, if any are available. The writing in red shows the ATmega328-to-Arduino pin mapping.<\/p>\n<p>Figure 3 shows a simplified block diagram of the ATmega328 microcontroller.<\/p>\n<p>The I\/O block is the 20 analog and digital pins. The six analog pins go to the ADC, which has a resolution of 4.9 mV because there are 1024 steps (remember, the ADC is 10-bits and that 2<sup>10<\/sup> = 1024) and when we divide 5 V by 1024, we get 4.9 mV per step.<\/p>\n<p> <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1667\" src=\"http:\/\/www.circuitcrush.com\/wp-content\/uploads\/ATMega328-Simple-Block-Diagram.jpg\" alt=\"ATmega328 block diagram\" width=\"546\" height=\"467\" srcset=\"https:\/\/www.circuitcrush.com\/wp-content\/uploads\/ATMega328-Simple-Block-Diagram.jpg 707w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/ATMega328-Simple-Block-Diagram-600x513.jpg 600w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/ATMega328-Simple-Block-Diagram-150x128.jpg 150w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/ATMega328-Simple-Block-Diagram-300x257.jpg 300w\" sizes=\"(max-width: 546px) 100vw, 546px\" \/><\/p>\n<p><em>Figure 3: ATmega328 simplified block diagram<\/em><\/p>\n<p>The two external interrupts that the chip supports map to Arduino digital pins D2 and D3.<\/p>\n<p>There was a time when the analog comparator was not accessible through the Arduino IDE.<\/p>\n<p>Personally, I have not tried to use this feature myself, but some quick searching seemed to suggest that there are now ways to use the comparator with the Arduino IDE.<\/p>\n<p>The analog comparator will trigger an interrupt when voltage on one input equals or exceeds the voltage on another input. This could come in handy for certain projects. For example, say you want a fan to come on when the air reaches a certain temperature.<\/p>\n<h2><strong>A Few Words About Arduino Sketches<br \/>\n<\/strong><\/h2>\n<p>As of this writing, the current version of the Arduino IDE is version 1.8.3.<\/p>\n<p>The Arduino comes with a Java-based IDE with a fully featured text editor. Once you write a sketch, it needs to be compiled which is referred to as <em>verifying<\/em> in the Arduino ecosystem.<\/p>\n<p>Sketches are written in a language similar to C, though a sketch itself is not completely compatible with C.<\/p>\n<p>There\u2019s no <em>main()<\/em> function for one thing, at least not one that\u2019s visible. It\u2019s hidden from view and added for you when you compile or verify your sketch.<\/p>\n<p>The sketch downloads to your Arduino via a USB cable. This process is automatic as the bootloader that resides in ATmega328 detects when a sketch is arriving.<\/p>\n<p>The sketch takes residence in the 32 Kb of flash memory inside the ATmega chip. This memory can endure at least 10,000 read\/write cycles, so you\u2019re unlikely to wear it out any time soon. The Arduino also sports 1 Kb of electrically erasable programmable read only memory (EEPROM) which is non-volatile. Finally, there is also 2 Kb of RAM available which <em>is<\/em> volatile.<\/p>\n<p>All Arduino sketches need two parts at a minimum: the <em>setup()<\/em> and <em>loop() <\/em>functions. These two functions take no arguments — so the parentheses are empty — but they still need to be there.<\/p>\n<p>They appear as follows:<\/p>\n<p>Void setup() { any code would go here<\/p>\n<p>}<\/p>\n<p>Void loop() { any code would go here<\/p>\n<p>}<\/p>\n<p>If you\u2019re familiar with C, you know that <em>void<\/em> in front of the functions simply means that the function does not return a value when it\u2019s done processing.<\/p>\n<p>These two functions are a requirement and the Arduino IDE will report an error during compile if any of the two are missing.<\/p>\n<p>Often, the makers of the board will release updated versions of the IDE. When they do, it\u2019s a good idea to download them and install them. You can keep multiple versions of the Arduino IDE on your PC and switch between them if you want, but this isn\u2019t something you should need to do often. If you do install the latest IDE, be sure to take a gander at the readme file for the latest changes.<\/p>\n<h2><strong>More Inner Workings of the Arduino Yet to Come<br \/>\n<\/strong><\/h2>\n<p>Many of you have used the Uno in your projects and experiments, but now you have a little more detail on how the board and the brains (the ATmega328) work.<\/p>\n<p>Future installments of this series will go into more detail about powering the board, a very important but often overlooked issue. We\u2019ll also take a closer look at the digital and analog I\/O pins, as there are some \u201cgotchas\u201d hiding there.<\/p>\n<p>Meanwhile, comment and let us know about your latest Arduino experiments, projects, and endeavors!<\/p>\n<h2 style=\"text-align: center;\">Become the Maker you were born to be. Try <a href=\"https:\/\/learnarduinonow.com\">Arduino Academy<\/a> for FREE!<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4238\" src=\"https:\/\/www.circuitcrush.com\/wp-content\/uploads\/FB_Cover2.png\" alt=\"\" width=\"828\" height=\"315\" srcset=\"https:\/\/www.circuitcrush.com\/wp-content\/uploads\/FB_Cover2.png 828w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/FB_Cover2-300x114.png 300w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/FB_Cover2-150x57.png 150w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/FB_Cover2-768x292.png 768w\" sizes=\"(max-width: 828px) 100vw, 828px\" \/><\/p>\n<p><strong><em>\u00a0<\/em><\/strong><\/p>\n<a target=\"_blank\" href=\"https:\/\/www.drpeterscode.com\/index.php\"><img src=\"https:\/\/www.circuitcrush.com\/wp-content\/plugins\/dpabottomofpostpage\/apixel1x1.jpg\" ><\/a><table><\/table>","protected":false},"excerpt":{"rendered":"<p>Since their inception in 2005, Arduino development boards have become a favorite among hobbyists, makers, and experimenters. Of all the different incarnations, the Uno remains one of the most popular.<\/p>\n","protected":false},"author":1,"featured_media":818,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[32],"tags":[34],"class_list":{"0":"post-812","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-arduino","8":"tag-arduino","9":"entry"},"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/www.circuitcrush.com\/wp-content\/uploads\/Arduino-Uno.jpg","_links":{"self":[{"href":"https:\/\/www.circuitcrush.com\/wp-json\/wp\/v2\/posts\/812","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.circuitcrush.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.circuitcrush.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.circuitcrush.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.circuitcrush.com\/wp-json\/wp\/v2\/comments?post=812"}],"version-history":[{"count":2,"href":"https:\/\/www.circuitcrush.com\/wp-json\/wp\/v2\/posts\/812\/revisions"}],"predecessor-version":[{"id":4329,"href":"https:\/\/www.circuitcrush.com\/wp-json\/wp\/v2\/posts\/812\/revisions\/4329"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.circuitcrush.com\/wp-json\/wp\/v2\/media\/818"}],"wp:attachment":[{"href":"https:\/\/www.circuitcrush.com\/wp-json\/wp\/v2\/media?parent=812"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.circuitcrush.com\/wp-json\/wp\/v2\/categories?post=812"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.circuitcrush.com\/wp-json\/wp\/v2\/tags?post=812"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}