{"id":2332,"date":"2017-07-27T21:18:36","date_gmt":"2017-07-27T21:18:36","guid":{"rendered":"http:\/\/blog.paranoidprofessor.com\/?p=2332"},"modified":"2017-07-30T21:22:43","modified_gmt":"2017-07-30T21:22:43","slug":"from-computers-to-microcontrollers","status":"publish","type":"post","link":"https:\/\/blog.paranoidprofessor.com\/index.php\/2017\/07\/27\/from-computers-to-microcontrollers\/","title":{"rendered":"From computers to microcontrollers"},"content":{"rendered":"<p>I kept telling Mikhail that the big advantage to using the Raspberry Pi was no matter what project I put it into I could set it on the window sill and <a href=\"https:\/\/en.wikipedia.org\/wiki\/Secure_Shell\" target=\"_blank\">ssh<\/a> to it. \u00a0It was a fast small and affordable personal computer. \u00a0He kept telling me that the Raspberry Pi and Raspbian wasn&#8217;t a real-time solution and that I should consider something else.<\/p>\n<p>He was of course right but this didn&#8217;t matter for most of the things that I was using the Raspberry Pi. \u00a0The best example of a custom solution was the<a href=\"http:\/\/blog.paranoidprofessor.com\/index.php\/2016\/09\/24\/just-making-it-a-4x4x4-led-cube\/\"> led cube<\/a> that I created that displayed various patterns.<\/p>\n<p>The Raspberry pi did a super job for my cube but when I tried to use it on a distance sensor project using the <a href=\"https:\/\/www.amazon.de\/HC-SR04-Ultrasonic-Distance-Measuring-arduino\/dp\/B01J4K0Z1A\/ref=sr_1_fkmr0_1?ie=UTF8&amp;qid=1494967788&amp;sr=8-1-fkmr0&amp;keywords=New+for+Arduino+Ultrasonic+Module+HC-SR04+Distance+Sensor+Measuring+Transducer\" target=\"_blank\">HC SR04<\/a>, I definitely had mixed results.<\/p>\n<p>I wanted to use my cube more often but there was no convenient way to turn it off, well without first connecting to it via ssh and shutting it down. \u00a0It took too long to boot, too long to shutdown, and requires a proper shutdown. \u00a0What about migrating from a computer to a micro-controller\u00a0&#8211; it starts faster and can simply be powered off.<\/p>\n<h2>Cheap as potato chips<\/h2>\n<p>Mikhail kept telling me that micro-controllers were cheap as potato chips and could easily be included into virtually any project. To encourage this path he decided to do this old school which meant a pile of chips, an AVR programmer, LED&#8217;s, resistors, a breadboard and a bunch of wires.<\/p>\n<div id=\"attachment_2368\" style=\"width: 3630px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/05\/programmer.jpg\"><img aria-describedby=\"caption-attachment-2368\" decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-2368\" src=\"http:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/05\/programmer.jpg\" alt=\"\" width=\"3620\" height=\"2144\" srcset=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/05\/programmer.jpg 3620w, https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/05\/programmer-300x178.jpg 300w, https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/05\/programmer-768x455.jpg 768w, https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/05\/programmer-1024x606.jpg 1024w\" sizes=\"(max-width: 3620px) 100vw, 3620px\" \/><\/a><p id=\"caption-attachment-2368\" class=\"wp-caption-text\">AVR Programmer<\/p><\/div>\n<div id=\"attachment_2369\" style=\"width: 650px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/05\/single-attiny85.jpg\"><img aria-describedby=\"caption-attachment-2369\" decoding=\"async\" loading=\"lazy\" class=\"size-large wp-image-2369\" src=\"http:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/05\/single-attiny85-768x1024.jpg\" alt=\"\" width=\"640\" height=\"853\" srcset=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/05\/single-attiny85-768x1024.jpg 768w, https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/05\/single-attiny85-225x300.jpg 225w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/a><p id=\"caption-attachment-2369\" class=\"wp-caption-text\">attiny85<\/p><\/div>\n<p>Mikhail comes from the electrical engineering side of computers and it was him who discovered, all those years back, some of the interesting projects being done with LED&#8217;s. \u00a0Below are a few interesting projects that he showed me from the internet.<\/p>\n<div style=\"margin-left: 1cm;\">\n<p><a href=\"https:\/\/www.youtube.com\/watch?v=Vn39txtVIHc\" target=\"_blank\">3D spectral analyzer<\/a><\/p>\n<p><a href=\"https:\/\/youtu.be\/guppB4cK3oU\" target=\"_blank\">8x8x8 LED cube<\/a><\/p>\n<\/div>\n<p>Well, I come from the software side but was willing to learn. \u00a0I no longer have photos of those old projects but I put together a rather similar example.<\/p>\n<p>Instead of one of those ATTINY85&#8217;s I have used an ATMEGA328. \u00a0Granted the 328 is a much nicer chip but for the task of doing I2C the two chips are equivalent.<\/p>\n<p><a href=\"http:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/05\/atmegaproject.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-2391\" src=\"http:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/05\/atmegaproject.jpg\" alt=\"\" width=\"2618\" height=\"3371\" srcset=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/05\/atmegaproject.jpg 2618w, https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/05\/atmegaproject-233x300.jpg 233w, https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/05\/atmegaproject-768x989.jpg 768w, https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/05\/atmegaproject-795x1024.jpg 795w\" sizes=\"(max-width: 2618px) 100vw, 2618px\" \/><\/a><\/p>\n<h2>Hardware setup<\/h2>\n<p>It has been awhile since I used Fritzing or Eagle to write up a circuit diagram. \u00a0I will simply show which pins need to be connected up.<\/p>\n<table class=\"w3-table-all\">\n<tbody>\n<tr>\n<th>ATMEGA328<\/th>\n<th>MCP 23017<\/th>\n<th>Comments<\/th>\n<\/tr>\n<tr>\n<td>A4 (pin 24)<\/td>\n<td>Pin 12<\/td>\n<td>I2C pin SCA<\/td>\n<\/tr>\n<tr>\n<td>A5 (pin 23)<\/td>\n<td>Pin 13<\/td>\n<td>I2C pin\u00a0SDA<\/td>\n<\/tr>\n<tr>\n<td>5V<\/td>\n<td>Pin 9 (Vdd)<\/td>\n<td>5 V power<\/td>\n<\/tr>\n<tr>\n<td>GND<\/td>\n<td>Pin 10 (vss)<\/td>\n<td>Ground<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>Pin 15 (A0)<\/td>\n<td>tie to ground<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>Pin 16 (A1)<\/td>\n<td>tie to ground<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>Pin 17 (A2)<\/td>\n<td>tie to ground<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>Pin 18 (! reset)<\/td>\n<td>tie to 5v<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>GPA 4<\/td>\n<td>connect to led<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>GPA\u00a05<\/td>\n<td>connect to led<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>GPA\u00a06<\/td>\n<td>connect to led<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>I2C explained<\/h2>\n<p>The I2C bus was created by Philips in the 1980&#8217;s. \u00a0It was actually a rather clever design decision for connecting CPU&#8217;s and peripheral chips together. \u00a0The use of a single bus is much more efficient in terms of wire used. \u00a0All chips on the bus do listen for commands that are relevant to them. This is is because each chip has its own address and thus listens for its own address.<\/p>\n<p>It is possible to add any number of bytes via this bus to the receiving chip. \u00a0The bus is bi-directional, I can send messages to another chip but I can also read back responses from those chips.<\/p>\n<p>The actual description of the how the low level technical details for this\u00a0two line bus is actually better described elsewhere. \u00a0One explanation can be found on <a href=\"https:\/\/en.wikipedia.org\/wiki\/I%C2%B2C\" target=\"_blank\">Wikipedia I2C<\/a>.<\/p>\n<p>The good news is that you don&#8217;t need to know how the bits are sent over the wire to use it. \u00a0Arduino has a very simple class Wire which makes this task almost trivial.<\/p>\n<table class=\"w3-table-all\">\n<tbody>\n<tr>\n<th>Wire Method<\/th>\n<th>Description<\/th>\n<th>Example<\/th>\n<\/tr>\n<tr>\n<td>begin()<\/td>\n<td>initializes connect to bus<\/td>\n<td>Wire.begin()<\/td>\n<\/tr>\n<tr>\n<td>beginTransmission(adr);<\/td>\n<td>begin command to send to device &#8220;adr&#8221;<\/td>\n<td>Wire.beginTransmission(0x20);<\/td>\n<\/tr>\n<tr>\n<td>write(byte2send)<\/td>\n<td>sends a single byte to device from begintransmission<\/td>\n<td>Wire.write(0x00);<\/td>\n<\/tr>\n<tr>\n<td>endTransmission()<\/td>\n<td>builds entire command and transmits it to device.<\/td>\n<td>Wire.endTransmission();<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Lines 10 &#8211; 13 is an actual example of sending the value 0x00 to the register &#8220;port&#8221; on device 0x20.<\/p>\n<h2>Software<\/h2>\n<p>This sample bit of code will simply turn on the green led, yellow led and red led in turn with a pause between each one and then it will turn all leds on for 1500 milliseconds. \u00a0Finally it will turn off all the leds for 1000 milliseconds (one second).<\/p>\n<p>This particular project is not completely interesting in itself but rather as a stepping stone to more interesting devices. \u00a0I will be replacing my raspberry pi with this Arduino in my next blog.<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n#include &lt;Wire.h&gt;\r\n\r\nint GPA4 = 0x04;\r\nint GPA5 = 0x08;\r\nint GPA6 = 0x10;\r\nint PORTA = 0x12;\r\n\r\nvoid alloff(int port)\r\n{\r\n  Wire.beginTransmission(0x20);\r\n  Wire.write(port);\r\n  Wire.write(0x00); \/\/ all pins off\r\n  Wire.endTransmission();\r\n}\r\n\r\nvoid scanner()\r\n{\r\n  Serial.println (&quot;\\nI2C scanner. Scanning ...&quot;);\r\n  int found = 0;\r\n\r\n  for (byte i = 1; i &lt; 120; i++)\r\n  {\r\n    Wire.beginTransmission (i);\r\n    if (Wire.endTransmission () == 0)\r\n    {\r\n      #if 0\r\n      \/\/ this is much simpler but takes 2k more of program space.\r\n      char buffer[128];\r\n      sprintf(buffer,&quot;Found address: 0x%02x&quot;,i);\r\n      Serial.println(buffer);\r\n      #endif\r\n      \r\n      Serial.print (&quot;Found address: &quot;);\r\n      Serial.print (&quot; (0x&quot;);\r\n      Serial.print (i, HEX);\r\n      Serial.println (&quot;)&quot;);\r\n      found = 1;\r\n      delay (10);\r\n    }\r\n  }\r\n\r\n  if (found)\r\n    Serial.print (&quot;Found device(s).&quot;);\r\n  Serial.println();\r\n}  \/\/ end of setup\r\n\r\nvoid setup()\r\n{\r\n  \/\/ real initialisation stuff\r\n  Serial.begin (115200);\r\n  Wire.begin();\r\n\r\n  \/\/ do a bit of searching for things.\r\n  scanner();\r\n\r\n  \/\/ setup our 23017\r\n  Wire.beginTransmission(0x20);\r\n  Wire.write(0x00); \/\/ IODIRA register\r\n  Wire.write(0x00); \/\/ set all of port A to outputs\r\n  Wire.endTransmission();\r\n\r\n\/* we dont actually use port b\r\n  Wire.beginTransmission(0x20);\r\n  Wire.write(0x01); \/\/ IODIRB register\r\n  Wire.write(0x00); \/\/ set all of port B to outputs\r\n  Wire.endTransmission();\r\n*\/\r\n  alloff(PORTA);\r\n}\r\n\r\nvoid expanderPinOn(int port, int pin)\r\n{\r\n  Wire.beginTransmission(0x20);\r\n  Wire.write(port);\r\n  Wire.write(pin);\r\n  Wire.endTransmission();\r\n}\r\n\r\nvoid loop()\r\n{\r\n  expanderPinOn(PORTA, GPA6);\r\n  delay(600);\r\n  expanderPinOn(PORTA, GPA5);\r\n  delay(600);\r\n  expanderPinOn(PORTA, GPA4);\r\n  delay(600);\r\n  expanderPinOn(PORTA, GPA4 | GPA5 | GPA6);\r\n  delay(1500);\r\n\r\n  \/\/ off\r\n  alloff(PORTA);\r\n  delay(1000);\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I kept telling Mikhail that the big advantage to using the Raspberry Pi was no matter what project I put it into I could set it on the window sill and ssh to it. \u00a0It was a fast small and &hellip; <a href=\"https:\/\/blog.paranoidprofessor.com\/index.php\/2017\/07\/27\/from-computers-to-microcontrollers\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[20,3],"tags":[80,23],"_links":{"self":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/2332"}],"collection":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/comments?post=2332"}],"version-history":[{"count":14,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/2332\/revisions"}],"predecessor-version":[{"id":2502,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/2332\/revisions\/2502"}],"wp:attachment":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/media?parent=2332"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/categories?post=2332"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/tags?post=2332"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}