








{"id":120,"date":"2020-10-12T21:32:45","date_gmt":"2020-10-12T21:32:45","guid":{"rendered":"https:\/\/osoyoo.info\/?p=120"},"modified":"2020-10-13T22:13:21","modified_gmt":"2020-10-13T22:13:21","slug":"arduino-basic-lesson-21-mini-fan","status":"publish","type":"post","link":"https:\/\/osoyoo.info\/index.php\/2020\/10\/12\/arduino-basic-lesson-21-mini-fan\/","title":{"rendered":"Hardware Programming with Arduino IDE &#8211; Lesson 21: Mini Motor Fan"},"content":{"rendered":"<p><a href=\"https:\/\/osoyoo.info\/index.php\/2020\/10\/12\/arduino-basic-lesson-20-ir-remote-control\/\"><img loading=\"lazy\" class=\"alignnone\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/Previous.png\" alt=\"\" width=\"206\" height=\"59\" \/><\/a>\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<a href=\"https:\/\/osoyoo.info\/index.php\/2020\/10\/11\/arduino-basic-tutorial-directory\/\"><img loading=\"lazy\" class=\"alignnone\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/Tutorial.png\" alt=\"\" width=\"206\" height=\"59\" \/><\/a>\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<a href=\"https:\/\/osoyoo.info\/index.php\/2020\/10\/12\/arduino-basic-lesson-22-ir-track-sensor\/\"><img loading=\"lazy\" class=\"alignright\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/Next.png\" alt=\"\" width=\"206\" height=\"59\" \/><\/a><\/p>\n<div align=\"center\">\n<div align=\"center\"><img class=\"aligncenter\" src=\"https:\/\/osoyoo.com\/picture\/icon\/authorized_retailers.png\" \/><\/div>\n<div align=\"center\"><\/div>\n<table>\n<tbody>\n<tr>\n<td>Buy from US<\/td>\n<td>Buy from UK<\/td>\n<td>Buy from DE<\/td>\n<td>Buy from IT<\/td>\n<td>Buy from FR<\/td>\n<td>Buy from ES<\/td>\n<td>Buy from JP<\/td>\n<\/tr>\n<tr>\n<td><a href=\"https:\/\/www.amazon.com\/dp\/B08J7DKJK9?ref=myi_title_dp\"><img loading=\"lazy\" class=\"alignnone size-large\" src=\"https:\/\/osoyoo.com\/picture\/icon\/us_buy_now.png\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.amazon.co.uk\/dp\/B08J6BL2P7\"><img loading=\"lazy\" class=\"alignnone\" src=\"https:\/\/osoyoo.com\/picture\/icon\/uk_buy_now.png\" alt=\"\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.amazon.de\/dp\/B08J6BL2P7\"><img loading=\"lazy\" class=\"alignnone size-large\" src=\"https:\/\/osoyoo.com\/picture\/icon\/de_buy_now.png\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.amazon.it\/dp\/B08J6BL2P7\"><img loading=\"lazy\" class=\"alignnone size-large\" src=\"https:\/\/osoyoo.com\/picture\/icon\/it_buy_now.png\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.amazon.fr\/dp\/B08J6BL2P7\"><img loading=\"lazy\" class=\"alignnone size-large\" src=\"https:\/\/osoyoo.com\/picture\/icon\/fr_buy_now.png\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.amazon.es\/dp\/B08J6BL2P7\"><img loading=\"lazy\" class=\"alignnone size-large\" src=\"https:\/\/osoyoo.com\/picture\/icon\/es_buy_now.png\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.amazon.co.jp\/dp\/B08J7TLP48\"><img loading=\"lazy\" class=\"alignnone size-large\" src=\"https:\/\/osoyoo.com\/picture\/icon\/jp_buy_now.png\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h2><strong>Content<\/strong><\/h2>\n<ol>\n<li><a href=\"#1\">Introduction<\/a><\/li>\n<li><a href=\"#2\">Preparations<\/a>\n<ul>\n<li><a href=\"#2.1\">Hardware<\/a><\/li>\n<li><a href=\"#2.2\">Software<\/a><\/li>\n<\/ul>\n<\/li>\n<li><a href=\"#3\">About the DC Motor<\/a>\n<ul>\n<li><a href=\"#3.1\">Overview<\/a><\/li>\n<li><a href=\"#3.2\">Features<\/a><\/li>\n<\/ul>\n<\/li>\n<li><a href=\"#4\">Why Driving Motors with L293D?<\/a>\n<ul>\n<li><a href=\"#4.2\">Specifications<\/a><\/li>\n<li><a href=\"#4.3\">Pin diagram of L293D&lt;<\/a><\/li>\n<\/ul>\n<\/li>\n<li><a href=\"#5\">Basic DC Motor direction<\/a>\n<ul>\n<li><a href=\"#5.1\">Connection<\/a><\/li>\n<li><a href=\"#5.2\">Code Program LCD<\/a><\/li>\n<li><a href=\"#5.3\">Running Result<\/a><\/li>\n<\/ul>\n<\/li>\n<li><a href=\"#6\">Control the DC Motor direction and speed by Arduino<\/a>\n<ul>\n<li><a href=\"#6.1\">Connection<\/a><\/li>\n<li><a href=\"#6.2\">Code Program LCD<\/a><\/li>\n<li><a href=\"#6.3\">Running Result<\/a><\/li>\n<\/ul>\n<\/li>\n<li><a href=\"#7\">Manually control the rotate direction and speed of the Mini Fan<\/a>\n<ul>\n<li><a href=\"#7.1\">Connection<\/a><\/li>\n<li><a href=\"#7.2\">Code Program LCD<\/a><\/li>\n<li><a href=\"#7.3\">Running Result<\/a><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h1 id=\"1\">Introduction<\/h1>\n<p>In this lesson, you will learn how to make a small fan by Arduino and L293DD. At the end of this tutorial, you should be able to control spinning direction and speed.<\/p>\n<div id=\"ContentBody\">\n<div class=\"PostContent\">\n<h1 id=\"2\">Preparations<\/h1>\n<h2 id=\"2.1\">Hardware<\/h2>\n<ul>\n<li>Osoyoo UNO Board (Fully compatible with Arduino UNO rev.3) x 1<\/li>\n<li>OSOYOO magic I\/O Shield for Arduino x1<\/li>\n<li>OSOYOO DC Motor with Fan x 1<\/li>\n<li>OSOYOO Red button module x 1<\/li>\n<li>OSOYOO Potentiometer Module x 1<\/li>\n<li>3pin PnP cable x5<\/li>\n<li>USB Cable x 1<\/li>\n<li>PC x 1<\/li>\n<\/ul>\n<p><strong>Warning<\/strong> \u2212 Do not drive the motor directly from Arduino board pins. This may damage the board. Use a driver Circuit or an IC.<\/p>\n<h2 id=\"2.2\">Software<\/h2>\n<p>Arduino IDE (version 1.6.4+)<\/p>\n<h1 id=\"3\">About the DC Motor<\/h1>\n<p><a title=\"By Abnormaal (Own work) [GFDL (http:\/\/www.gnu.org\/copyleft\/fdl.html) or CC-BY-SA-3.0 (http:\/\/creativecommons.org\/licenses\/by-sa\/3.0\/)], via Wikimedia Commons\" href=\"https:\/\/commons.wikimedia.org\/wiki\/File%3AElectric_motor.gif\"><img loading=\"lazy\" class=\"aligncenter\" src=\"https:\/\/upload.wikimedia.org\/wikipedia\/commons\/8\/89\/Electric_motor.gif\" alt=\"An animated image showing the internals of a DC motor and how the brushes activate the coils and make the motor turn\" width=\"252\" height=\"252\" \/><\/a><\/p>\n<h2 id=\"3.1\">Overview<\/h2>\n<p>A DC Motor is a type of electric motor that converts DC electrical power to mechanical power i.e. a DC supply is converted to rotation or movement. DC motors are one of the commonly used motors in different applications like electronic toys, power tools, portable fans, etc.<\/p>\n<p>DC Motors are further classified in to different types like series, shunt and compound and each type is used in different areas of applications. Some DC motors are also used in Robotic and Industrial applications for their easy control and precision.<\/p>\n<p>Since DC motors are generally associated with small to medium applications, where the system mainly consists of a Microcontroller as the main processing unit, controlling and driving a DC motor is very important. This is because, driving a motor directly using the microcontroller is not advised (sometimes not possible) as the current from the Microcontroller is very small (usually less than 30mA). <strong>Before you connect the circuit, <a href=\"http:\/\/www.sharetechnote.com\/html\/Arduino_MotorBasics.html\" target=\"_blank\" rel=\"noopener noreferrer\">check this link for how to power the dc motor correctly<\/a><\/strong>, thanks for the\u00a0www.sharetechnote.com!<\/p>\n<p>The\u00a0DC motor (Direct Current motor) is the most common type of motor. DC motors normally have just two leads, one positive and one negative. If you connect these two leads directly to a battery, the motor will rotate. If you switch the leads, the motor will rotate in the opposite direction.<\/p>\n<h2 id=\"3.2\">Features<\/h2>\n<ul>\n<li>Operating Temperature: -10\u00b0C ~ +60\u00b0C<\/li>\n<li>Rated Voltage: 6.0VDC<\/li>\n<li>Rated Load: 10 g*cm<\/li>\n<li>No-load Current: 70 mA max<\/li>\n<li>No-load Speed: 9100 \u00b11800 rpm<\/li>\n<li>Loaded Current: 250 mA max<\/li>\n<li>Loaded Speed: 4500 \u00b11500 rpm<\/li>\n<li>Starting Torque: 20 g*cm<\/li>\n<li>Starting Voltage: 2.0<\/li>\n<li>Stall Current: 500mA max<\/li>\n<li>Body Size: 27.5mm x 20mm x 15mm<\/li>\n<li>Shaft Size: 8mm x 2mm diameter<\/li>\n<li>Weight: 17.5 grams<\/li>\n<\/ul>\n<h1 id=\"4\">Why Driving Motors with L293DD?<\/h1>\n<p>Driving electromotors needs a high current. In addition, spinning direction and speed are two important parameters to be controlled. These requirements can be handled by using a microcontroller (or a development board like Arduino). But there is a problem; Microcontrollers cannot provide enough current to run the motor and if you connect the motor to the microcontroller directly, you may damage the microcontroller. For example, Arduino UNO pins are limited to 40mA of current which is far less than the 100-200mA current necessary to control a small hobby motor.\u00a0 To solve this, we should use a motor driver. Motor drivers can be connected to the microcontroller to receive\u00a0commands and run the motor with a high current.<\/p>\n<p>L293DD is one of the most popular motor drivers to run DC motors with up to 1A current load.L293DD has 4 outputs which makes it suitable for 4-wire stepper motors. L293DD can also be used to drive servo motors. In this project, you will learn how to drive motors with L293 and Arduino UNO as the controller. To learn more about L293DD, do not miss this article: <a href=\"https:\/\/electropeak.com\/learn\/guides\/l293d-theory-diagram-simulation-pinout\/\" rel=\"nofollow\">L293DD: Theory, Diagram, Simulation &amp; Pinout<\/a>.<\/p>\n<h2 id=\"4.2\"><strong>Specifications<\/strong><\/h2>\n<ul>\n<li>Supply Voltage Range 4.5V to 36V<\/li>\n<li>600-mA Output current capability per driver<\/li>\n<li>Separate Input-logic supply<\/li>\n<li>It can drive small DC-geared motors, bipolar stepper motor.<\/li>\n<li>Pulsed Current 1.2-A Per Driver<\/li>\n<li>Thermal Shutdown<\/li>\n<li>Internal ESD Protection<\/li>\n<li>High-Noise-Immunity Inputs<\/li>\n<\/ul>\n<h2 id=\"4.3\">Pin diagram of L293D<\/h2>\n<p><a href=\"https:\/\/osoyoo.com\/wp-content\/uploads\/2017\/10\/learn_arduino_L293D.jpg\"><img loading=\"lazy\" class=\"wp-image-11648 alignleft\" src=\"https:\/\/osoyoo.com\/wp-content\/uploads\/2017\/10\/learn_arduino_L293D.jpg\" sizes=\"(max-width: 230px) 100vw, 230px\" srcset=\"https:\/\/osoyoo.com\/wp-content\/uploads\/2017\/10\/learn_arduino_L293D.jpg 420w, https:\/\/osoyoo.com\/wp-content\/uploads\/2017\/10\/learn_arduino_L293D-197x300.jpg 197w\" alt=\"\" width=\"230\" height=\"350\" \/><\/a><\/p>\n<p><strong>1,2EN:<\/strong> To activate the channel 1 and 2 we supply +5v to this pin.<br \/>\n<strong>3,4EN:<\/strong> To activate the channel 3 and 4 we supply +5v to this pin.<br \/>\n<strong>Vcc1:<\/strong> Input voltage to derive the internal circuit (darligton array) = 4.5 to 36 v<br \/>\n<strong>Vcc2:<\/strong> Supply\/Output to appear at output = 4.5 to 36 v<br \/>\n<strong>1A:<\/strong> Channel-1 Input Pin<br \/>\n<strong>2A:<\/strong> Channel-2 Input Pin<br \/>\n<strong>3A:<\/strong> Channel-3 Input Pin<br \/>\n<strong>4A:<\/strong> Channel-4 Input Pin<br \/>\n<strong>1Y:<\/strong> Channel-1 Output Pin<br \/>\n<strong>2Y:<\/strong> Channel-2 Output Pin<br \/>\n<strong>3Y:<\/strong> Channel-3 Output Pin<br \/>\n<strong>4Y:<\/strong> Channel-4 Output Pin<\/p>\n<p><a href=\"http:\/\/www.microcontroller-project.com\/l293d-pin-description-and-working.html\" target=\"_blank\" rel=\"noopener noreferrer\">More info about the L293D chip, please check this link.<\/a> thanks for the\u00a0www.microcontroller-project.com!<\/p>\n<h3><\/h3>\n<h1 id=\"5\">Basic DC Motor direction<\/h1>\n<h2 id=\"5.1\">Connection<\/h2>\n<p>First, please plug OSOYOO Magic I\/O shield into UNO board:<\/p>\n<p><img loading=\"lazy\" class=\"size-full aligncenter\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/lesson4\/UNO%2BBase-min.png\" width=\"1000\" height=\"600\" \/><\/p>\n<p>Then connect DC motor to the port L_1 interface of the Magic I\/O shield,\u00a0 connect IN1 to D10, connect IN2 to D9. as below:<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-full\" src=\"http:\/\/osoyoo.com\/picture\/OSOYOO_PnP_Kit_for_Arduino\/Lesson22\/mini_fan1.jpg\" width=\"1000\" height=\"792\" \/><\/p>\n<h2 id=\"5.2\">Code Program<\/h2>\n<p>After above operations are completed, connect the Arduino board to your computer using the USB cable. The green power LED (labelled <strong>PWR<\/strong>) should go on.Open the Arduino IDE and choose corresponding board type and port type for you project. Then load up <a href=\"http:\/\/osoyoo.com\/picture\/ESP-01-PROGRAMMER\/Basic_mini_fan.zip\" target=\"_blank\" rel=\"noopener noreferrer\">the following sketch<\/a> onto your Arduino.<\/p>\n<pre><span style=\"color: #00979c;\">int<\/span> <span style=\"color: #000000;\">IN1<\/span> <span style=\"color: #434f54;\">=<\/span> <span style=\"color: #000000;\">10<\/span><span style=\"color: #000000;\">;<\/span> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #434f54;\">\/\/define digital output pin D10<\/span>\r\n<span style=\"color: #00979c;\">int<\/span> <span style=\"color: #000000;\">IN2<\/span> <span style=\"color: #434f54;\">=<\/span> <span style=\"color: #000000;\">9<\/span><span style=\"color: #000000;\">;<\/span> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #434f54;\">\/\/define digital output pin D9<\/span>\r\n\r\n<span style=\"color: #00979c;\">void<\/span> <span style=\"color: #5e6d03;\">setup<\/span> <span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">)<\/span>\r\n<span style=\"color: #000000;\">{<\/span>\r\n \u00a0<span style=\"color: #d35400;\">pinMode<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">IN1<\/span><span style=\"color: #434f54;\">,<\/span><span style=\"color: #00979c;\">OUTPUT<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #434f54;\">\/\/set pin 10 as output<\/span>\r\n \u00a0<span style=\"color: #d35400;\">pinMode<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">IN2<\/span><span style=\"color: #434f54;\">,<\/span><span style=\"color: #00979c;\">OUTPUT<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #434f54;\">\/\/ set pin 9 as output<\/span>\r\n<span style=\"color: #000000;\">}<\/span>\r\n\r\n<span style=\"color: #00979c;\">void<\/span> <span style=\"color: #5e6d03;\">loop<\/span> <span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">)<\/span> \r\n<span style=\"color: #000000;\">{<\/span>\r\n \u00a0<span style=\"color: #d35400;\">digitalWrite<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">IN1<\/span><span style=\"color: #434f54;\">,<\/span><span style=\"color: #00979c;\">LOW<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n \u00a0<span style=\"color: #d35400;\">digitalWrite<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">IN2<\/span><span style=\"color: #434f54;\">,<\/span><span style=\"color: #00979c;\">HIGH<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n<span style=\"color: #000000;\">}<\/span>\r\n\r\n<\/pre>\n<h2 id=\"5.3\">Running Result<\/h2>\n<p>A few seconds after the upload finishes, you can see that the DC motor turns in one direction. To drive the motor in opposite direction you just need to put HIGH instead of LOW and vice versa.<\/p>\n<h1 id=\"6\">Control the DC Motor direction and speed by Arduino<\/h1>\n<p>In this example, we will show how to control the direction and speed of a fan by using the OSOYOO Expansion Shield and the Arduino board.<\/p>\n<h2 id=\"6.1\">Connection<\/h2>\n<p>Connect DC motor to the port L_1 interface of the Magic I\/O shield,\u00a0 connect IN1 to D10, IN2 to D9, ENA to D5. as below:<img loading=\"lazy\" class=\"aligncenter size-full\" src=\"http:\/\/osoyoo.com\/picture\/OSOYOO_PnP_Kit_for_Arduino\/Lesson22\/mini_fan2.jpg\" width=\"1000\" height=\"943\" \/><\/p>\n<\/div>\n<div class=\"PostContent\">\n<h2 id=\"6.2\">Code Program<\/h2>\n<p>After above operations are completed, connect the Arduino board to your computer using the USB cable. The green power LED (labelled <strong>PWR<\/strong>) should go on.Open the Arduino IDE and choose corresponding board type and port type for you project. Then load up <a href=\"http:\/\/osoyoo.com\/picture\/ESP-01-PROGRAMMER\/MINI_FAN_CONTROL.zip\" target=\"_blank\" rel=\"noopener noreferrer\">the following sketch<\/a> onto your Arduino.<\/p>\n<pre><span style=\"color: #5e6d03;\">#define<\/span> <span style=\"color: #000000;\">ENA<\/span> \u00a0<span style=\"color: #000000;\">5<\/span> \u00a0\u00a0\u00a0\r\n<span style=\"color: #5e6d03;\">#define<\/span> <span style=\"color: #000000;\">IN1<\/span> \u00a0<span style=\"color: #000000;\">10<\/span> \u00a0\u00a0\r\n<span style=\"color: #5e6d03;\">#define<\/span> <span style=\"color: #000000;\">IN2<\/span> \u00a0<span style=\"color: #000000;\">9<\/span> \u00a0\r\n\r\n<span style=\"color: #95a5a6;\">\/*motor FAN control*\/<\/span>\r\n<span style=\"color: #00979c;\">void<\/span> <span style=\"color: #000000;\">go_Advance<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #00979c;\">void<\/span><span style=\"color: #000000;\">)<\/span> \u00a0<span style=\"color: #434f54;\">\/\/Forward<\/span>\r\n<span style=\"color: #000000;\">{<\/span>\r\n \u00a0<span style=\"color: #d35400;\">digitalWrite<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">IN1<\/span><span style=\"color: #434f54;\">,<\/span><span style=\"color: #00979c;\">LOW<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n \u00a0<span style=\"color: #d35400;\">digitalWrite<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">IN2<\/span><span style=\"color: #434f54;\">,<\/span><span style=\"color: #00979c;\">HIGH<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n \u00a0<span style=\"color: #d35400;\">analogWrite<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">ENA<\/span><span style=\"color: #434f54;\">,<\/span><span style=\"color: #000000;\">255<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span> \u00a0\u00a0\u00a0<span style=\"color: #434f54;\">\/\/SPEED 255(YOU CAN SET IT FROM 0~255);<\/span>\r\n<span style=\"color: #000000;\">}<\/span>\r\n<span style=\"color: #00979c;\">void<\/span> <span style=\"color: #000000;\">go_Back<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">)<\/span> \u00a0<span style=\"color: #434f54;\">\/\/Reverse<\/span>\r\n<span style=\"color: #000000;\">{<\/span>\r\n \u00a0<span style=\"color: #d35400;\">digitalWrite<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">IN1<\/span><span style=\"color: #434f54;\">,<\/span><span style=\"color: #00979c;\">HIGH<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n \u00a0<span style=\"color: #d35400;\">digitalWrite<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">IN2<\/span><span style=\"color: #434f54;\">,<\/span><span style=\"color: #00979c;\">LOW<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n \u00a0<span style=\"color: #d35400;\">analogWrite<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">ENA<\/span><span style=\"color: #434f54;\">,<\/span><span style=\"color: #000000;\">100<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span> \u00a0\u00a0<span style=\"color: #434f54;\">\/\/SPEED 100(YOU CAN SET IT FROM 0~255); \u00a0\u00a0<\/span>\r\n<span style=\"color: #000000;\">}<\/span>\r\n<span style=\"color: #00979c;\">void<\/span> <span style=\"color: #000000;\">stop_Stop<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">)<\/span> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #434f54;\">\/\/Stop<\/span>\r\n<span style=\"color: #000000;\">{<\/span>\r\n \u00a0<span style=\"color: #d35400;\">digitalWrite<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">IN1<\/span><span style=\"color: #434f54;\">,<\/span><span style=\"color: #00979c;\">LOW<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n \u00a0<span style=\"color: #d35400;\">digitalWrite<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">IN2<\/span><span style=\"color: #434f54;\">,<\/span><span style=\"color: #00979c;\">LOW<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n \u00a0<span style=\"color: #d35400;\">analogWrite<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">ENA<\/span><span style=\"color: #434f54;\">,<\/span><span style=\"color: #000000;\">0<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span> \u00a0\u00a0\u00a0\u00a0<span style=\"color: #434f54;\">\/\/SPEED 0(YOU CAN SET IT FROM 0~255);<\/span>\r\n<span style=\"color: #000000;\">}<\/span>\r\n\r\n<span style=\"color: #00979c;\">void<\/span> <span style=\"color: #5e6d03;\">setup<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">)<\/span>\r\n<span style=\"color: #000000;\">{<\/span>\r\n \u00a0<span style=\"color: #d35400;\">pinMode<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">IN1<\/span><span style=\"color: #434f54;\">,<\/span> <span style=\"color: #00979c;\">OUTPUT<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span> \r\n \u00a0<span style=\"color: #d35400;\">pinMode<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">IN2<\/span><span style=\"color: #434f54;\">,<\/span> <span style=\"color: #00979c;\">OUTPUT<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span> \r\n \u00a0<span style=\"color: #d35400;\">pinMode<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">ENA<\/span><span style=\"color: #434f54;\">,<\/span> <span style=\"color: #00979c;\">OUTPUT<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span> \u00a0\r\n \u00a0<span style=\"color: #000000;\">go_Advance<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span><span style=\"color: #434f54;\">\/\/Forward<\/span>\r\n \u00a0<span style=\"color: #d35400;\">delay<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">4000<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span> <span style=\"color: #434f54;\">\/\/DELAY FOR 4 seconds.<\/span>\r\n \u00a0<span style=\"color: #000000;\">go_Back<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span> \u00a0\u00a0<span style=\"color: #434f54;\">\/\/Reverse<\/span>\r\n \u00a0<span style=\"color: #d35400;\">delay<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">2000<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span> <span style=\"color: #434f54;\">\/\/DELAY FOR 2 seconds.<\/span>\r\n \u00a0<span style=\"color: #000000;\">stop_Stop<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span> <span style=\"color: #434f54;\">\/\/Stop<\/span>\r\n<span style=\"color: #000000;\">}<\/span>\r\n\r\n<span style=\"color: #00979c;\">void<\/span> <span style=\"color: #5e6d03;\">loop<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">{<\/span>\r\n<span style=\"color: #000000;\">}<\/span>\r\n\r\n<\/pre>\n<h2 id=\"6.3\">Running Result<\/h2>\n<p>A few seconds after the upload finishes, the fan runs at full speed for 4 seconds, then the fan will reverse at a slower speed for 2 seconds and then stop<\/p>\n<h1 id=\"7\">Manually control the rotate direction and speed of the Mini Fan<\/h1>\n<p>In this example,\u00a0we use\u00a0a potentiometer to control the speed of the motor and a push button to control the direction.\u00a0Moving forwards, this hardware and code can be adapted to make a small driving robot.<\/p>\n<h2 id=\"7.1\">Connection<\/h2>\n<p>Connect DC motor to the port L_1 interface of the Magic I\/O shield,\u00a0 connect IN1 to D10, connect IN2 to D9, ENA to D5,\u00a0 Connect OSOYOO Red button module to the port D7 of the Magic I\/O shield with 3-pin PNP cables,\u00a0 Connect OSOYOO Potentiometer Module to the port A0 of the Magic I\/O shield with 3-pin PNP cables. as below:<img loading=\"lazy\" class=\"aligncenter size-full\" src=\"http:\/\/osoyoo.com\/picture\/OSOYOO_PnP_Kit_for_Arduino\/Lesson22\/mini_fan3.jpg\" width=\"1000\" height=\"713\" \/><\/p>\n<h2 id=\"7.2\">Code Program<\/h2>\n<p>After above operations are completed, connect the Arduino board to your computer using the USB cable. The green power LED (labelled <strong>PWR<\/strong>) should go on.Open the Arduino IDE and choose corresponding board type and port type for you project. Then load up <a href=\"http:\/\/osoyoo.com\/picture\/ESP-01-PROGRAMMER\/Button_potentiometer_fan.zip\">the following sketch<\/a> onto your Arduino.<\/p>\n<pre><span style=\"color: #00979c;\">int<\/span> <span style=\"color: #000000;\">switchPinFwd<\/span> <span style=\"color: #434f54;\">=<\/span> <span style=\"color: #000000;\">7<\/span><span style=\"color: #000000;\">;<\/span> \u00a0\u00a0\u00a0\u00a0<span style=\"color: #434f54;\">\/\/Input from the switch when in the Forward position<\/span>\r\n<span style=\"color: #00979c;\">int<\/span> <span style=\"color: #000000;\">potentiometerIn<\/span><span style=\"color: #000000;\">;<\/span> \u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #434f54;\">\/\/variable to hold the potentiometer input<\/span>\r\n\r\n<span style=\"color: #00979c;\">int<\/span> <span style=\"color: #000000;\">fwdPin<\/span> <span style=\"color: #434f54;\">=<\/span> <span style=\"color: #000000;\">10<\/span><span style=\"color: #000000;\">;<\/span> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #434f54;\">\/\/Logic level output to the H-Bridge (Forward)<\/span>\r\n<span style=\"color: #00979c;\">int<\/span> <span style=\"color: #000000;\">revPin<\/span> <span style=\"color: #434f54;\">=<\/span> <span style=\"color: #000000;\">9<\/span><span style=\"color: #000000;\">;<\/span> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #434f54;\">\/\/Another logic level output to the H-Bridge (Reverse)<\/span>\r\n\r\n<span style=\"color: #00979c;\">void<\/span> <span style=\"color: #5e6d03;\">setup<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">)<\/span> <span style=\"color: #000000;\">{<\/span>\r\n \u00a0<span style=\"color: #434f54;\">\/\/ put your setup code here, to run once:<\/span>\r\n \u00a0<span style=\"color: #d35400;\">pinMode<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">switchPinFwd<\/span><span style=\"color: #434f54;\">,<\/span> <span style=\"color: #00979c;\">INPUT_PULLUP<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n\r\n \u00a0<span style=\"color: #d35400;\">pinMode<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">fwdPin<\/span><span style=\"color: #434f54;\">,<\/span> <span style=\"color: #00979c;\">OUTPUT<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span> <span style=\"color: #434f54;\">\/\/Set the forward pin to an output<\/span>\r\n \u00a0<span style=\"color: #d35400;\">pinMode<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">revPin<\/span><span style=\"color: #434f54;\">,<\/span> <span style=\"color: #00979c;\">OUTPUT<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span> <span style=\"color: #434f54;\">\/\/Set the forward pin to an output<\/span>\r\n<span style=\"color: #000000;\">}<\/span>\r\n\r\n<span style=\"color: #00979c;\">void<\/span> <span style=\"color: #5e6d03;\">loop<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">)<\/span> <span style=\"color: #000000;\">{<\/span>\r\n \u00a0<span style=\"color: #434f54;\">\/\/ put your main code here, to run repeatedly:<\/span>\r\n \u00a0<span style=\"color: #000000;\">potentiometerIn<\/span> <span style=\"color: #434f54;\">=<\/span> <span style=\"color: #d35400;\">analogRead<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">A0<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n \u00a0<span style=\"color: #00979c;\">int<\/span> <span style=\"color: #000000;\">output<\/span> <span style=\"color: #434f54;\">=<\/span> <span style=\"color: #000000;\">potentiometerIn<\/span> <span style=\"color: #434f54;\">\/<\/span> <span style=\"color: #000000;\">4<\/span><span style=\"color: #000000;\">;<\/span> <span style=\"color: #434f54;\">\/\/divide the potentiometer input by 4 so it can be used in the AnalogWrite function<\/span>\r\n\r\n \u00a0<span style=\"color: #5e6d03;\">if<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #d35400;\">digitalRead<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">switchPinFwd<\/span><span style=\"color: #000000;\">)<\/span> <span style=\"color: #434f54;\">==<\/span> <span style=\"color: #00979c;\">HIGH<\/span><span style=\"color: #000000;\">)<\/span> <span style=\"color: #434f54;\">\/\/Check to see if the pin is high or low<\/span>\r\n \u00a0<span style=\"color: #000000;\">{<\/span>\r\n \u00a0\u00a0\u00a0<span style=\"color: #434f54;\">\/\/If the pin is HIGH, it must be set to forward<\/span>\r\n \u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #d35400;\">analogWrite<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">fwdPin<\/span><span style=\"color: #434f54;\">,<\/span> <span style=\"color: #000000;\">output<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span> <span style=\"color: #434f54;\">\/\/Output our potentiometer value on the forward pin.<\/span>\r\n \u00a0<span style=\"color: #000000;\">}<\/span>\r\n \u00a0<span style=\"color: #5e6d03;\">else<\/span>\r\n \u00a0<span style=\"color: #000000;\">{<\/span>\r\n \u00a0\u00a0\u00a0<span style=\"color: #434f54;\">\/\/Otherwise the switch must be set to Reverse<\/span>\r\n \u00a0\u00a0\u00a0<span style=\"color: #d35400;\">analogWrite<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">revPin<\/span><span style=\"color: #434f54;\">,<\/span> <span style=\"color: #000000;\">output<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span> <span style=\"color: #434f54;\">\/\/Output our potentiometer value on the forward pin.<\/span>\r\n \u00a0<span style=\"color: #000000;\">}<\/span>\r\n \u00a0<span style=\"color: #d35400;\">delay<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">25<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n<span style=\"color: #000000;\">}<\/span>\r\n\r\n<\/pre>\n<h2 id=\"7.3\">Running Result<\/h2>\n<p>A few seconds after the upload finishes,\u00a0\u00a0plug the motor power supply in so the motor has power and after a few seconds try adjusting the potentiometer to adjust the motor speed. When changing directions, ensure the motor is stopped as it is not a good idea to reverse the polarity of a motor while running. Going forward, code could be added to this to prevent the user from changing direction unless the throttle is at 0.<\/p>\n<p><center><iframe src=\"https:\/\/www.youtube.com\/embed\/FuITgLOjwKI\" width=\"1280\" height=\"720\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/center><a href=\"https:\/\/osoyoo.info\/index.php\/2020\/10\/12\/arduino-basic-lesson-20-ir-remote-control\/\"><img loading=\"lazy\" class=\"alignnone\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/Previous.png\" alt=\"\" width=\"206\" height=\"59\" \/><\/a>\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<a href=\"https:\/\/osoyoo.info\/index.php\/2020\/10\/11\/arduino-basic-tutorial-directory\/\"><img loading=\"lazy\" class=\"alignnone\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/Tutorial.png\" alt=\"\" width=\"206\" height=\"59\" \/><\/a>\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<a href=\"https:\/\/osoyoo.info\/index.php\/2020\/10\/12\/arduino-basic-lesson-22-ir-track-sensor\/\"><img loading=\"lazy\" class=\"alignright\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/Next.png\" alt=\"\" width=\"206\" height=\"59\" \/><\/a><\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0 Buy from US Buy from UK Buy from DE Buy from IT Buy from FR Buy from ES Buy from JP Content Introduction Preparations Hardware Software About the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":164,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/posts\/120"}],"collection":[{"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/comments?post=120"}],"version-history":[{"count":3,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/posts\/120\/revisions"}],"predecessor-version":[{"id":163,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/posts\/120\/revisions\/163"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/media\/164"}],"wp:attachment":[{"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/media?parent=120"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/categories?post=120"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/tags?post=120"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}