Back to Arduino Step By Step Course

Lesson 52: Control a DC motor with Relay using Arduino (two projects)

If you don't like to see ads while video is being played you can purchase this course for $200 from Udemy or purchase YouTube premium Here

Please select other codes for this lecture from the links below.

  • Purchase Dual channel relay from eBay
  • Purchase Dual channel relay from Amazon USA
  • Purchase Dual channel relay from Amazon Canada
  • Purchase Dual channel relay from All other Amazon
  • Part 5: DC Motors

    In this lesson we learn how to contorl a DC motor using two relays and Arduino . Please watch the video for full details. There are two codes. basic and advanced. This is basic code.

    AliExpress.com Product - 5pcs/lot L293D L293 DIP-16 In Stock
    
       
    /*
    * Lesson 52: Coontrol a DC motor with Relay using Arduino | Arduino Step By Step Course
    Basic code
     * Arduino code change the direction of rotation 
     * of a DC motor with 2 relays.
     
     * 
     * This is basic code. I have advanced code which can be used in both 
     * for Low-level trigger and High-lever trigger relay with clean code
    Please watch video explaining this code : https://youtu.be/2n0vUa0cZOI
    
     * 
     * Written by Ahmad Shamshiri for Robojax.com on 
     * Sunday August 18, 2019 
     * at 20:22 in Ajax, Ontario, Canada
     * 
    
    This code is available at http://robojax.com/course1/?vid=lecture52
     
    with over 100 lectures Free On  YouTube Watch it here http://robojax.com/L/?id=338
    Get the code for the course: http://robojax.com/L/?id=339  
    
    
    or make donation using PayPal http://robojax.com/L/?id=64
    * 
     * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* 
     * This code has been download from Robojax.com
        This program is free software: you can redistribute it and/or modify
        it under the terms of the GNU General Public License as published by
        the Free Software Foundation, either version 3 of the License, or
        (at your option) any later version.
    
        This program is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.
    
        You should have received a copy of the GNU General Public License
        along with this program.  If not, see <https://www.gnu.org/licenses/>.
     */
    
    
    int relay1 = 2;
    int relay2 = 3;
    
    
    void setup() {
    
        pinMode(relay1, OUTPUT);// set pin as output for relay 1
        pinMode(relay2, OUTPUT);// set pin as output for relay 2
    
    
        // keep the motor off by keeping both HIGH
        digitalWrite(relay1, HIGH); 
        digitalWrite(relay2, HIGH); 
    
     
      
      Serial.begin(9600);// initialize serial monitor with 9600 baud
      Serial.println("Robojax Motor Direction of Rotation");
      Serial.println("Using 2 Relays");  
      delay(2000);
    }
    
    void loop() {
    
     // Rotate in CCW direction
      digitalWrite(relay1, LOW);// turn relay 1 ON
      digitalWrite(relay2, HIGH);// turn relay 2 OFF  
      Serial.println("Rotating in CCW");  
      delay(3000);// wait for 3 seconds
    
     // stop the motor
      digitalWrite(relay1, HIGH);// turn relay 1 OFF
      digitalWrite(relay2, HIGH);// turn relay 2 OFF
      Serial.println("Stopped");  
      delay(2000);// stop for 2 seconds
      
     // Rotate in CW direction
      digitalWrite(relay1, HIGH);// turn relay 1 OFF
      digitalWrite(relay2, LOW);// turn relay 2 ON 
      Serial.println("Rotating in CW");  
      delay(3000);// wait for 3 seconds
    
     // stop the motor
      digitalWrite(relay1, HIGH);// turn relay 1 OFF
      digitalWrite(relay2, HIGH);// turn relay 2 OFF
      Serial.println("Stopped");  
      delay(2000);// stop for 2 seconds 
      
     Serial.println("===============");
    
              
    }// loop end
    
    
    
       

    The least I expect from you is to thumb up the video and subscribe to my channel. I appriciate that. .I have spent months making these lectures and writing code. You don't lose anything by subscribging to my channel. Your subscription is stamp of approval to my videos and more people can find them and in it turn it helps me. Thank you

    If you found this tutorial helpful, please support me so I can continue creating content like this. support me via PayPal