{"id":277,"date":"2015-01-15T00:06:21","date_gmt":"2015-01-14T16:06:21","guid":{"rendered":"http:\/\/aidilj.com\/?p=277"},"modified":"2015-05-12T21:23:58","modified_gmt":"2015-05-12T13:23:58","slug":"sensored-bldc-motor-control-with-arduino","status":"publish","type":"post","link":"https:\/\/aidilj.com\/worklog\/2015\/01\/sensored-bldc-motor-control-with-arduino\/","title":{"rendered":"Sensored BLDC motor control with Arduino"},"content":{"rendered":"<p>UPDATE : May 12, 2015, I&#8217;ve added schematic after the Arduino sketch below<\/p>\n<p>I couldn&#8217;t find much info on this on the net so I thought to share my experiment for those researching on this.<\/p>\n<p>Hardware as follows<br \/>\nArduino &#8211; Pro Micro 5V<br \/>\nMosfet &#8211; STP75NF75<br \/>\nHalf bridge driver &#8211; IR2103<\/p>\n<p>Three PWM outputs are used in this design to pulse the upper mosfets while the bottom mosfets are either on or off.<\/p>\n<p><iframe loading=\"lazy\" width=\"792\" height=\"446\" src=\"https:\/\/www.youtube.com\/embed\/DDMGxnrRlxI?feature=oembed\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<p><code><br \/>\n\/\/Knobs<br \/>\nconst int analogInPin1 = A0;  \/\/ Analog input pin that the potentiometer is attached to<\/p>\n<p>int sensorValue1 = 0;        \/\/ value read from the pot<br \/>\nint outputValue1 = 0;        \/\/ value output to the P<\/p>\n<p>\/\/hall input<br \/>\nint hall_sensor_y = A3;<br \/>\nint hall_sensor_b = A2;<br \/>\nint hall_sensor_g = 15;<\/p>\n<p>\/\/buzzer and LED<br \/>\nint buzzer = 16;<br \/>\nint LED = 17;<\/p>\n<p>float throttle = 0.0;<\/p>\n<p>boolean MotorOff = false;<\/p>\n<p>float Cn = 0;<\/p>\n<p>\/\/Motor setup<br \/>\nconst int y_motor_lout = 5;<br \/>\nconst int y_motor_pwm_hout = 6;<br \/>\nconst int b_motor_lout = 7;<br \/>\nconst int b_motor_pwm_hout = 9;<br \/>\nconst int g_motor_lout = 8;<br \/>\nconst int g_motor_pwm_hout = 10;<\/p>\n<p>enum WheelDirection {<br \/>\n    DIR_FORWARD,<br \/>\n    DIR_BACKWARD,<br \/>\n    DIR_STOP<br \/>\n};<\/p>\n<p>\/\/ MOTOR DRIVE<br \/>\nvoid MoveWheel(WheelDirection (dir), float (speed)) {<br \/>\n  if (MotorOff) return;<\/p>\n<p>  \/\/empty all motor registers<br \/>\n  \/\/half bridge driver, hi part is active high<br \/>\n  \/\/lo part is active low<br \/>\n  analogWrite(y_motor_pwm_hout, 0);\/\/set motor to stop<br \/>\n  analogWrite(b_motor_pwm_hout, 0);<br \/>\n  analogWrite(g_motor_pwm_hout, 0);<\/p>\n<p>  digitalWrite(y_motor_lout, HIGH);<br \/>\n  digitalWrite(b_motor_lout, HIGH);<br \/>\n  digitalWrite(g_motor_lout, HIGH);<\/p>\n<p>  int hall_y = digitalRead(hall_sensor_y);<br \/>\n  int hall_b = digitalRead(hall_sensor_b);<br \/>\n  int hall_g = digitalRead(hall_sensor_g);<\/p>\n<p>  \/\/Serial.print(\"\\r\\n\");<br \/>\n  \/\/Serial.print(hall_y);<br \/>\n  \/\/Serial.print(hall_b);<br \/>\n  \/\/Serial.print(hall_g);<\/p>\n<p>  if (dir == DIR_STOP) {<br \/>\n    \/\/ do nothing<br \/>\n  } else if (dir == DIR_FORWARD) {<br \/>\n        if (hall_y == 0 && hall_b == 0 && hall_g == 1) {\/\/001<br \/>\n          analogWrite(b_motor_pwm_hout, speed);<br \/>\n          digitalWrite(g_motor_lout, LOW);<br \/>\n        } else if (hall_y == 1 && hall_b == 0 && hall_g == 1) {\/\/101<br \/>\n          analogWrite(b_motor_pwm_hout, speed);<br \/>\n          digitalWrite(y_motor_lout, LOW);<br \/>\n        } else if (hall_y == 1 && hall_b == 0 && hall_g == 0) {\/\/100<br \/>\n          analogWrite(g_motor_pwm_hout, speed);<br \/>\n          digitalWrite(y_motor_lout, LOW);<br \/>\n        } else if (hall_y == 1 && hall_b == 1 && hall_g == 0) {\/\/110<br \/>\n          analogWrite(g_motor_pwm_hout, speed);<br \/>\n          digitalWrite(b_motor_lout, LOW);<br \/>\n        } else if (hall_y == 0 && hall_b == 1 && hall_g == 0) {\/\/010<br \/>\n          analogWrite(y_motor_pwm_hout, speed);<br \/>\n          digitalWrite(b_motor_lout, LOW);<br \/>\n        } else if (hall_y == 0 && hall_b == 1 && hall_g == 1) {\/\/011<br \/>\n          analogWrite(y_motor_pwm_hout, speed);<br \/>\n          digitalWrite(g_motor_lout, LOW);<br \/>\n        }<br \/>\n    } else if (dir == DIR_BACKWARD) {<br \/>\n      if (hall_y == 0 && hall_b == 0 && hall_g == 1) {\/\/001<br \/>\n          analogWrite(g_motor_pwm_hout, speed);<br \/>\n          digitalWrite(b_motor_lout, LOW);<br \/>\n        } else if (hall_y == 1 && hall_b == 0 && hall_g == 1) {\/\/101<br \/>\n          analogWrite(y_motor_pwm_hout, speed);<br \/>\n          digitalWrite(b_motor_lout, LOW);<br \/>\n        } else if (hall_y == 1 && hall_b == 0 && hall_g == 0) {\/\/100<br \/>\n          analogWrite(y_motor_pwm_hout, speed);<br \/>\n          digitalWrite(g_motor_lout, LOW);<br \/>\n        } else if (hall_y == 1 && hall_b == 1 && hall_g == 0) {\/\/110<br \/>\n          analogWrite(b_motor_pwm_hout, speed);<br \/>\n          digitalWrite(g_motor_lout, LOW);<br \/>\n        } else if (hall_y == 0 && hall_b == 1 && hall_g == 0) {\/\/010<br \/>\n          analogWrite(b_motor_pwm_hout, speed);<br \/>\n          digitalWrite(y_motor_lout, LOW);<br \/>\n        } else if (hall_y == 0 && hall_b == 1 && hall_g == 1) {\/\/011<br \/>\n          analogWrite(g_motor_pwm_hout, speed);<br \/>\n          digitalWrite(y_motor_lout, LOW);<br \/>\n        }<br \/>\n    }<br \/>\n    delay(10);<br \/>\n}<\/p>\n<p>void setup()<br \/>\n{<br \/>\n  \/\/Serial.begin(115200);<\/p>\n<p>  pinMode(y_motor_lout, OUTPUT);<br \/>\n  pinMode(y_motor_pwm_hout, OUTPUT);<br \/>\n  pinMode(b_motor_lout, OUTPUT);<br \/>\n  pinMode(b_motor_pwm_hout, OUTPUT);<br \/>\n  pinMode(g_motor_lout, OUTPUT);<br \/>\n  pinMode(g_motor_pwm_hout, OUTPUT);<\/p>\n<p>  pinMode(buzzer, OUTPUT);<br \/>\n  pinMode(LED, OUTPUT);<\/p>\n<p>  pinMode(hall_sensor_y, INPUT);<br \/>\n  pinMode(hall_sensor_b, INPUT);<br \/>\n  pinMode(hall_sensor_g, INPUT);<\/p>\n<p>  \/\/half bridge driver, hi part is active high<br \/>\n  \/\/ lo part is active low<br \/>\n  analogWrite(y_motor_pwm_hout, 0);\/\/set motor to stop<br \/>\n  analogWrite(b_motor_pwm_hout, 0);<br \/>\n  analogWrite(g_motor_pwm_hout, 0);<\/p>\n<p>  digitalWrite(y_motor_lout, HIGH);<br \/>\n  digitalWrite(b_motor_lout, HIGH);<br \/>\n  digitalWrite(g_motor_lout, HIGH);<\/p>\n<p>}<\/p>\n<p>void loop()<br \/>\n{<br \/>\n  \/\/ read the tuning knob value:<br \/>\n  for(int i=0;i<5;i++){\n    sensorValue1 += analogRead (analogInPin1);\n  }\n  sensorValue1  =  sensorValue1\/5;\n  \n  \/\/ map tuning knob to the range of the analog out:\n  outputValue1 = map(sensorValue1, 0, 1023, -90, 90);\n  \n    \/\/PID CALC\n    Cn = outputValue1;\n\n    \/\/MOTOR DRIVE\n    WheelDirection dir;\n\n    if (Cn > 0)<br \/>\n      dir = DIR_FORWARD;<br \/>\n    else if (Cn < -0)\n      dir = DIR_BACKWARD;\n    else\n      dir = DIR_STOP;\n    \n    throttle = abs(Cn);\n      \n      MoveWheel(dir, throttle);\n}\n<\/code><\/p>\n<p>May 12, 2015 - Hi, due to quite a few requests on the circuit I've decided to draw it out and post it here. The components for the half-bridge driver is picked up from http:\/\/www.irf.com\/technical-info\/appnotes\/an-978.pdf . The schematic below shows that the motor is driven by 24V power source, you can use higher voltage as long as it meets the rating of the MOSFET. Guys, please use the schematic at your own risk and please take necessary precaution when working with high voltage. I suggest test your circuit and motor at low voltage first before supplying full voltage. Again, I should stress that this is how I do it in respect to my hardware (Arduino) limitation, your project should use this as a base and expand to fix it's flaws. Cheers<\/p>\n<p><a href=\"http:\/\/aidilj.com\/worklog\/wp-content\/uploads\/2015\/01\/DSC03573.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/aidilj.com\/worklog\/wp-content\/uploads\/2015\/01\/DSC03573-1024x750.jpg\" alt=\"DSC03573\" width=\"640\" height=\"468\" class=\"aligncenter size-large wp-image-284\" srcset=\"https:\/\/aidilj.com\/worklog\/wp-content\/uploads\/2015\/01\/DSC03573-1024x750.jpg 1024w, https:\/\/aidilj.com\/worklog\/wp-content\/uploads\/2015\/01\/DSC03573-300x219.jpg 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>UPDATE : May 12, 2015, I&#8217;ve added schematic after the Arduino sketch below I couldn&#8217;t find much info on this on the net so I thought to share my experiment for those researching on this. Hardware as follows Arduino &#8211; Pro Micro 5V Mosfet &#8211; STP75NF75 Half bridge driver &#8211; IR2103 Three PWM outputs are <a class=\"read-more\" href=\"https:\/\/aidilj.com\/worklog\/2015\/01\/sensored-bldc-motor-control-with-arduino\/\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-277","post","type-post","status-publish","format-standard","hentry","category-electronics"],"_links":{"self":[{"href":"https:\/\/aidilj.com\/worklog\/wp-json\/wp\/v2\/posts\/277","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/aidilj.com\/worklog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aidilj.com\/worklog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aidilj.com\/worklog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/aidilj.com\/worklog\/wp-json\/wp\/v2\/comments?post=277"}],"version-history":[{"count":6,"href":"https:\/\/aidilj.com\/worklog\/wp-json\/wp\/v2\/posts\/277\/revisions"}],"predecessor-version":[{"id":285,"href":"https:\/\/aidilj.com\/worklog\/wp-json\/wp\/v2\/posts\/277\/revisions\/285"}],"wp:attachment":[{"href":"https:\/\/aidilj.com\/worklog\/wp-json\/wp\/v2\/media?parent=277"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aidilj.com\/worklog\/wp-json\/wp\/v2\/categories?post=277"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aidilj.com\/worklog\/wp-json\/wp\/v2\/tags?post=277"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}