{"id":540,"date":"2009-05-28T02:10:08","date_gmt":"2009-05-28T07:10:08","guid":{"rendered":"http:\/\/www.tigoe.net\/pcomp\/code\/?p=540"},"modified":"2009-05-31T02:52:51","modified_gmt":"2009-05-31T07:52:51","slug":"cd4067-multiplexer-used-to-control-leds","status":"publish","type":"post","link":"https:\/\/www.tigoe.com\/pcomp\/code\/arduinowiring\/540\/","title":{"rendered":"CD4067 Multiplexer used to control LEDs"},"content":{"rendered":"<p>This tutorial will show how to control multiple LED outputs from a microcontroller using a CD4067\u00a0 analog multiplexer.<\/p>\n<p>This is a stub. More explanation will follow, but for now, here are schematics and code for Arduino.<\/p>\n<p>Parts you&#8217;ll need:<\/p>\n<ul>\n<li>CD4067B multiplexer<\/li>\n<li>Arduino microcontroller<\/li>\n<li>LEDs<\/li>\n<\/ul>\n<p><!--more--><\/p>\n<h2>The Circuit<\/h2>\n<table border=\"0\">\n<tbody>\n<tr>\n<td>\n<p><figure id=\"attachment_543\" aria-describedby=\"caption-attachment-543\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/tigoe.com\/pcomp\/code\/wp-content\/uploads\/2009\/05\/cd4967_photo_1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-543\" title=\"cd4967_photo_1\" src=\"https:\/\/tigoe.com\/pcomp\/code\/wp-content\/uploads\/2009\/05\/cd4967_photo_1-300x199.jpg\" alt=\"CD4067B attached to an Arduino microcontroller\" width=\"300\" height=\"199\" srcset=\"https:\/\/www.tigoe.com\/pcomp\/code\/wp-content\/uploads\/2009\/05\/cd4967_photo_1-300x199.jpg 300w, https:\/\/www.tigoe.com\/pcomp\/code\/wp-content\/uploads\/2009\/05\/cd4967_photo_1.jpg 800w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-543\" class=\"wp-caption-text\">CD4067B attached to an Arduino microcontroller<\/figcaption><\/figure><\/td>\n<td>\n<p><figure id=\"attachment_542\" aria-describedby=\"caption-attachment-542\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/tigoe.com\/pcomp\/code\/wp-content\/uploads\/2009\/05\/cd4047_photo_2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-542\" title=\"cd4047_photo_2\" src=\"https:\/\/tigoe.com\/pcomp\/code\/wp-content\/uploads\/2009\/05\/cd4047_photo_2-300x199.jpg\" alt=\"The anodes of the LEDs connect to the multiplexer's outputs\" width=\"300\" height=\"199\" srcset=\"https:\/\/www.tigoe.com\/pcomp\/code\/wp-content\/uploads\/2009\/05\/cd4047_photo_2-300x199.jpg 300w, https:\/\/www.tigoe.com\/pcomp\/code\/wp-content\/uploads\/2009\/05\/cd4047_photo_2.jpg 800w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-542\" class=\"wp-caption-text\">The anodes of the LEDs connect to the multiplexer&#39;s outputs<\/figcaption><\/figure><\/td>\n<\/tr>\n<tr>\n<td colspan=\"2\">\n<p><figure id=\"attachment_544\" aria-describedby=\"caption-attachment-544\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/tigoe.com\/pcomp\/code\/wp-content\/uploads\/2009\/05\/cd4067.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-544\" title=\"cd4067\" src=\"https:\/\/tigoe.com\/pcomp\/code\/wp-content\/uploads\/2009\/05\/cd4067-300x164.png\" alt=\"The schematic\" width=\"300\" height=\"164\" srcset=\"https:\/\/www.tigoe.com\/pcomp\/code\/wp-content\/uploads\/2009\/05\/cd4067-300x164.png 300w, https:\/\/www.tigoe.com\/pcomp\/code\/wp-content\/uploads\/2009\/05\/cd4067-1024x561.png 1024w, https:\/\/www.tigoe.com\/pcomp\/code\/wp-content\/uploads\/2009\/05\/cd4067.png 1204w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-544\" class=\"wp-caption-text\">The schematic<\/figcaption><\/figure><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>The code<\/h2>\n<pre><span style=\"color: #777755;\">\/*<\/span>\r\n\r\n<span style=\"color: #777755;\"> Multiplexer output example<\/span>\r\n<span style=\"color: #777755;\"> Controls 16 outputs, one at a time, using a CD4067B multiplexer.<\/span>\r\n<span style=\"color: #777755;\"> <\/span>\r\n<span style=\"color: #777755;\"> This sketch loops over the 16 channels of a CD4067 multiplexer, switching<\/span>\r\n<span style=\"color: #777755;\"> the input to each output channel in turn.  It then fades up and down the <\/span>\r\n<span style=\"color: #777755;\"> input channel using analogWrite().<\/span>\r\n<span style=\"color: #777755;\"> <\/span>\r\n<span style=\"color: #777755;\"> The circuit:<\/span>\r\n<span style=\"color: #777755;\"> * CD4067 multiplexer attached as follows:<\/span>\r\n<span style=\"color: #777755;\"> - address pin A: digital I\/O 2<\/span>\r\n<span style=\"color: #777755;\"> - address pin B: digital I\/O 3<\/span>\r\n<span style=\"color: #777755;\"> - address pin C: digital I\/O 4<\/span>\r\n<span style=\"color: #777755;\"> - address pin D: digital I\/O 5<\/span>\r\n<span style=\"color: #777755;\"> - input pin: digital I\/O pin 6<\/span>\r\n<span style=\"color: #777755;\"> - LEDs attached from each of the CD4067's output channels<\/span>\r\n<span style=\"color: #777755;\"> to ground<\/span>\r\n<span style=\"color: #777755;\"> <\/span>\r\n<span style=\"color: #777755;\"> created 21 May 2009<\/span>\r\n<span style=\"color: #777755;\"> by Tom Igoe<\/span>\r\n<span style=\"color: #777755;\"> <\/span>\r\n<span style=\"color: #777755;\"> https:\/\/tigoe.com\/pcomp\/code\/category\/arduinowiring\/540<\/span> for more\r\n<span style=\"color: #777755;\"> <\/span>\r\n<span style=\"color: #777755;\"> *\/<\/span>\r\n\r\n<span style=\"color: #777755;\">\/\/ put the address pin numbers in an array<\/span>\r\n<span style=\"color: #777755;\">\/\/ so they're easier to iterate over:<\/span>\r\nconst <span style=\"color: #996600;\">int<\/span> channel[] = {\r\n  2, 3, 4, 5};\r\n\r\n<span style=\"color: #777755;\">\/\/ the output pin channel (mux's input):<\/span>\r\nconst <span style=\"color: #996600;\">int<\/span> outputPin = 6;\r\n\r\n<span style=\"color: #CC6600;\">void<\/span> <span style=\"color: #993300;\"><strong>setup<\/strong><\/span>() {\r\n  <span style=\"color: #777755;\">\/\/ set up all pins as output:<\/span>\r\n  <span style=\"color: #CC6600;\">for<\/span> (<span style=\"color: #996600;\">int<\/span> thisPin = 2; thisPin &lt; 7; thisPin++) {\r\n    <span style=\"color: #996600;\">pinMode<\/span>(thisPin, <span style=\"color: #CC0000;\">OUTPUT<\/span>);\r\n  }\r\n}\r\n\r\n<span style=\"color: #CC6600;\">void<\/span> <span style=\"color: #993300;\"><strong>loop<\/strong><\/span>() {\r\n  <span style=\"color: #777755;\">\/\/ iterate over the 16 channels of the multiplexer:<\/span>\r\n  <span style=\"color: #CC6600;\">for<\/span> (<span style=\"color: #996600;\">int<\/span> thisChannel = 0; thisChannel &lt; 16; thisChannel++) {\r\n    <span style=\"color: #777755;\">\/\/ set the channel pins based on the channel you want:<\/span>\r\n    muxWrite(thisChannel);\r\n\r\n    <span style=\"color: #777755;\">\/\/ fade the current channel up:<\/span>\r\n    <span style=\"color: #CC6600;\">for<\/span> (<span style=\"color: #996600;\">int<\/span> brightness = 0; brightness &lt; 256; brightness++) {\r\n      <span style=\"color: #996600;\">analogWrite<\/span>(outputPin, brightness);\r\n      <span style=\"color: #996600;\">delay<\/span>(5);\r\n    }\r\n  }\r\n}\r\n\r\n<span style=\"color: #CC6600;\">void<\/span> muxWrite(<span style=\"color: #996600;\">int<\/span> whichChannel) {\r\n  <span style=\"color: #777755;\">\/\/ iterate over the number of pins you're using:<\/span>\r\n  <span style=\"color: #CC6600;\">for<\/span> (<span style=\"color: #996600;\">int<\/span> thisPin = 0; thisPin &lt; 4; thisPin++) {\r\n    <span style=\"color: #777755;\">\/\/ calculate the state of this pin based on <\/span>\r\n    <span style=\"color: #777755;\">\/\/ its bit value in whichChannel:<\/span>\r\n    <span style=\"color: #996600;\">int<\/span> pinState = bitRead(whichChannel, thisPin);\r\n    <span style=\"color: #777755;\">\/\/ turn the pin on or off:<\/span>\r\n    <span style=\"color: #996600;\">digitalWrite<\/span>(channel[thisPin],pinState);\r\n  }\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial will show how to control multiple LED outputs from a microcontroller using a CD4067\u00a0 analog multiplexer. This is a stub. More explanation will follow, but for now, here are schematics and code for Arduino. Parts you&#8217;ll need: CD4067B multiplexer Arduino microcontroller LEDs<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,24],"tags":[63,59],"class_list":["post-540","post","type-post","status-publish","format-standard","hentry","category-arduinowiring","category-circuits","tag-led","tag-multiplexer"],"_links":{"self":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/540","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/comments?post=540"}],"version-history":[{"count":6,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/540\/revisions"}],"predecessor-version":[{"id":563,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/540\/revisions\/563"}],"wp:attachment":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/media?parent=540"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/categories?post=540"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/tags?post=540"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}