{"id":20276,"date":"2019-02-02T09:30:50","date_gmt":"2019-02-02T09:30:50","guid":{"rendered":"http:\/\/randomnerdtutorials.com\/?p=20276"},"modified":"2019-10-31T10:30:44","modified_gmt":"2019-10-31T10:30:44","slug":"electronics-basics-how-do-rgb-leds-work","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/electronics-basics-how-do-rgb-leds-work\/","title":{"rendered":"How do RGB LEDs work?"},"content":{"rendered":"<p>With an RGB LED you can produce almost any color. How is this possible with just one single LED? In this article you&#8217;ll learn:<\/p>\n<ul>\n<li>How RGB LEDs work<\/li>\n<li>Control an RGB LED with an Arduino<\/li>\n<\/ul>\n<p><!--more--><\/p>\n<p><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-80897 size-full\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/how-rgb-leds-work.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"1200\" height=\"675\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/how-rgb-leds-work.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/how-rgb-leds-work.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/how-rgb-leds-work.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/how-rgb-leds-work.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/p>\n<h2>How do RGB LEDs work?<\/h2>\n<p>An <a href=\"https:\/\/makeradvisor.com\/tools\/5mm-rgb-leds-common-cathode\/\" target=\"_blank\" rel=\"noopener noreferrer\">RGB LED<\/a> is a combination of 3 LEDs in just one package:<\/p>\n<ul>\n<li>1x<span style=\"color: #ff0000;\"><strong>\u00a0R<\/strong><\/span>ed LED<\/li>\n<li>1x\u00a0<span style=\"color: #00ff00;\"><strong>G<\/strong><\/span>reen\u00a0LED<\/li>\n<li>1x<span style=\"color: #0000ff;\"><strong>\u00a0B<\/strong><\/span>lue\u00a0LED<\/li>\n<\/ul>\n<p>You can produce almost any color by combining those three colors. An RGB LED is shown in the following figure:<\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" class=\"aligncenter wp-image-80896\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/rgb-led.png?resize=264%2C239&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"264\" height=\"239\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/rgb-led.png?w=350&amp;quality=100&amp;strip=all&amp;ssl=1 350w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/rgb-led.png?resize=300%2C272&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 264px) 100vw, 264px\" \/><\/p>\n<h2>How to create different colors?<\/h2>\n<p>With an RGB LED you can, of course, produce red, green, and blue light, and by configuring the intensity of each LED, you can produce other colors as well.<\/p>\n<p>For example, to produce purely blue light, you\u2019d set the blue LED to the highest intensity and the green and red LEDs to the lowest intensity. For a white light, you\u2019d set all three LEDs to the highest intensity.<\/p>\n<h3>Mixing colors<\/h3>\n<p>To produce other colors, you can combine the three colors in different intensities. To adjust the intensity of each LED you can use a PWM signal.<\/p>\n<p>Because the LEDs are very close to each other, our eyes see the result of the combination of colors, rather than the three colors individually.<\/p>\n<p>To have an idea on how to combine the colors, take a look at the following chart. This is the simplest color mixing chart, but gives you an idea how it works and how to produce different colors.<\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" class=\"aligncenter wp-image-20284\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/color-mixing.png?resize=276%2C276&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"color mixing\" width=\"276\" height=\"276\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/color-mixing.png?w=2000&amp;quality=100&amp;strip=all&amp;ssl=1 2000w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/color-mixing.png?resize=150%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 150w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/color-mixing.png?resize=300%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/color-mixing.png?resize=768%2C768&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/color-mixing.png?resize=1024%2C1024&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/color-mixing.png?resize=80%2C80&amp;quality=100&amp;strip=all&amp;ssl=1 80w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/color-mixing.png?resize=220%2C220&amp;quality=100&amp;strip=all&amp;ssl=1 220w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/color-mixing.png?resize=100%2C100&amp;quality=100&amp;strip=all&amp;ssl=1 100w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/color-mixing.png?resize=238%2C238&amp;quality=100&amp;strip=all&amp;ssl=1 238w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/color-mixing.png?resize=415%2C415&amp;quality=100&amp;strip=all&amp;ssl=1 415w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/color-mixing.png?resize=487%2C487&amp;quality=100&amp;strip=all&amp;ssl=1 487w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/color-mixing.png?resize=595%2C595&amp;quality=100&amp;strip=all&amp;ssl=1 595w\" sizes=\"(max-width: 276px) 100vw, 276px\" \/><\/p>\n<h2>Common Anode and Common Cathode RGB LEDs<\/h2>\n<p>There are two kinds of RGB LEDs: <em>common anode<\/em> LED and <em>common cathode<\/em> LED. The figure below illustrates a common anode and a common cathode LED.<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-20280\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/rgb-led.png?resize=1200%2C482&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"rgb led\" width=\"1200\" height=\"482\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/rgb-led.png?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/rgb-led.png?resize=150%2C60&amp;quality=100&amp;strip=all&amp;ssl=1 150w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/rgb-led.png?resize=300%2C120&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/rgb-led.png?resize=768%2C308&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/rgb-led.png?resize=1024%2C411&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/rgb-led.png?resize=80%2C32&amp;quality=100&amp;strip=all&amp;ssl=1 80w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/rgb-led.png?resize=220%2C88&amp;quality=100&amp;strip=all&amp;ssl=1 220w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/rgb-led.png?resize=250%2C100&amp;quality=100&amp;strip=all&amp;ssl=1 250w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/rgb-led.png?resize=280%2C112&amp;quality=100&amp;strip=all&amp;ssl=1 280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/rgb-led.png?resize=510%2C205&amp;quality=100&amp;strip=all&amp;ssl=1 510w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/rgb-led.png?resize=750%2C301&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/rgb-led.png?resize=975%2C392&amp;quality=100&amp;strip=all&amp;ssl=1 975w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/rgb-led.png?resize=1190%2C478&amp;quality=100&amp;strip=all&amp;ssl=1 1190w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/p>\n<p>In a common cathode RGB LED, all three LEDs share a negative connection (cathode). In a common anode RGB LED, the three LEDs share a positive connection (anode).<\/p>\n<p>This results in an LED that has 4 pins, one for each LED, and one common cathode or one common anode.<\/p>\n<h3>RGB LED Pins<\/h3>\n<p>RGB LEDs have four leads\u2014one for each LED and another for the common anode or cathode. You can identify each lead by its length, as shown in the following figure.<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-20299\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/rgb-led-pin.png?resize=463%2C341&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"rgb led pin\" width=\"463\" height=\"341\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/rgb-led-pin.png?w=700&amp;quality=100&amp;strip=all&amp;ssl=1 700w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/rgb-led-pin.png?resize=150%2C111&amp;quality=100&amp;strip=all&amp;ssl=1 150w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/rgb-led-pin.png?resize=300%2C221&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/rgb-led-pin.png?resize=80%2C59&amp;quality=100&amp;strip=all&amp;ssl=1 80w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/rgb-led-pin.png?resize=220%2C162&amp;quality=100&amp;strip=all&amp;ssl=1 220w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/rgb-led-pin.png?resize=136%2C100&amp;quality=100&amp;strip=all&amp;ssl=1 136w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/rgb-led-pin.png?resize=203%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 203w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/rgb-led-pin.png?resize=323%2C238&amp;quality=100&amp;strip=all&amp;ssl=1 323w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/rgb-led-pin.png?resize=563%2C415&amp;quality=100&amp;strip=all&amp;ssl=1 563w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/rgb-led-pin.png?resize=661%2C487&amp;quality=100&amp;strip=all&amp;ssl=1 661w\" sizes=\"(max-width: 463px) 100vw, 463px\" \/><\/p>\n<p>With the LED facing you so the anode or cathode (the longest lead) is second from the left, the leads should be in the following order: red, anode or cathode, green, and blue.<\/p>\n<h2>Distinguish between RGB LED common anode and common cathode<\/h2>\n<p>The best way to distinguish between a common cathode and common anode RGB LEDs is using a multimeter.<\/p>\n<p>Put you multimeter is in continuity mode.<\/p>\n<p>Place the red multimeter tip on the longest LED lead. Then, place the black tip on one of the other leads.<\/p>\n<p>If the LED lights up, this means you have a common anode LED.<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-80895\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/common-anode.jpg?resize=750%2C503&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"identify common anode rgb led\" width=\"750\" height=\"503\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/common-anode.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/common-anode.jpg?resize=300%2C201&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/p>\n<p>On the other hand,\u00a0 if you have a common cathode RGB LED, you need to placed the black tip on the longest lead, and the red tip on one of the other leads (see figure below).<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-80894\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/common-cathode.jpg?resize=750%2C500&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"identify common cathode rgb led\" width=\"750\" height=\"500\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/common-cathode.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/common-cathode.jpg?resize=300%2C200&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/p>\n<p>So, to distinguish between common cathode and common anode RGB LEDs:<\/p>\n<ul>\n<li>Use a multimeter in continuity mode<\/li>\n<li>If the LED lights up with the red tip on the longest lead and the black on one of the other leads &#8211; you have a common anode RGB LED<\/li>\n<li>If the LED lights up with the black tip on the longest lead and the red tip on one of the other leads &#8211; you have a common cathode RGB LED.<\/li>\n<\/ul>\n<h2>Control an RGB LED with the Arduino<\/h2>\n<p>In this example, we show you how to control the color of an RGB LED using an Arduino.<\/p>\n<p>The project we&#8217;ll build uses three potentiometers to control the light intensity of each pin (LED) of the RGB LED to produce any color you want.<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-20533\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/DSC08404.jpg?resize=601%2C338&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"DSC08404\" width=\"601\" height=\"338\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/DSC08404.jpg?w=900&amp;quality=100&amp;strip=all&amp;ssl=1 900w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/DSC08404.jpg?resize=150%2C84&amp;quality=100&amp;strip=all&amp;ssl=1 150w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/DSC08404.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/DSC08404.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/DSC08404.jpg?resize=80%2C45&amp;quality=100&amp;strip=all&amp;ssl=1 80w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/DSC08404.jpg?resize=220%2C124&amp;quality=100&amp;strip=all&amp;ssl=1 220w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/DSC08404.jpg?resize=178%2C100&amp;quality=100&amp;strip=all&amp;ssl=1 178w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/DSC08404.jpg?resize=267%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 267w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/DSC08404.jpg?resize=423%2C238&amp;quality=100&amp;strip=all&amp;ssl=1 423w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/DSC08404.jpg?resize=738%2C415&amp;quality=100&amp;strip=all&amp;ssl=1 738w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/DSC08404.jpg?resize=866%2C487&amp;quality=100&amp;strip=all&amp;ssl=1 866w\" sizes=\"(max-width: 601px) 100vw, 601px\" \/><\/p>\n<h3>Parts required<\/h3>\n<p>For this example you need the following parts:<\/p>\n<p><span class=\"rntbox rntclgreen\"><strong>Note: <\/strong> we\u2019ll be using a common anode LED for this project, but if you already have a common cathode LED it\u2019s fine to use that; just watch out for the differences noted in the circuit wiring and code.<\/span><\/p>\n<ul>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/compatible-arduino-uno-r3-board\/\" target=\"_blank\" rel=\"noopener noreferrer\">Arduino UNO<\/a>\u00a0\u2013 read\u00a0<a href=\"https:\/\/makeradvisor.com\/best-arduino-starter-kits\/\" target=\"_blank\" rel=\"noopener noreferrer\">Best Arduino Starter Kits<\/a><\/li>\n<li><a href=\"https:\/\/www.banggood.com\/10pcs-5mm-RGB-LED-Common-Anode-4-Pin-Tri-Color-Emitting-Diodes-F5-p-937446.html?p=MA240439985285201910&amp;cur_warehouse=CN\" target=\"_blank\" rel=\"noopener noreferrer nofollow\">RGB LED common anode<\/a> or <a href=\"https:\/\/makeradvisor.com\/tools\/5mm-rgb-leds-common-cathode\/\" target=\"_blank\" rel=\"noopener noreferrer\">RGB LED common cathode<\/a><\/li>\n<li>3\u00d7\u00a0<a href=\"https:\/\/makeradvisor.com\/tools\/trimpot-kit\/\" target=\"_blank\" rel=\"noopener noreferrer\">1k \u03a9 trimpot<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\" rel=\"noopener noreferrer\">Breadboard\u00a0<\/a><\/li>\n<li>3 \u00d7 <a href=\"https:\/\/makeradvisor.com\/tools\/resistors-kits\/\" target=\"_blank\" rel=\"noopener noreferrer\">220 \u03a9 Resistor<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\" rel=\"noopener noreferrer\">Jumper wires<\/a><\/li>\n<\/ul>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-20534\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/DSC08413.jpg?resize=626%2C377&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"DSC08413\" width=\"626\" height=\"377\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/DSC08413.jpg?w=900&amp;quality=100&amp;strip=all&amp;ssl=1 900w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/DSC08413.jpg?resize=150%2C90&amp;quality=100&amp;strip=all&amp;ssl=1 150w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/DSC08413.jpg?resize=300%2C181&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/DSC08413.jpg?resize=768%2C463&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/DSC08413.jpg?resize=80%2C48&amp;quality=100&amp;strip=all&amp;ssl=1 80w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/DSC08413.jpg?resize=220%2C132&amp;quality=100&amp;strip=all&amp;ssl=1 220w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/DSC08413.jpg?resize=166%2C100&amp;quality=100&amp;strip=all&amp;ssl=1 166w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/DSC08413.jpg?resize=249%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 249w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/DSC08413.jpg?resize=395%2C238&amp;quality=100&amp;strip=all&amp;ssl=1 395w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/DSC08413.jpg?resize=689%2C415&amp;quality=100&amp;strip=all&amp;ssl=1 689w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/DSC08413.jpg?resize=809%2C487&amp;quality=100&amp;strip=all&amp;ssl=1 809w\" sizes=\"(max-width: 626px) 100vw, 626px\" \/><\/p>\n<p>You can use the preceding links or go directly to <a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\">MakerAdvisor.com\/tools<\/a> to find all the parts for your projects at the best price!<\/p><p style=\"text-align:center;\"><a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/header-200.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\"><\/a><\/p>\n<h2>Schematic Diagram<\/h2>\n<p>Follow the next schematic diagram to wire the circuit:<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-20525\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/rgb-led_potentiometer.png?resize=1200%2C1259&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"rgb led_potentiometer\" width=\"1200\" height=\"1259\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/rgb-led_potentiometer.png?w=1292&amp;quality=100&amp;strip=all&amp;ssl=1 1292w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/rgb-led_potentiometer.png?resize=143%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 143w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/rgb-led_potentiometer.png?resize=286%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 286w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/rgb-led_potentiometer.png?resize=768%2C806&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/rgb-led_potentiometer.png?resize=976%2C1024&amp;quality=100&amp;strip=all&amp;ssl=1 976w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/rgb-led_potentiometer.png?resize=76%2C80&amp;quality=100&amp;strip=all&amp;ssl=1 76w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/rgb-led_potentiometer.png?resize=210%2C220&amp;quality=100&amp;strip=all&amp;ssl=1 210w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/rgb-led_potentiometer.png?resize=95%2C100&amp;quality=100&amp;strip=all&amp;ssl=1 95w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/rgb-led_potentiometer.png?resize=227%2C238&amp;quality=100&amp;strip=all&amp;ssl=1 227w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/rgb-led_potentiometer.png?resize=395%2C415&amp;quality=100&amp;strip=all&amp;ssl=1 395w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/rgb-led_potentiometer.png?resize=464%2C487&amp;quality=100&amp;strip=all&amp;ssl=1 464w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/rgb-led_potentiometer.png?resize=567%2C595&amp;quality=100&amp;strip=all&amp;ssl=1 567w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/p>\n<p><span class=\"rntbox rntcred\"><strong>Important: <\/strong> if you&#8217;re using an common cathode RGB LED , you need to connect the longer lead to GND instead of 5V.<\/span><\/p>\n<h2>Code<\/h2>\n<p>Upload the following sketch to your Arduino board:<\/p>\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*\n \n All the resources for this project:\n http:\/\/randomnerdtutorials.com\/\n \n*\/\n\nint redPin = 3;     \/\/ Red RGB pin -&gt; D3\nint greenPin = 5;   \/\/ Green RGB pin -&gt; D5\nint bluePin = 6;    \/\/ Blue RGB pin -&gt; D6\n\nint potRed = A0;     \/\/ Potentiometer controls Red pin -&gt; A0\nint potGreen = A1;   \/\/ Potentiometer controls Green pin -&gt; A1\nint potBlue = A2;    \/\/ Potentiometer controls Blue pin -&gt; A2\n\nvoid setup() {\n  pinMode(redPin,OUTPUT);\n  pinMode(bluePin,OUTPUT);\n  pinMode(greenPin, OUTPUT);\n  \n  pinMode(potRed, INPUT); \n  pinMode(potGreen, INPUT); \n  pinMode(potBlue, INPUT); \n}\n\nvoid loop() {\n  \/\/ Reads the current position of the potentiometer and converts \n  \/\/ to a value between 0 and 255 to control the according RGB pin with PWM\n  \/\/ RGB LED COMMON ANODE\n  analogWrite(redPin, 255-(255.\/1023.)*analogRead(potRed));\n  analogWrite(greenPin, 255-(255.\/1023.)*analogRead(potGreen));\n  analogWrite(bluePin, 255-(255.\/1023.)*analogRead(potBlue));\n \n  \/\/ Uncomment for RGB LED COMMON CATHODE\n  \/*\n  analogWrite(redPin, (255.\/1023.)*analogRead(potRed));\n  analogWrite(greenPin, (255.\/1023.)*analogRead(potGreen));\n  analogWrite(bluePin, (255.\/1023.)*analogRead(potBlue));\n  *\/\n  \n  delay(10);\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/How_do_RGB_LEDs_work.ino\" target=\"_blank\">View raw code<\/a><\/p>\n<p><span class=\"rntbox rntcred\"><strong>Important: <\/strong> if you&#8217;re using an RGB LED common cathode, you need to comment and uncomment some code in the <span class=\"rnthl rntliteral\">loop()<\/span><\/span><\/p>\n<h2>How the Code Works<\/h2>\n<p>The code to control an RGB LED is very straightforward. In our sketch, we start by defining three integer variables called <span class=\"rnthl rntliteral\">redPin<\/span>, <span class=\"rnthl rntliteral\">greenPin<\/span> and <span class=\"rnthl rntliteral\">bluePin<\/span> that refer to the pins that the LED leads are connected to:<\/p>\n<pre><code class=\"language-c\">int redPin = 3; \/\/ Red RGB pin -&gt; D3\nint greenPin = 5; \/\/ Green RGB pin -&gt; D5\nint bluePin = 6; \/\/ Blue RGB pin -&gt; D6<\/code><\/pre>\n<p>Then, we also declare variables to refer to the potentiometers. The <span class=\"rnthl rntliteral\">potRed<\/span> will control the red lead, and so on.<\/p>\n<pre><code class=\"language-c\">int potRed = A0; \/\/ Potentiometer controls Red pin -&gt; A0\nint potGreen = A1; \/\/ Potentiometer controls Green pin -&gt; A1\nint potBlue = A2; \/\/ Potentiometer controls Blue pin -&gt; A2<\/code><\/pre>\n<p>The potentiometers should be connected to the Arduino analog pins, because we want to read an analog value from the pots.<\/p>\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span> function, you set the LED pins as outputs:<\/p>\n<pre><code class=\"language-c\">pinMode(redPin, OUTPUT);\npinMode(bluePin, OUTPUT);\npinMode(greenPin, OUTPUT);<\/code><\/pre>\n<p>And the potentiometers as inputs:<\/p>\n<pre><code class=\"language-c\">pinMode(potRed, INPUT);\npinMode(potGreen, INPUT);\npinMode(potBlue, INPUT);<\/code><\/pre>\n<p>In the <span class=\"rnthl rntliteral\">loop()<\/span>, we read the value from the potentiometers and control each LED of the RGB LED accordingly.<\/p>\n<p>To read the value from a potentiometer, we need to use the <span class=\"rnthl rntliteral\">analogRead()<\/span> function. For example, to read the value from <span class=\"rnthl rntliteral\">potRed<\/span>:<\/p>\n<pre><code class=\"language-c\">analogRead(potRed)<\/code><\/pre>\n<p>The <span class=\"rnthl rntliteral\">analogRead()<\/span> function returns a value between 0 and 1023.<\/p>\n<p>To send a PWM signal to the LEDs, we use the <span class=\"rnthl rntliteral\">analogWrite()<\/span> function. The <span class=\"rnthl rntliteral\">analogWrite()<\/span> function accepts as arguments the pin we want to control and a value between 0 and 255, in which 0 corresponds to zero brightness, and 255 maximum brightness. However, a common anode RGB LED works the other way around. Sending 0 sets the LED to maximum brightness, and 255 sets to the lowest brightness (off). So, we need to subtract the result to 255.<\/p>\n<p>Because we read a value between 0 and 1023, and need to output a value between 0 and 255, we need to multiply the value read by (255\/1023).<\/p>\n<pre><code class=\"language-c\">analogWrite(redPin,255-(255.\/1023.)*analogRead(potRed));\nanalogWrite(greenPin,255-(255.\/1023.)*analogRead(potGreen));\nanalogWrite(bluePin,255-(255.\/1023.)*analogRead(potBlue));<\/code><\/pre>\n<p>To control the common cathode RGB LED, comment the previous lines and uncomment the next ones.<\/p>\n<pre><code class=\"language-c\">analogWrite(redPin,(255.\/1023.)*analogRead(potRed)); \nanalogWrite(greenPin,(255.\/1023.)*analogRead(potGreen)); \nanalogWrite(bluePin,(255.\/1023.)*analogRead(potBlue));<\/code><\/pre>\n<p>Sending 255 sets the LEDs to the maximum brightness, and 0 sets to the lowest brightness.<\/p>\n<h2>Demonstration<\/h2>\n<p>Watch this quick video demonstration to see this project in action:<\/p>\n<p style=\"text-align:center\"><iframe width=\"720\" height=\"405\" src=\"https:\/\/www.youtube.com\/embed\/x4h9R40umN4?rel=0\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<h2>Wrapping Up<\/h2>\n<p>In summary:<\/p>\n<ul>\n<li>an RGB LED is a combination of three LEDs in just one package: red, green and blue;<\/li>\n<li>there are two kinds of RGB LEDs: common cathode and common anode RGB LEDs;<\/li>\n<li>you generate different colors by adjusting the brightness of each of the three LEDs of the RGB LED;<\/li>\n<li>to adjust the brightness of each LED, you use a PWM signal.<\/li>\n<\/ul>\n<p>I hope you found this guide\u00a0useful. If you like this subject, you may also be interested in the following resources:<\/p>\n<ul>\n<li><a href=\"https:\/\/randomnerdtutorials.com\/guide-for-ws2812b-addressable-rgb-led-strip-with-arduino\/\">Guide for WS2812B Addressable RGB LED Strip with Arduino<\/a><\/li>\n<li><a href=\"https:\/\/randomnerdtutorials.com\/best-led-strip-for-your-project\/\">What\u2019s the Best LED Strip For Your Project?<\/a><\/li>\n<li><a href=\"https:\/\/randomnerdtutorials.com\/node-red-with-ws2812b-addressable-rgb-led-strip\/\">Node-RED with WS2812B Addressable RGB LED Strip<\/a><\/li>\n<li><a href=\"https:\/\/randomnerdtutorials.com\/arduino-step-by-step-projects\/\"><strong>Arduino Step-by-step Projects &#8211; Build 25 Projects (course)<\/strong><\/a><\/li>\n<\/ul>\n<p>If you like this post probably you might like my next ones (<a href=\"https:\/\/randomnerdtutorials.com\/download\" target=\"_blank\" rel=\"noopener noreferrer\">click here to subscribe my blog<\/a>).<\/p>\n<p>Thanks for reading.<\/p>\n<p><em><span style=\"color: #808080;\">February 2, 2019<\/span><\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>With an RGB LED you can produce almost any color. How is this possible with just one single LED? In this article you&#8217;ll learn how RGB LEDs work and how to control an RGB LED with Arduino. <\/p>\n<p class=\"read-more-container\"><a href=\"https:\/\/randomnerdtutorials.com\/electronics-basics-how-do-rgb-leds-work\/\" class=\"read-more button\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":1,"featured_media":80897,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[305,267,256,269,264],"tags":[],"class_list":["post-20276","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-0-other","category-arduino-project","category-electronics","category-guide-project","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/how-rgb-leds-work.jpg?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/20276","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=20276"}],"version-history":[{"count":0,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/20276\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/80897"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=20276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=20276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=20276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}