{"id":1508,"date":"2019-04-12T19:07:27","date_gmt":"2019-04-12T11:07:27","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=1508"},"modified":"2020-05-12T10:36:29","modified_gmt":"2020-05-12T02:36:29","slug":"how-to-control-a-sg90-servo-motor-with-the-esp8266-nodemcu-lua-development-board","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-control-a-sg90-servo-motor-with-the-esp8266-nodemcu-lua-development-board\/","title":{"rendered":"How to control a SG90 servo motor with the ESP8266 NodeMCU LUA Development Board"},"content":{"rendered":"<p>When you are looking to operate remote-controlled or radio-controlled toy cars, robots and airplanes, you will need to use servo motors.<\/p>\n<p>Since the <a href=\"https:\/\/www.amazon.com\/HiLetgo-Internet-Development-Wireless-Micropython\/dp\/B010N1SPRK\/ref=as_li_ss_tl?ie=UTF8&linkCode=ll1&tag=clivsperswebs-20&linkId=f10c494f458ac25c0e9a23c52f604832&language=en_US\" rel=\"noopener\" target=\"_blank\">ESP8266 NodeMCU LUA Development Board<\/a> is cost efficient for <a href=\"https:\/\/www.techcoil.com\/glossary\/internet-of-thing\" rel=\"noopener\" target=\"_blank\">IOT<\/a> solutions, it can be used for controlling servo motors.<\/p>\n<p>So how can we control a servo motor with the ESP8266 NodeMCU LUA Development Board?<\/p>\n<p>This post discusses how we can control the <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 the ESP8266 NodeMCU LUA Development Board.<\/p>\n<h2>How to connect the SG90 servo motor to the ESP8266 NodeMCU LUA Development Board<\/h2>\n<p>First, let us look at how we can connect the SG90 servo motor to the ESP8266 NodeMCU LUA Development Board.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/ESP8266-on-breadboard-with-wires-connected-to-D1-3v3-and-Gnd-ports.jpg\" alt=\"ESP8266 on breadboard with wires connected to D1 3v3 and Gnd ports\"><\/p>\n<p>As shown above, we first seat the ESP8266 development board onto a <a href=\"https:\/\/www.amazon.com\/s\/ref=as_li_ss_tl?k=breadboard&linkCode=ll2&tag=clivsperswebs-20&linkId=2f9e4d2302b418e73665ad1677d53ef1&language=en_US\">breadboard<\/a>. Next to the development board, we use three <a href=\"https:\/\/www.amazon.com\/GenBasic-Solderless-Dupont-Compatible-Breadboard-Prototyping\/dp\/B077N6HFCX\/ref=as_li_ss_tl?ie=UTF8&linkCode=ll1&tag=clivsperswebs-20&linkId=f2001c226dba493361e582349330819c&language=en_US\" rel=\"noopener\" target=\"_blank\">male to male jumper wires<\/a>:<\/p>\n<ul>\n<li>The red one connects to a 3v3 port.<\/li>\n<li>The black one connects to a Gnd port.<\/li>\n<li>The orange one connects to the D1 port.<\/li>\n<\/ul>\n<p>Once we had connected the wires onto the breadboard, we then connect them to the SG90 servo motor in the following manner:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Tower-Pro-SG90-servo-motor-connected-to-wires-from-breadboard.jpg\" alt=\"Tower Pro SG90 servo motor connected to wires from breadboard\" ><\/p>\n<p>After we have connected the hardware in this way, we will be able to control the servo motor from the board.<\/p>\n<h2>Enabling ESP8266 Development on Arduino IDE<\/h2>\n<p>At this point in time, we are ready to get our mini program into the ESP8266 board to control the servo motor. In order to do so, we need to use a tool to write our program into the flash memory of the development board.<\/p>\n<p>Since it is easy to write code and flash programs with <a href=\"https:\/\/www.arduino.cc\/en\/main\/software\" rel=\"noopener\" target=\"_blank\">Arduino IDE<\/a>, we can use it to serve our purpose.<\/p>\n<p>In order to use Arduino IDE for this guide, be sure to <a href=\"https:\/\/www.techcoil.com\/blog\/enabling-esp8266-development-on-arduino-ide\/\" rel=\"noopener\" target=\"_blank\">enable ESP8266 development on Arduino IDE<\/a> before continuing.<\/p>\n<h2>Writing the Arduino Sketch to get ESP8266 NodeMCU LUA Development Board turn the servo motor<\/h2>\n<p>In order to understand how to control our servo motor, let's take a look at the following Arduino Sketch:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#include &lt;Servo.h&gt;\r\n\r\nServo servo;\r\n\r\nvoid setup() {\r\n    servo.attach(D1);\r\n    servo.write(0);\r\n    delay(2000);\r\n}\r\n\r\nvoid loop() {\r\n    servo.write(0);\r\n    delay(3000);\r\n    servo.write(90);\r\n    delay(3000);\r\n    servo.write(180);\r\n    delay(3000);\r\n}\r\n<\/pre>\n<p>So what will the above codes do to our servo motor?<\/p>\n<p>First, we included the <a href=\"https:\/\/www.arduino.cc\/en\/reference\/servo\" rel=\"noopener\" target=\"_blank\">Servo library<\/a> into the sketch.<\/p>\n<p>After we had done so, we create a <code>Servo<\/code> object from the library. When we had created a <code>Servo<\/code> object, we will be able to work on it inside the <a href=\"https:\/\/www.arduino.cc\/reference\/en\/language\/structure\/sketch\/setup\/\" rel=\"noopener\" target=\"_blank\"><code>setup<\/code><\/a> and <code>loop<\/code> functions. The <code>setup()<\/code> function will be run once and the <a href=\"https:\/\/www.arduino.cc\/reference\/en\/language\/structure\/sketch\/loop\/\" rel=\"noopener\" target=\"_blank\"><code>loop()<\/code><\/a> function will be run until power is cut off from the board.<\/p>\n<p>Within the <code>setup<\/code> function, we first attach the servo to D1, a predefined constant for the D1 port, via the <a href=\"https:\/\/www.arduino.cc\/en\/Reference\/ServoAttach\" rel=\"noopener\" target=\"_blank\"><code>Servo.attach()<\/code><\/a> function. By doing so, we will be able to control the servo which is attached to D1 port of the board. We then turn the servo motor to 0 degrees via <a href=\"https:\/\/www.arduino.cc\/en\/Reference\/ServoWrite\" rel=\"noopener\" target=\"_blank\"><code>Servo.write()<\/code><\/a> and make the program wait for 2 seconds.<\/p>\n<p>Within the <code>loop<\/code> function, we repeatedly turn the servo motor to 0, 90 and 180 degrees. Before making each turn, we make the program wait for 3 seconds.<\/p>\n<p><img decoding=\"async\" width=\"600\" height=\"900\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/How-to-control-a-SG90-servo-motor-with-the-ESP8266-NodeMCU-LUA-Development-Board.jpg\" alt=\"How to control a SG90 servo motor with the ESP8266 NodeMCU LUA Development Board\" class=\"aligncenter size-full wp-image-1935\" \/><\/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-ok\" 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-ok&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-ok&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%2F1508&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 you are looking to operate remote-controlled or radio-controlled toy cars, robots and airplanes, you will need to use servo motors.<\/p>\n<p>Since the <a href=\"https:\/\/www.amazon.com\/HiLetgo-Internet-Development-Wireless-Micropython\/dp\/B010N1SPRK\/ref=as_li_ss_tl?ie=UTF8&#038;linkCode=ll1&#038;tag=clivsperswebs-20&#038;linkId=f10c494f458ac25c0e9a23c52f604832&#038;language=en_US\" rel=\"noopener\" target=\"_blank\">ESP8266 NodeMCU LUA Development Board<\/a> is cost efficient for <a href=\"https:\/\/www.techcoil.com\/glossary\/internet-of-thing\" rel=\"noopener\" target=\"_blank\">IOT<\/a> solutions, it can be used for controlling servo motors.<\/p>\n<p>So how can we control a servo motor with the ESP8266 NodeMCU LUA Development Board?<\/p>\n<p>This post discusses how we can control the <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 the ESP8266 NodeMCU LUA Development Board.<\/p>\n","protected":false},"author":1,"featured_media":1519,"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":[619,589,590,561,484,615,613,617,616,618,614],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Tower-Pro-SG90-servo-motor-connected-to-wires-from-breadboard.jpg","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-ok","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1508"}],"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=1508"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1508\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1519"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=1508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=1508"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=1508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}