{"id":378,"date":"2022-10-02T23:53:55","date_gmt":"2022-10-03T04:53:55","guid":{"rendered":"https:\/\/horazmakes.com\/blog\/?p=378"},"modified":"2023-09-11T23:50:13","modified_gmt":"2023-09-12T05:50:13","slug":"how-to-drive-a-12v-led-strip-with-a-microcontroller","status":"publish","type":"post","link":"https:\/\/horazmakes.com\/blog\/2022\/10\/02\/how-to-drive-a-12v-led-strip-with-a-microcontroller\/","title":{"rendered":"How to drive a 12V LED strip with a microcontroller"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Introduction<\/h3>\n\n\n\n<p>It&#8217;s common task you have to control a 12V LED strip (consuming more than some milliamps) using a 5V\/3.3V signal from a microcontroller.<\/p>\n\n\n\n<p>It is not possible to drive directly from the output pin of a microcontroller. To achieve this goal, we need some basic power interface. We can use a MOSFET.<\/p>\n\n\n\n<p>This post is based on the following discussion in Electronics StackExchange: <a href=\"https:\/\/electronics.stackexchange.com\/questions\/81935\/mosfet-usage-and-p-vs-n-channel\">Mosfet usage and P- vs N-channel<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">N MOSFET circuit (LED strip connected between +12V and Drain)<\/h3>\n\n\n\n<p>When the microcontroller output is HIGH the MOSFET is turned ON, when it is LOW the MOSFET is turned OFF. The series resistor has been made smaller to aid the turn ON, turn OFF times by charging or discharging the gate capacitance more quickly.<\/p>\n\n\n\n<p>I figured out there was no need of an anti-kickback diode across the load, since LED strip would not be an inductive load.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"586\" height=\"466\" src=\"https:\/\/horazmakes.com\/blog\/wp-content\/uploads\/2022\/10\/FZJf6.png\" alt=\"\" class=\"wp-image-385\"\/><\/figure>\n\n\n\n<p>Some N channel MOSFETS we could use for this task are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>P55NF06 (60V 55A)<\/li>\n\n\n\n<li>FQP30N06L (60V 32A)<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Arduino Example<\/h3>\n\n\n\n<p>Once we have connected the N MOSFET according to above diagram, we can use an Arduino PWM signal to control the brightness of a LED strip.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>The Arduino IDE has a built in function \u201canalogWrite()\u201d which can be used to generate a PWM signal. The frequency of this generated signal for most pins will be about 490Hz and we can give the value from 0-255 using this function.<\/p>\n\n\n\n<p>analogWrite(0) means a signal of 0% duty cycle.<\/p>\n\n\n\n<p>analogWrite(127) means a signal of 50% duty cycle.<\/p>\n\n\n\n<p>analogWrite(255) means a signal of 100% duty cycle.<\/p>\n\n\n\n<p>On Arduino Uno, the PWM pins are 3, 5, 6, 9, 10 and 11. The frequency of PWM signal on pins 5 and 6 will be about 980Hz and on other pins will be 490Hz. The PWM pins are labeled with ~ sign.<\/p>\n<\/blockquote>\n\n\n\n<p>See <a href=\"https:\/\/www.arduino.cc\/reference\/en\/language\/functions\/analog-io\/analogwrite\/\">Arduino analogWrite() function<\/a>.<\/p>\n\n\n\n<p>See <a href=\"https:\/\/create.arduino.cc\/projecthub\/muhammad-aqib\/arduino-pwm-tutorial-ae9d71\">Arduino PWM tutorial<\/a>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Arduino code<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/Initializing LED Pin\nint led_pin = 6;\n\nvoid setup() {\n  \/\/Declaring LED pin as output\n  pinMode(led_pin, OUTPUT);\n}\n\nvoid loop() {\n  \/\/Fading the LED\n  for(int i = 0; i &lt; 255; i++) {\n    analogWrite(led_pin, i);\n    delay(5);\n  }\n  for(int i = 255; i > 0; i--) {\n    analogWrite(led_pin, i);\n    delay(5);\n  }\n}<\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction It&#8217;s common task you have to control a 12V LED strip (consuming more than some milliamps) using a 5V\/3.3V signal from a microcontroller. It is not possible to drive directly from the output pin of a microcontroller. To achieve this goal, we need some basic power interface. We can use a MOSFET. This post [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":381,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,5,6,11,86],"tags":[25,147,149,143,145,146,51,59,144,148],"class_list":["post-378","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-actuators","category-arduino","category-electronics","category-maker","category-programming","tag-arduino","tag-fqp30n06l","tag-irfz44n","tag-led","tag-led-strip","tag-microcontroller","tag-mosfet","tag-power","tag-power-interface","tag-rfp30n06le"],"_links":{"self":[{"href":"https:\/\/horazmakes.com\/blog\/wp-json\/wp\/v2\/posts\/378","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/horazmakes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/horazmakes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/horazmakes.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/horazmakes.com\/blog\/wp-json\/wp\/v2\/comments?post=378"}],"version-history":[{"count":7,"href":"https:\/\/horazmakes.com\/blog\/wp-json\/wp\/v2\/posts\/378\/revisions"}],"predecessor-version":[{"id":452,"href":"https:\/\/horazmakes.com\/blog\/wp-json\/wp\/v2\/posts\/378\/revisions\/452"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/horazmakes.com\/blog\/wp-json\/wp\/v2\/media\/381"}],"wp:attachment":[{"href":"https:\/\/horazmakes.com\/blog\/wp-json\/wp\/v2\/media?parent=378"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/horazmakes.com\/blog\/wp-json\/wp\/v2\/categories?post=378"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/horazmakes.com\/blog\/wp-json\/wp\/v2\/tags?post=378"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}