{"id":72,"date":"2016-07-26T14:12:03","date_gmt":"2016-07-26T19:12:03","guid":{"rendered":"http:\/\/hconde.com\/wp\/?p=72"},"modified":"2020-02-04T23:58:30","modified_gmt":"2020-02-05T05:58:30","slug":"how-to-control-a-12-volts-dc-motor-or-solenoid-from-your-raspberry-pi","status":"publish","type":"post","link":"https:\/\/horazmakes.com\/blog\/2016\/07\/26\/how-to-control-a-12-volts-dc-motor-or-solenoid-from-your-raspberry-pi\/","title":{"rendered":"How to control a 12 volts DC motor (or solenoid) from your Raspberry Pi"},"content":{"rendered":"<p><strong>Introduction<\/strong><\/p>\n<p>Raspberry Pi GPIO pins work at 3.3 volts and the current you can draw from them is quite limited for many applications (16 mA &#8211; 50 mA).<\/p>\n<blockquote><p>An external&nbsp;power source and a power interface are&nbsp;required to control a 12 volts&nbsp;DC motor (or solenoid) from a Raspberry Pi GPIO pin.<\/p><\/blockquote>\n<p><strong>Materials<\/strong><\/p>\n<ul>\n<li>N-Channel MOSFET (FQP30N06L\/RFP30N06LE) x 1<\/li>\n<li>10 k\u03a9 resistor x 1<\/li>\n<li>1N4001 rectifier diode x 1<\/li>\n<\/ul>\n<p><strong>Schematic diagram<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-200\" src=\"https:\/\/horazmakes.com\/blog\/wp-content\/uploads\/2016\/07\/Rpi-control-solenoid_schem.png\" alt=\"\" width=\"1068\" height=\"447\"><\/p>\n<p><strong>Wiring&nbsp;diagram<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-202\" src=\"https:\/\/horazmakes.com\/blog\/wp-content\/uploads\/2016\/07\/Rpi-control-solenoid_bb.png\" alt=\"\" width=\"1617\" height=\"1161\"><\/p>\n<p><strong>Components explanation<\/strong><\/p>\n<p>The power interface between our Raspberry Pi signal pin and the 12 volts&nbsp;DC actuator we&#8217;d like to control is a MOSFET (metal\u2013oxide\u2013semiconductor field-effect transistor).<\/p>\n<p>The 10 k\u03a9 resistor is used as a pull down resistor for the MOSFET to have a well defined gate input level&nbsp;at any time.<\/p>\n<p>FInally, the rectifier diode 1N4001 is for kickback protection (should be placed across solenoids, relays &amp; DC motors to safely discharge the spikes generated by the coils and avoid damaging the Raspberry Pi signal&nbsp;pin)<\/p>\n<p><strong>Python test code<\/strong><\/p>\n<pre class=\"lang:python decode:true \" title=\"control_solenoid.py\">#! \/usr\/bin\/python\n\nimport sys\nimport RPi.GPIO as GPIO\nimport time\n\nGPIO.setmode(GPIO.BCM)\nCTRL_PIN = 4\nGPIO.setup(CTRL_PIN, GPIO.OUT)\n\nprint \"SOLENOID CTRL Test (CTRL+C to exit)\"\ntime.sleep(2)\nprint \"Ready\"\n\ntry:\n  while True:\n    print \"On\"\n    GPIO.output(CTRL_PIN, True)\n    time.sleep(1)\n    print \"Off\"\n    GPIO.output(CTRL_PIN, False)\n    time.sleep(1)\nexcept KeyboardInterrupt:\n  print 'Quit'\n  GPIO.cleanup()\nexcept:\n  print 'Unexpected error: ', sys.exc_info()[0]<\/pre>\n<p>This python program will turn on\/off your motor every second until you exit the program by pressing CTRL + C.<\/p>\n<p>Note that the control GPIO pin is defined in the program as pin number 4 (see the wiring diagram above).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Raspberry Pi GPIO pins work at 3.3 volts and the current you can draw from them is quite limited for many applications (16 mA &#8211; 50 mA). An external&nbsp;power source and a power interface are&nbsp;required to control a 12 volts&nbsp;DC motor (or solenoid) from a Raspberry Pi GPIO pin. Materials N-Channel MOSFET (FQP30N06L\/RFP30N06LE) x [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":202,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,6,8,11,12,13,15],"tags":[21,35,38,40,42,44,51,52,54,59,62,68],"class_list":["post-72","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-actuators","category-electronics","category-iot","category-maker","category-mechanics","category-motors","category-raspberry-pi","tag-actuator","tag-dc","tag-diode","tag-external-power","tag-interface","tag-kickback","tag-mosfet","tag-motor","tag-n-channel","tag-power","tag-raspberry-pi","tag-solenoid"],"_links":{"self":[{"href":"https:\/\/horazmakes.com\/blog\/wp-json\/wp\/v2\/posts\/72","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=72"}],"version-history":[{"count":2,"href":"https:\/\/horazmakes.com\/blog\/wp-json\/wp\/v2\/posts\/72\/revisions"}],"predecessor-version":[{"id":313,"href":"https:\/\/horazmakes.com\/blog\/wp-json\/wp\/v2\/posts\/72\/revisions\/313"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/horazmakes.com\/blog\/wp-json\/wp\/v2\/media\/202"}],"wp:attachment":[{"href":"https:\/\/horazmakes.com\/blog\/wp-json\/wp\/v2\/media?parent=72"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/horazmakes.com\/blog\/wp-json\/wp\/v2\/categories?post=72"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/horazmakes.com\/blog\/wp-json\/wp\/v2\/tags?post=72"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}