{"id":428,"date":"2013-02-24T13:22:23","date_gmt":"2013-02-24T13:22:23","guid":{"rendered":"http:\/\/randomnerdtutorials.wordpress.com\/?p=428"},"modified":"2019-04-02T10:23:39","modified_gmt":"2019-04-02T10:23:39","slug":"arduino-control-dc-motor-via-bluetooth","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/arduino-control-dc-motor-via-bluetooth\/","title":{"rendered":"Arduino &#8211; Control DC Motor via Bluetooth"},"content":{"rendered":"<p>In this project we will control a DC motor with a smartphone via bluetooth.<!--more--><\/p>\n<p><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-979\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2013\/07\/6bluethumb.jpg?resize=689%2C515&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"689\" height=\"515\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2013\/07\/6bluethumb.jpg?w=2592&amp;quality=100&amp;strip=all&amp;ssl=1 2592w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2013\/07\/6bluethumb.jpg?resize=300%2C224&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2013\/07\/6bluethumb.jpg?resize=1024%2C764&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2013\/07\/6bluethumb.jpg?w=2400&amp;quality=100&amp;strip=all&amp;ssl=1 2400w\" sizes=\"(max-width: 689px) 100vw, 689px\" \/><\/p>\n<p>This project is great to learn more about:<\/p>\n<ul>\n<li>DC motors<\/li>\n<li>Interfacing Arduino with your smartphone<\/li>\n<li>Bluetooth<\/li>\n<li>L293D IC<\/li>\n<\/ul>\n<p>If you don&#8217;t have the L293 IC you can make the same circuit using the H bridge, anyway I really recommend you to read more about that and the IC datasheet. There&#8217;s plenty of tutorials about that. Also If you don&#8217;t have a bluetooth module yet <a href=\"https:\/\/randomnerdtutorials.com\/parts-hc-05-bluetooth-module-review\/\" target=\"_blank\" rel=\"noopener noreferrer\">you can see my review about this bluetooth module and where to buy one.<\/a>\u00a0So Let&#8217;s start&#8230;<\/p>\n<h2>Parts required<a href=\"http:\/\/randomnerdtutorials.wordpress.com\/2013\/02\/24\/arduino-control-dc-motor-via-bluetooth\/img_0244\/\" rel=\"attachment wp-att-435\"><br \/>\n<\/a><\/h2>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" class=\"aligncenter wp-image-435 \" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2013\/02\/img_0244.jpg?resize=331%2C246&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"IMG_0244\" width=\"331\" height=\"246\" \/><\/p>\n<ul>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/compatible-arduino-uno-r3-board\/\" target=\"_blank\" rel=\"noopener noreferrer\">Arduino UNO<\/a>\u00a0\u2013 read\u00a0<a href=\"https:\/\/makeradvisor.com\/best-arduino-starter-kits\/\" target=\"_blank\" rel=\"noopener noreferrer\">Best Arduino Starter Kits<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/bluetooth-module-hc-06\/\" target=\"_blank\" rel=\"noopener noreferrer\">1x Bluetooth Module (for example: HC-05 or 06)<\/a><\/li>\n<li>1x Smartphone (any Android will work)<\/li>\n<li>BlueTerm application<\/li>\n<li>1x L293D IC<\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/mini-dc-motor\/\" target=\"_blank\" rel=\"noopener noreferrer\">1x DC motor<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\" rel=\"noopener noreferrer\">1x Breadboard<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\" rel=\"noopener noreferrer\">Jumper Cables<\/a><\/li>\n<\/ul>\n<p>You can use the preceding links or go directly to <a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\">MakerAdvisor.com\/tools<\/a> to find all the parts for your projects at the best price!<\/p><p style=\"text-align:center;\"><a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/header-200.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\"><\/a><\/p>\n<h2>Schematics<\/h2>\n<p>I can only\u00a0TX and RX\u00a0cables after you upload the Arduino code.<\/p>\n<p style=\"text-align: center;\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2013\/02\/schematics.png?quality=100&#038;strip=all&#038;ssl=1\" rel=\"nofollow\"><img data-recalc-dims=\"1\" decoding=\"async\" class=\"aligncenter wp-image-695\" title=\"\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2013\/02\/schematics.png?resize=376%2C279&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"schematics\" width=\"376\" height=\"279\" \/><\/a><\/p>\n<h3>Two common mistakes<\/h3>\n<ul>\n<li>You need to remove the RX and TX cables when you&#8217;re uploading the sketch to your Arduino.<\/li>\n<li>Sometimes people connect the TX from the bluetooth module to the TX of the Arduino&#8230; that&#8217;s wrong and it won&#8217;t work. Make sure you connect it properly, the TX into RX and the RX into the TX.<\/li>\n<\/ul>\n<p><strong>Note:<\/strong><br \/>\nIf the HC-05 Bluetooth Module asks for a password, It&#8217;s &#8216;1234&#8217;.<\/p>\n<h2>Upload this code\u00a0below<\/h2>\n<p>Make sure you remove the wires from RX and TX otherwise the code won&#8217;t upload properly!<br \/>\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*\n * Control DC motor with Smartphone via bluetooth\n * created by Rui Santos, http:\/\/randomnerdtutorials.com\n*\/\n\nint motorPin1 = 3; \/\/ pin 2 on L293D IC\nint motorPin2 = 4; \/\/ pin 7 on L293D IC\nint enablePin = 5; \/\/ pin 1 on L293D IC\nint state;\nint flag=0;        \/\/makes sure that the serial only prints once the state\n \nvoid setup() {\n    \/\/ sets the pins as outputs:\n    pinMode(motorPin1, OUTPUT);\n    pinMode(motorPin2, OUTPUT);\n    pinMode(enablePin, OUTPUT);\n    \/\/ sets enablePin high so that motor can turn on:\n    digitalWrite(enablePin, HIGH);\n    \/\/ initialize serial communication at 9600 bits per second:\n    Serial.begin(9600);\n}\n \nvoid loop() {\n    \/\/if some date is sent, reads it and saves in state\n    if(Serial.available() &gt; 0){     \n      state = Serial.read();   \n      flag=0;\n    }   \n    \/\/ if the state is '0' the DC motor will turn off\n    if (state == '0') {\n        digitalWrite(motorPin1, LOW); \/\/ set pin 2 on L293D low\n        digitalWrite(motorPin2, LOW); \/\/ set pin 7 on L293D low\n        if(flag == 0){\n          Serial.println(&quot;Motor: off&quot;);\n          flag=1;\n        }\n    }\n    \/\/ if the state is '1' the motor will turn right\n    else if (state == '1') {\n        digitalWrite(motorPin1, LOW); \/\/ set pin 2 on L293D low\n        digitalWrite(motorPin2, HIGH); \/\/ set pin 7 on L293D high\n        if(flag == 0){\n          Serial.println(&quot;Motor: right&quot;);\n          flag=1;\n        }\n    }\n    \/\/ if the state is '2' the motor will turn left\n    else if (state == '2') {\n        digitalWrite(motorPin1, HIGH); \/\/ set pin 2 on L293D high\n        digitalWrite(motorPin2, LOW); \/\/ set pin 7 on L293D low\n        if(flag == 0){\n          Serial.println(&quot;Motor: left&quot;);\n          flag=1;\n        }\n    }\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/Control_DC_motor_with_Smartphone_via_bluetooth.c\" target=\"_blank\">View raw code<\/a><\/p><\/p>\n<p>For the android communication with our bluetooth module I&#8217;ve used the BlueTerm app, It&#8217;s completely free, so you just need to go to \u201cPlay store\u201d and download it. Then you just need to connect your smarthphone with the bluetooth module. Remember to remove the TX and RX cables. (you can see in youtube video below how that&#8217;s done).<\/p>\n<p><strong>I&#8217;ve only set 3 commands to control the DC motor:<\/strong><\/p>\n<ul>\n<li>&#8216;0&#8217; &#8211; Turns off the DC motor<\/li>\n<li>&#8216;1&#8217; &#8211; DC motor rotates to right<\/li>\n<li>&#8216;2&#8217; &#8211; DC motor rotates to left<\/li>\n<\/ul>\n<h2>Watch the video demonstration<\/h2>\n<p style=\"text-align:center\"><iframe width=\"720\" height=\"405\" src=\"https:\/\/www.youtube.com\/embed\/nXymP6ttxD4?rel=0\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<p>Thanks for reading, you can contact me by leaving a comment. If you like this post probably you might like my next ones, so please support me by subscribing my blog and my <a href=\"https:\/\/www.facebook.com\/RandomNerdTutorials?ref=hl\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Facebook Page.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this project we will control a DC motor with a smartphone via bluetooth.<\/p>\n","protected":false},"author":1,"featured_media":979,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[303,271,2,267,264,10],"tags":[20,241,27,32,34,36,53,61,77,92],"class_list":["post-428","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-0-arduino","category-android","category-arduino","category-arduino-project","category-project","category-a-tutorials","tag-android","tag-arduino","tag-arduino-with-smartphone","tag-beginner","tag-blueterm-app","tag-bluetooth-module","tag-dc-motor-control","tag-easy","tag-hc-05","tag-l293d"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2013\/07\/6bluethumb.jpg?fit=2592%2C1936&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/428","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=428"}],"version-history":[{"count":0,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/428\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/979"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=428"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=428"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}