{"id":91,"date":"2013-09-23T21:26:48","date_gmt":"2013-09-23T10:26:48","guid":{"rendered":"https:\/\/www.newioit.com.au\/?p=91"},"modified":"2017-03-28T22:50:28","modified_gmt":"2017-03-28T11:50:28","slug":"%ef%bb%bf%ef%bb%bfebay-cheapies-arduino-mega-and-8x8-led-matrix-wmax7219-saleae-logic-analyzer","status":"publish","type":"post","link":"https:\/\/www.newioit.com.au\/blog\/archives\/91","title":{"rendered":"\ufeff\ufeffEbay Cheapies: Arduino Mega and 8&#215;8 LED Matrix w\/MAX7219 + &#8216;Saleae&#8217; Logic Analyzer"},"content":{"rendered":"<h1>\u00a0Ebay Cheapies 001<\/h1>\n<p>A couple of my ebay cheap buys have arrived. I have an Arduino Mega for $12, and a 8&#215;8 LED Matrix with a (fake) MAX7219 driver for $3. To test them out, i&#8217;ve hooked them both up, and wrote some Arduino code to show what they can do.<\/p>\n<p>I usually don&#8217;t use Arduino for Projects, but for testing out some parts it works great! Don&#8217;t even have to get my MKII programmer out fo the drawer.<\/p>\n<p>Also as a bonus, my knock off Saleae Logic Anayzler ($10) has arrived. I&#8217;ve used it to plot out the logic of shifting the data from the Arduino to the MAX7219.<\/p>\n<p>Not much else to say here, so lets get into some pictures, code, and a video!<\/p>\n<p><strong>MAX7219 kit:<\/strong> Pretty simple setup here, the MAX7219 provides 3 data lines to the arduino, which allows the 8&#215;8 matrix to set which leds are to be lit.<\/p>\n<p><a href=\"https:\/\/www.newioit.com.au\/blog\/wp-content\/uploads\/2013\/09\/ledmatrix_parts.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-103\" src=\"https:\/\/www.newioit.com.au\/blog\/wp-content\/uploads\/2013\/09\/ledmatrix_parts.jpg\" alt=\"ledmatrix_parts\" width=\"716\" height=\"561\" srcset=\"https:\/\/www.newioit.com.au\/blog\/wp-content\/uploads\/2013\/09\/ledmatrix_parts.jpg 716w, https:\/\/www.newioit.com.au\/blog\/wp-content\/uploads\/2013\/09\/ledmatrix_parts-300x235.jpg 300w, https:\/\/www.newioit.com.au\/blog\/wp-content\/uploads\/2013\/09\/ledmatrix_parts-624x488.jpg 624w\" sizes=\"auto, (max-width: 716px) 100vw, 716px\" \/><\/a><a href=\"https:\/\/www.newioit.com.au\/blog\/wp-content\/uploads\/2013\/09\/ledmatrix.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-102\" src=\"https:\/\/www.newioit.com.au\/blog\/wp-content\/uploads\/2013\/09\/ledmatrix.jpg\" alt=\"ledmatrix\" width=\"491\" height=\"311\" srcset=\"https:\/\/www.newioit.com.au\/blog\/wp-content\/uploads\/2013\/09\/ledmatrix.jpg 491w, https:\/\/www.newioit.com.au\/blog\/wp-content\/uploads\/2013\/09\/ledmatrix-300x190.jpg 300w\" sizes=\"auto, (max-width: 491px) 100vw, 491px\" \/><\/a><\/p>\n<p><strong>Arduino Mega: <\/strong>The back and front of the Arduino Mega. Amazing value at $10, considering the retail price of the non-clone is like $70. The only difference i could tell is the back of the PCB looks to say &#8216;ROARD&#8217; instead of &#8216;BOARD&#8217;.<\/p>\n<p><a href=\"https:\/\/www.newioit.com.au\/blog\/wp-content\/uploads\/2013\/09\/mega_front.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-104\" src=\"https:\/\/www.newioit.com.au\/blog\/wp-content\/uploads\/2013\/09\/mega_front.jpg\" alt=\"mega_front\" width=\"840\" height=\"419\" srcset=\"https:\/\/www.newioit.com.au\/blog\/wp-content\/uploads\/2013\/09\/mega_front.jpg 840w, https:\/\/www.newioit.com.au\/blog\/wp-content\/uploads\/2013\/09\/mega_front-300x149.jpg 300w, https:\/\/www.newioit.com.au\/blog\/wp-content\/uploads\/2013\/09\/mega_front-624x311.jpg 624w\" sizes=\"auto, (max-width: 840px) 100vw, 840px\" \/><\/a><a href=\"https:\/\/www.newioit.com.au\/blog\/wp-content\/uploads\/2013\/09\/mega_reverse.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-105\" src=\"https:\/\/www.newioit.com.au\/blog\/wp-content\/uploads\/2013\/09\/mega_reverse.jpg\" alt=\"mega_reverse\" width=\"914\" height=\"462\" srcset=\"https:\/\/www.newioit.com.au\/blog\/wp-content\/uploads\/2013\/09\/mega_reverse.jpg 914w, https:\/\/www.newioit.com.au\/blog\/wp-content\/uploads\/2013\/09\/mega_reverse-300x151.jpg 300w, https:\/\/www.newioit.com.au\/blog\/wp-content\/uploads\/2013\/09\/mega_reverse-624x315.jpg 624w\" sizes=\"auto, (max-width: 914px) 100vw, 914px\" \/><\/a><\/p>\n<p><strong>&#8216;Saleae&#8217; Logic Anazlyer: <\/strong>A $10 rip off of the <a title=\"Saleae\" href=\"http:\/\/www.saleae.com\/\">Saleae<\/a> system. Looks nothing like the quite refined Saleae logic, but does the job quite well. Uses the official software form Saleae. Read somewhere that this can be reconfigured to use the USBee software also, but havn&#8217;t had a chance to look at that yet.<\/p>\n<p><a href=\"https:\/\/www.newioit.com.au\/blog\/wp-content\/uploads\/2013\/09\/saleae_clone03.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-108\" src=\"https:\/\/www.newioit.com.au\/blog\/wp-content\/uploads\/2013\/09\/saleae_clone03.jpg\" alt=\"saleae_clone03\" width=\"912\" height=\"549\" srcset=\"https:\/\/www.newioit.com.au\/blog\/wp-content\/uploads\/2013\/09\/saleae_clone03.jpg 912w, https:\/\/www.newioit.com.au\/blog\/wp-content\/uploads\/2013\/09\/saleae_clone03-300x180.jpg 300w, https:\/\/www.newioit.com.au\/blog\/wp-content\/uploads\/2013\/09\/saleae_clone03-624x375.jpg 624w\" sizes=\"auto, (max-width: 912px) 100vw, 912px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/www.newioit.com.au\/blog\/wp-content\/uploads\/2013\/09\/saleae_clone02.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-107\" src=\"https:\/\/www.newioit.com.au\/blog\/wp-content\/uploads\/2013\/09\/saleae_clone02.jpg\" alt=\"saleae_clone02\" width=\"761\" height=\"321\" srcset=\"https:\/\/www.newioit.com.au\/blog\/wp-content\/uploads\/2013\/09\/saleae_clone02.jpg 761w, https:\/\/www.newioit.com.au\/blog\/wp-content\/uploads\/2013\/09\/saleae_clone02-300x126.jpg 300w, https:\/\/www.newioit.com.au\/blog\/wp-content\/uploads\/2013\/09\/saleae_clone02-624x263.jpg 624w\" sizes=\"auto, (max-width: 761px) 100vw, 761px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>Here is a screen shot showing the analyzer in use, channel description:<\/p>\n<ol>\n<li>MAX7219 Clock line<\/li>\n<li>MAX7219 Load line<\/li>\n<li>MAX7219 Data line<\/li>\n<li>Custom line to show pushing out an entire 8&#215;8 led update<\/li>\n<\/ol>\n<p><a href=\"https:\/\/www.newioit.com.au\/blog\/wp-content\/uploads\/2013\/09\/saleae_logic.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-109\" src=\"https:\/\/www.newioit.com.au\/blog\/wp-content\/uploads\/2013\/09\/saleae_logic.png\" alt=\"saleae_logic\" width=\"1393\" height=\"899\" srcset=\"https:\/\/www.newioit.com.au\/blog\/wp-content\/uploads\/2013\/09\/saleae_logic.png 1393w, https:\/\/www.newioit.com.au\/blog\/wp-content\/uploads\/2013\/09\/saleae_logic-300x193.png 300w, https:\/\/www.newioit.com.au\/blog\/wp-content\/uploads\/2013\/09\/saleae_logic-1024x660.png 1024w, https:\/\/www.newioit.com.au\/blog\/wp-content\/uploads\/2013\/09\/saleae_logic-624x402.png 624w\" sizes=\"auto, (max-width: 1393px) 100vw, 1393px\" \/><\/a><\/p>\n<p>Here is the video showing a quick demostration of using the MAX7219 kit and the Arduino Mega<\/p>\n<p>httpv:\/\/www.youtube.com\/watch?v=YOjP6t8D-eY<\/p>\n<p>&nbsp;<\/p>\n<p>And finally, the code for doing this. The MAX7219 code has been ripped from the arduino.cc website. The rotary code is my own, which you can read more about in this post <a title=\"Examples \u2013 Rotary Encoders using PCINT\" href=\"https:\/\/www.newioit.com.au\/blog\/archives\/56\" target=\"_blank\">here<\/a>. This will also work on a standard Arduino (clones available on the net form $8!)<\/p>\n<p>You can also find the lastest version of this code on my SVN repo <a title=\"max7219_matrix \" href=\"https:\/\/www.newioit.com.au\/websvn\/filedetails.php?repname=group.electronics&amp;path=%2FEbayCheapies%2Fmax7219_matrix%2Fmax7219_matrix.ino\" target=\"_blank\">here<\/a>.<\/p>\n<p><strong>** RECOMMEND using the above WebSVN link until i sort out my Syntax Highligter<\/strong><\/p>\n<pre lang=\"c\" line=\"1\" escaped=\"true\">#include <Encoder.h>\r\n\r\n\/\/ Delays for certain events in ms\r\nconst int ROTARY_DELAY = 100;    \/\/ Time between checking for rotary turns\r\nconst int BUTTON_DELAY = 800;    \/\/ Button debouncing, and repeating\r\nconst int FLASH_DELAY = 600;     \/\/ How long to flash the entire row after button press\r\nconst int CURSOR_DELAY = 200;    \/\/ How quickly to flash the cursor led\r\n\r\n\/\/ Pin Assignments\r\nuint8_t rota = 2;\r\nuint8_t rotb = 3;\r\nuint8_t button = 4;\r\nuint8_t dataIn = 5;\r\nuint8_t load = 6;\r\nuint8_t clock = 7;\r\nuint8_t trigger = 12;\r\n \r\nint maxInUse = 1; \r\nuint8_t x = 0;\r\n \r\nbyte max7219_reg_noop        = 0x00;\r\nbyte max7219_reg_digit0      = 0x01;\r\nbyte max7219_reg_digit1      = 0x02;\r\nbyte max7219_reg_digit2      = 0x03;\r\nbyte max7219_reg_digit3      = 0x04;\r\nbyte max7219_reg_digit4      = 0x05;\r\nbyte max7219_reg_digit5      = 0x06;\r\nbyte max7219_reg_digit6      = 0x07;\r\nbyte max7219_reg_digit7      = 0x08;\r\nbyte max7219_reg_decodeMode  = 0x09;\r\nbyte max7219_reg_intensity   = 0x0a;\r\nbyte max7219_reg_scanLimit   = 0x0b;\r\nbyte max7219_reg_shutdown    = 0x0c;\r\nbyte max7219_reg_displayTest = 0x0f;\r\n\r\n\r\nEncoder knobLeft(rota, rotb);\r\n\r\n\r\nchar id=0;\r\n \r\nvoid putByte(byte data) {\r\n  byte i = 8;\r\n  byte mask;\r\n  while(i > 0) {\r\n    mask = 0x01 << (i - 1);      \/\/ get bitmask\r\n    digitalWrite( clock, LOW);   \/\/ tick\r\n    if (data &#038; mask){            \/\/ choose bit\r\n      digitalWrite(dataIn, HIGH);\/\/ send 1\r\n    }else{\r\n      digitalWrite(dataIn, LOW); \/\/ send 0\r\n    }\r\n    digitalWrite(clock, HIGH);   \/\/ tock\r\n    --i;                         \/\/ move to lesser bit\r\n  }\r\n}\r\n \r\nvoid maxSingle( byte reg, byte col) {    \r\n\/\/maxSingle is the \"easy\"  function to use for a     \/\/single max7219\r\n \r\n  digitalWrite(load, LOW);       \/\/ begin    \r\n  putByte(reg);                  \/\/ specify register\r\n  putByte(col);\/\/((data &#038; 0x01) * 256) + data >> 1); \/\/ put data  \r\n  digitalWrite(load, LOW);       \/\/ and load da shit\r\n  digitalWrite(load,HIGH);\r\n}\r\n \r\nvoid maxAll (byte reg, byte col) {    \/\/ initialize  all  MAX7219's in the system\r\n  int c = 0;\r\n  digitalWrite(load, LOW);  \/\/ begin    \r\n  for ( c =1; c<= maxInUse; c++) {\r\n  putByte(reg);  \/\/ specify register\r\n  putByte(col);\/\/((data &#038; 0x01) * 256) + data >> 1); \/\/ put data\r\n    }\r\n  digitalWrite(load, LOW);\r\n  digitalWrite(load,HIGH);\r\n}\r\n \r\nvoid maxOne(byte maxNr, byte reg, byte col) {    \r\n\/\/maxOne is for adressing different MAX7219's,\r\n\/\/whilele having a couple of them cascaded\r\n \r\n  int c = 0;\r\n  digitalWrite(load, LOW);  \/\/ begin    \r\n \r\n  for ( c = maxInUse; c > maxNr; c--) {\r\n    putByte(0);    \/\/ means no operation\r\n    putByte(0);    \/\/ means no operation\r\n  }\r\n \r\n  putByte(reg);  \/\/ specify register\r\n  putByte(col);\/\/((data & 0x01) * 256) + data >> 1); \/\/ put data\r\n \r\n  for ( c =maxNr-1; c >= 1; c--) {\r\n    putByte(0);    \/\/ means no operation\r\n    putByte(0);    \/\/ means no operation\r\n  }\r\n \r\n  digitalWrite(load, LOW); \/\/ and load da shit\r\n  digitalWrite(load,HIGH);\r\n}\r\n \r\n \r\nvoid setup () {\r\n  pinMode(dataIn, OUTPUT);\r\n  pinMode(clock,  OUTPUT);\r\n  pinMode(load,   OUTPUT);\r\n  pinMode(button, INPUT);\r\n  pinMode(trigger, OUTPUT);\r\n  digitalWrite(button, HIGH);\r\n  digitalWrite(13, LOW);  \r\n  digitalWrite(trigger, LOW);\r\n \r\n  \/\/initiation of the max 7219\r\n  maxAll(max7219_reg_scanLimit, 0x07);      \r\n  maxAll(max7219_reg_decodeMode, 0x00);  \/\/ using an led matrix (not digits)\r\n  maxAll(max7219_reg_shutdown, 0x01);    \/\/ not in shutdown mode\r\n  maxAll(max7219_reg_displayTest, 0x00); \/\/ no display test\r\n  for (x=1; x<=8; x++) {    \/\/ empty registers, turn all LEDs off\r\n    maxAll(x,0);\r\n  }\r\n  maxAll(max7219_reg_intensity, 0x01 &#038; 0x0f);  \/\/ the first 0x0f is the value you can set range: 0x00 to 0x0f\r\n  \r\n}  \r\n\r\n\/\/ Value to display a single LED on a row\r\nuint8_t values[9] = {0, 1, 2, 4, 8, 16, 32, 64, 128};\r\n\/\/ Array with all rows, indicating which 'value' to show\r\nuint8_t line[8] = {0, 0, 0, 0, 0, 0, 0, 0}; \r\n\/\/ What row we are current working with\r\nuint8_t idx = 0;\r\n\/\/ Our delay counters (in ms)\r\nint delayButton = 0;\r\nint delayRotary = ROTARY_DELAY;\r\nint delayFlash = 0;\r\nint delayCursor = CURSOR_DELAY;\r\nint cursorState = 0;\r\n\r\n\/\/ Rotary position, reset after each loop\r\nlong pos = 0;\r\n\/\/ Do we update the matrix this run?\r\nuint8_t update = 1;\r\n\r\nvoid loop () { \r\n\r\n  \/\/ Flash the row after a button press\r\n  if (delayFlash &#038;&#038; idx != 8) {\r\n    maxSingle(idx+1, 255);\r\n    \r\n    if (delayFlash == 1) {\r\n      maxSingle(idx+1, values[line[idx+1]]);\r\n    }\r\n    \r\n    delayFlash--;\r\n    if (delayButton) delayButton--;\r\n    \r\n    return;\r\n  } \r\n  \r\n  \/\/ Check for button press\r\n  if (delayButton) delayButton--;\r\n  if (!digitalRead(4) &#038;&#038; delayButton == 0) { \r\n    if (idx == 8) \r\n      idx = 0;\r\n     else\r\n       idx++;\r\n     delayButton = BUTTON_DELAY;\r\n     delayFlash = FLASH_DELAY;\r\n  }\r\n  \r\n  \r\n  \/\/ Checks for rotary turning, and update\r\n  \/\/  the matrix buffer\r\n  \/\/ My rotary encoder had '2 turns' per detent, making\r\n  \/\/  it awkward to move a single position. This is why\r\n  \/\/  the check is for > 1 and < -1 to make it a full detent.\r\n  if (delayRotary) delayRotary--;\r\n  if (delayRotary == 0 &#038;&#038; idx != 8) {\r\n    pos = knobLeft.read();\r\n    if (pos > 1) {\r\n      if (line[idx] != 0)\r\n        line[idx]--;\r\n      else\r\n        line[idx] = 8;\r\n      update = 1;\r\n    } else if (pos < -1) {\r\n      if (line[idx] != 8)\r\n        line[idx]++;\r\n      else\r\n        line[idx]= 0;\r\n      update = 1;\r\n    }\r\n    \r\n    if (update) { \r\n      knobLeft.write(0);\r\n      delayRotary = ROTARY_DELAY;\r\n      \r\n      if (pos) {\r\n        cursorState = 1;\r\n        delayCursor = CURSOR_DELAY;\r\n      }\r\n    }\r\n  }\r\n  \r\n  \r\n  \/\/ Toggle the state of the cursor after the\r\n  \/\/  cursor delay (in ms) has been reached\r\n  if (delayCursor) delayCursor--; \r\n  if (delayCursor == 0 &#038;&#038; idx != 8) {\r\n    delayCursor = CURSOR_DELAY;\r\n    cursorState = 1 - cursorState;\r\n    update = 1; \r\n  }\r\n  \r\n  \/\/ Update the matrix only if a change has been made\r\n  if (update) {\r\n    for (x = 0; x<8; x++) {\r\n       if (x == idx) {\r\n         if (cursorState &#038;&#038; line[x] != 0)\r\n             maxSingle(x+1, values[line[x]]);\r\n         else\r\n             maxSingle(x+1, 0);\r\n       } else {\r\n         if (line[x] == 0) continue;\r\n         digitalWrite(trigger, HIGH);\r\n         maxSingle(x+1, values[line[x]]);\r\n         digitalWrite(trigger, LOW);\r\n       }\r\n    }\r\n    update = 0;\r\n  }\r\n  \r\n  delay(1);\r\n \r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u00a0Ebay Cheapies 001 A couple of my ebay cheap buys have arrived. I have an Arduino Mega for $12, and a 8&#215;8 LED Matrix with a (fake) MAX7219 driver for $3. To test them out, i&#8217;ve hooked them both up, and wrote some Arduino code to show what they can do. I usually don&#8217;t use [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,2,6],"tags":[],"class_list":["post-91","post","type-post","status-publish","format-standard","hentry","category-ebaycheapies","category-electronics","category-examples"],"_links":{"self":[{"href":"https:\/\/www.newioit.com.au\/blog\/wp-json\/wp\/v2\/posts\/91","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.newioit.com.au\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.newioit.com.au\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.newioit.com.au\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.newioit.com.au\/blog\/wp-json\/wp\/v2\/comments?post=91"}],"version-history":[{"count":13,"href":"https:\/\/www.newioit.com.au\/blog\/wp-json\/wp\/v2\/posts\/91\/revisions"}],"predecessor-version":[{"id":152,"href":"https:\/\/www.newioit.com.au\/blog\/wp-json\/wp\/v2\/posts\/91\/revisions\/152"}],"wp:attachment":[{"href":"https:\/\/www.newioit.com.au\/blog\/wp-json\/wp\/v2\/media?parent=91"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.newioit.com.au\/blog\/wp-json\/wp\/v2\/categories?post=91"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.newioit.com.au\/blog\/wp-json\/wp\/v2\/tags?post=91"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}