{"id":374,"date":"2023-12-31T21:41:50","date_gmt":"2023-12-31T21:41:50","guid":{"rendered":"https:\/\/www.hardwarehacker.co.uk\/blog\/?p=374"},"modified":"2024-03-14T17:53:27","modified_gmt":"2024-03-14T17:53:27","slug":"driving-a-bldc-motor-from-zero-to-slow-and-janky-in-python","status":"publish","type":"post","link":"https:\/\/www.hardwarehacker.co.uk\/blog\/driving-a-bldc-motor-from-zero-to-slow-and-janky-in-python\/","title":{"rendered":"Driving a BLDC motor from zero to slow-and-janky in Python"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2023\/12\/PXL_20231029_215544980-scaled.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"771\" src=\"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2023\/12\/PXL_20231029_215544980-1024x771.jpg\" alt=\"\" class=\"wp-image-378\" srcset=\"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2023\/12\/PXL_20231029_215544980-1024x771.jpg 1024w, https:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2023\/12\/PXL_20231029_215544980-300x226.jpg 300w, https:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2023\/12\/PXL_20231029_215544980-768x578.jpg 768w, https:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2023\/12\/PXL_20231029_215544980-1536x1157.jpg 1536w, https:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2023\/12\/PXL_20231029_215544980-2048x1542.jpg 2048w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Motor and Pico, ready for battle&#8230;<\/figcaption><\/figure>\n\n\n\n<p>One of <em>my<\/em> personal goals for PiWars this year was to try to build a BLDC motor controller. Mainly because I find that kind of thing a fun challenge&#8230;  <\/p>\n\n\n\n<p>Why is controlling a  <em>BrushLess<\/em> DC motor a challenge?  It has no brushes(!) the brushes in a brushed motor <em>physically<\/em> implement the control algorithm for the coils, energising each coil in the motor in correct sequence in lockstep with the rotation of the motor.  Controlling a brushless motor requires the controller to correctly energise the coils in lockstep with the rotation of the motor.  This gives great flexibility (but also allows for easily burning out a motor!).<\/p>\n\n\n\n<p> Of course, we could buy a controller off the shelf, but the Pico is out and I wanted to use one in anger. It has been done before by the <a href=\"https:\/\/docs.simplefoc.com\/rpi_mcu\">SimpleFOC project<\/a> and my plan B was to &#8220;just use SimpleFOC&#8221; (but there&#8217;s less fun in using a library)!<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\">\n<h2 class=\"wp-block-heading\">Hello world<\/h2>\n<\/blockquote>\n\n\n\n<p>At the most basic level, I had the idea that field-oriented control (FOC) was about driving sine wave signals into the three terminals of the motor at 120-degrees out of phase with each other and then rotating those sine waves through 360 degrees in lock-step.  In turn that rotates the magnetic field smoothly through 360 degrees and motor&#8217;s rotor turns with it.  There&#8217;s a lot more to it than that, but I didn&#8217;t have have a rotary encoder when my motor arrived so I thought &#8220;let&#8217;s just give that a try in MicroPython&#8221;:<\/p>\n\n\n\n<ul>\n<li>Assign 3 GPIO pins to the 3 phases of the motor.<\/li>\n\n\n\n<li>Make them all PWM outputs at say, 20KHz and connect to motor through a (half bridge) motor driver per pin.<\/li>\n\n\n\n<li>Loop, incrementing x each PWM cycle&#8230;\n<ul>\n<li>Set PWM 1 to sin(x)<\/li>\n\n\n\n<li>Set PWM 2 to sin(x+120 degrees)<\/li>\n\n\n\n<li>Set PWM 3 to sin(x+240 degrees)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>And&#8230; after some fiddling (and lots of debug prints to the console) something happened!<\/p>\n\n\n\n<figure class=\"wp-block-video\"><video controls src=\"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2023\/12\/PXL_20231030_213033263.LS_.mp4\"><\/video><\/figure>\n\n\n\n<p>Writing this now that I&#8217;m a bit further on in the project I now know that controlling a motor like this is really bad(TM) because the rotor in the motor &#8220;catches up&#8221; to the magnetic field, which means that the field stops doing as much work on the rotor and it dumps all that electrical energy we&#8217;re feeding it into heat instead :-\/  Still, as a hello world, it felt great!  <\/p>\n\n\n\n<p>If you&#8217;re going to try anything like this and you want to keep the magic smoke inside the motor, you&#8217;ll want a benchtop power supply with current limiter and an oscilloscope!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of my personal goals for PiWars this year was to try to build a BLDC motor controller. Mainly because I find that kind of thing a fun challenge&#8230; Why is controlling a BrushLess DC motor a challenge? It has no brushes(!) the brushes in a brushed motor physically implement the control algorithm for the &hellip; <a href=\"https:\/\/www.hardwarehacker.co.uk\/blog\/driving-a-bldc-motor-from-zero-to-slow-and-janky-in-python\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Driving a BLDC motor from zero to slow-and-janky in Python<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15,9,1],"tags":[20,17,22,2,16,21],"_links":{"self":[{"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/posts\/374"}],"collection":[{"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=374"}],"version-history":[{"count":4,"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/posts\/374\/revisions"}],"predecessor-version":[{"id":381,"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/posts\/374\/revisions\/381"}],"wp:attachment":[{"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=374"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=374"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=374"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}