{"id":1916,"date":"2019-12-21T15:07:45","date_gmt":"2019-12-21T07:07:45","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=1916"},"modified":"2020-05-12T14:04:07","modified_gmt":"2020-05-12T06:04:07","slug":"how-to-control-a-servo-motor-with-an-esp32-development-board","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-control-a-servo-motor-with-an-esp32-development-board\/","title":{"rendered":"How to control a servo motor with an ESP32 development board"},"content":{"rendered":"<p>When I try to reuse the <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-control-a-sg90-servo-motor-with-the-esp8266-nodemcu-lua-development-board\/\" rel=\"noopener\" target=\"_blank\">Ardunio Sketch to control a servo motor with a ESP8266<\/a>, I encountered the following error during compile time:<\/p>\n<blockquote><p>\nWARNING: library Servo claims to run on (avr, sam, samd, nrf52, stm32f4) architecture(s) and may be incompatible with your current board which runs on (esp32) architecture(s).<br \/>\nIn file included from \/Users\/i309663\/Documents\/learning\/Arduino-sketches\/sketch_arduino_servo\/sketch_arduino_servo.ino:1:0:<br \/>\n\/Users\/i309663\/Arduino.1.8.7\/Arduino.app\/Contents\/Java\/libraries\/Servo\/src\/Servo.h:73:2: error: #error \"This library only supports boards with an AVR, SAM, SAMD, NRF52 or STM32F4 processor.\"<br \/>\n #error \"This library only supports boards with an AVR, SAM, SAMD, NRF52 or STM32F4 processor.\"<br \/>\n  ^<br \/>\nexit status 1<br \/>\nError compiling for board ESP32 Dev Module.\n<\/p><\/blockquote>\n<p>Therefore, I had to find another way to control my <a href=\"https:\/\/www.amazon.com\/American-Robotic-Supply-Authentic-Digital\/dp\/B0783NS32R\/ref=as_li_ss_tl?ie=UTF8&linkCode=ll1&tag=clivsperswebs-20&linkId=26741ffacf21192e3861cef4b3f65738&language=en_US\" rel=\"noopener\" target=\"_blank\">SG90 servo motor<\/a> with an ESP32 development board.<\/p>\n<p>Given that, this post shows how to control a servo motor with an ESP32 development board.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/ESP32-devkit-c-v4-connected-to-sg90-servo-motor.jpg\" alt=\"ESP32 devkit-c v4 connected to sg90 servo motor\" class=\"aligncenter size-full wp-image-1919\" \/><\/p>\n<h2>How to connect your servo motor to your ESP32 development board<\/h2>\n<p>If you look at your servo motor data sheet, then you will find information about how to connect your servo motor.<\/p>\n<p>For example, the <a href=\"http:\/\/www.ee.ic.ac.uk\/pcheung\/teaching\/DE1_EE\/stores\/sg90_datasheet.pdf\" rel=\"noopener\" target=\"_blank\">SG90 servo motor data sheet<\/a> indicated that the:<\/p>\n<ul>\n<li>orange wire has to be connected to a pin on your ESP32 that can apply pulse width modulation.<\/li>\n<li>red wire has to be connected to VCC.<\/li>\n<li>brown wire has to be connected to ground.<\/li>\n<\/ul>\n<p>Given that, this is how I had connected my SG90 servo motor to my ESP32 development board:<br \/>\n<img decoding=\"async\" width=\"800\" height=\"600\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/GPIO-cables-connected-to-SG90-Tower-Pro-servo-motor.jpg\" alt=\"GPIO cables connected to SG90 Tower Pro servo motor\" class=\"aligncenter size-full wp-image-1921\" \/><br \/>\n<img decoding=\"async\" width=\"800\" height=\"600\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Underside-of-ESP32-development-board-with-GPIO-cables-connected-to-GND-GPIO13-and-5V.jpg\" alt=\"Underside of ESP32 development board with GPIO cables connected to GND GPIO13 and 5V\" class=\"aligncenter size-full wp-image-1922\" \/><\/p>\n<p>As shown above, I had connected the:<\/p>\n<ul>\n<li><strong>orange<\/strong> wire of my SG90 servo motor to <strong>GPIO13<\/strong> on my ESP32 development board.<\/li>\n<li><strong>red<\/strong> wire of my SG90 servo motor to <strong>5V<\/strong> pin on my ESP32 development board.<\/li>\n<li><strong>brown<\/strong> wire of my SG90 servo motor to <strong>GND<\/strong> pin on my ESP32 development board.<\/li>\n<\/ul>\n<h2>Writing an Arduino Sketch to control a servo motor connected to an ESP32 board<\/h2>\n<p>Once you have connected your servo motor to your ESP32 board, you can write the software to control it.<\/p>\n<p>If you <a href=\"https:\/\/www.techcoil.com\/blog\/enabling-esp32-development-on-arduino-ide\/\" rel=\"noopener\" target=\"_blank\">enable ESP32 Development on Arduino IDE<\/a>, then you can upload an Arduino Sketch to your ESP32 board to control servo motor.<\/p>\n<h3>Installing a library to help us control servo motors connected to an ESP32 development board<\/h3>\n<p>Since I was not able use the <a href=\"https:\/\/www.arduino.cc\/en\/reference\/servo\" rel=\"noopener\" target=\"_blank\">default Arduino Servo library<\/a>, I had to find another library to control the servo motor.<\/p>\n<p>Given that, I proceeded to install the <a href=\"https:\/\/github.com\/jkb-git\/ESP32Servo\" rel=\"noopener\" target=\"_blank\">ESP32Servo library by John K. Bennett and Kevin Harrington<\/a>. <\/p>\n<p>In order to do so, I went to <strong>Tools -> Manage Libraries...<\/strong> after my Arduino IDE had started. After I had done so, the Library Manager window appeared in the foreground. Given that, I search for <strong>ESP32Servo<\/strong> and installed the ESP32Servo library by John K. Bennett and Kevin Harrington:<br \/>\n<img decoding=\"async\" width=\"800\" height=\"450\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Searching-for-ESP32Servo-in-Arduino-IDE-1.8.9-libary-manager.gif\" alt=\"Searching for ESP32Servo in Arduino IDE 1.8.9 libary manager\" class=\"aligncenter size-full wp-image-1923\" \/><\/p>\n<h3>Example Arduino Sketch to control a servo motor connected to an ESP32 development board<\/h3>\n<p>Once I had installed the library, I took out parts of <a href=\"https:\/\/github.com\/jkb-git\/ESP32Servo\/blob\/master\/examples\/Sweep\/Sweep.ino\" rel=\"noopener\" target=\"_blank\">an example sketch from the library<\/a> and wrote the following sketch: <\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#include &lt;ESP32Servo.h&gt;\r\n\r\nServo myservo;  \/\/ create servo object to control a servo\r\n\r\n\/\/ Recommended PWM GPIO pins on the ESP32 include 2,4,12-19,21-23,25-27,32-33 \r\nint servoPin = 13;\r\n\r\nvoid setup() {\r\n  myservo.setPeriodHertz(50); \r\n  myservo.attach(servoPin);\r\n}\r\n\r\nvoid loop() {\r\n  myservo.write(0); \r\n  delay(2000);\r\n  myservo.write(180);\r\n  delay(2000);\r\n}\r\n<\/pre>\n<p>So what will the sketch do?<\/p>\n<p>First of all, we include the <code>ESP32Servo<\/code> library into the sketch.<\/p>\n<p>After that, we create a <code>Servo<\/code> object for controlling the servo.<\/p>\n<p>When the <a href=\"https:\/\/www.arduino.cc\/reference\/en\/language\/structure\/sketch\/setup\/\" rel=\"noopener\" target=\"_blank\"><code>setup<\/code> function<\/a> is called, we set the frequency of the PWM period to 50Hz. After that, we attach the Servo object to the GPIO13 pin of our ESP32 development board. <\/p>\n<p>Whenever the <a href=\"https:\/\/www.arduino.cc\/reference\/en\/language\/structure\/sketch\/loop\/\" rel=\"noopener\" target=\"_blank\"><code>loop<\/code> function<\/a> is called, we position the servo motor at an angle of 0 and 180 degrees with a 2 seconds delay in between calls to <code>myservo.write<\/code>.<\/p>\n<p>After uploading the Arduino Sketch to my ESP32 development board, the servo motor starts to rotate to 0 and 180 degrees in 2 seconds interval:<br \/>\n<iframe width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/z2bDlQQ7Us4\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/p>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-uU\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-uU&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-uU&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F1916&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>When I try to reuse the <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-control-a-sg90-servo-motor-with-the-esp8266-nodemcu-lua-development-board\/\" rel=\"noopener\" target=\"_blank\">Ardunio Sketch to control a servo motor with a ESP8266<\/a>, I encountered the following error during compile time:<\/p>\n<blockquote><p>\nWARNING: library Servo claims to run on (avr, sam, samd, nrf52, stm32f4) architecture(s) and may be incompatible with your current board which runs on (esp32) architecture(s).<br \/>\nIn file included from \/Users\/i309663\/Documents\/learning\/Arduino-sketches\/sketch_arduino_servo\/sketch_arduino_servo.ino:1:0:<br \/>\n\/Users\/i309663\/Arduino.1.8.7\/Arduino.app\/Contents\/Java\/libraries\/Servo\/src\/Servo.h:73:2: error: #error &#8220;This library only supports boards with an AVR, SAM, SAMD, NRF52 or STM32F4 processor.&#8221;<br \/>\n #error &#8220;This library only supports boards with an AVR, SAM, SAMD, NRF52 or STM32F4 processor.&#8221;<br \/>\n  ^<br \/>\nexit status 1<br \/>\nError compiling for board ESP32 Dev Module.\n<\/p><\/blockquote>\n<p>Therefore, I had to find another way to control my <a href=\"https:\/\/www.amazon.com\/American-Robotic-Supply-Authentic-Digital\/dp\/B0783NS32R\/ref=as_li_ss_tl?ie=UTF8&#038;linkCode=ll1&#038;tag=clivsperswebs-20&#038;linkId=26741ffacf21192e3861cef4b3f65738&#038;language=en_US\" rel=\"noopener\" target=\"_blank\">SG90 servo motor<\/a> with an ESP32 development board.<\/p>\n<p>Given that, this post shows how to control a servo motor with an ESP32 development board.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/ESP32-devkit-c-v4-connected-to-sg90-servo-motor.jpg\" alt=\"ESP32 devkit-c v4 connected to sg90 servo motor\" class=\"aligncenter size-full wp-image-1919\" \/><\/p>\n","protected":false},"author":1,"featured_media":1924,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[375,4],"tags":[588,630,711,484,615,613,614],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/ESP32-devkit-c-v4-connected-to-sg90-servo-motor.jpg","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-uU","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1916"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=1916"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1916\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1924"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=1916"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=1916"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=1916"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}