搜索代码

Controlling DC Motor Rotation Direction with Two Relays and an Arduino

Controlling DC Motor Rotation Direction with Two Relays and an Arduino

This code controls the direction of rotation of a DC motor with two relays. The basic code provides simple control, while the advanced code allows for the use of either low-level trigger relays or high-level trigger relays. This code controls the motor's direction of rotation with low-level trigger relays or high-level trigger relays easily. Please watch the video for details.
234-Basic code to control the direction of rotation of a motor with two relays
语言: C++
/*
 * Arduino code to change the direction of rotation 
 * of a DC motor with 2 relays.
 * 
 * This is basic code. I have advanced code that can be used in both 
 * for low-level trigger and high-level trigger relays with clean code.
 * 
 * Written by Ahmad Shamshiri for Robojax.com on 
 * Sunday, August 18, 2019 
 * at 20:22 in Ajax, Ontario, Canada
 * Watch video instructions for this code: https://youtu.be/JMBgGO07YLM
 * 
 * 
 * 
 Get this code and other Arduino codes from Robojax.com.
 Learn Arduino step by step in a structured course with all materials, wiring diagrams, and libraries
 all in one place. Purchase my course on Udemy.com: http://robojax.com/L/?id=62

If you found this tutorial helpful, please support me so I can continue creating 
content like this. You can support me on Patreon: http://robojax.com/L/?id=63

or make a 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 downloaded 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
235-Advanced code to control a motor using two relays and an Arduino
语言: C++
/*
 * Arduino code (Advanced) change the direction of rotation 
 * of a DC motor with 2 relays.
 * This code can be used with High-Level Trigger relays and
 * Low-Level Trigger relays. Watch the video for instructions.

 * 
 * Written by Ahmad Shamshiri for Robojax.com on 
 * Sunday, August 18, 2019 
 * at 20:22 in Ajax, Ontario, Canada
 * Watch video instructions for this code: https://youtu.be/JMBgGO07YLM
 * 
 Get this code and other Arduino codes from Robojax.com.
 Learn Arduino step by step in a structured course with all materials, wiring diagrams and libraries
 all in one place. 

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

or make a 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 downloaded 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;

const int triggerType = LOW;// your relay type

int relayOFF, relayON;// relay ON and OFF values for LOW and HIGH Trigger relays



void setup() {

    pinMode(relay1, OUTPUT);// set pin as output for relay 1
    pinMode(relay2, OUTPUT);// set pin as output for relay 2


    if(triggerType ==LOW){
      relayON = LOW;
      relayOFF = HIGH;
      digitalWrite(relay1, relayOFF);//keep both relay OFF so motor is stopped
      digitalWrite(relay2, relayOFF);//keep both relay OFF so motor is stopped
    }else{
      relayON = HIGH;
      relayOFF = LOW; 
      digitalWrite(relay1, relayOFF);//keep both relay OFF so motor is stopped
      digitalWrite(relay2, relayOFF);//keep both relay OFF so motor is stopped
    }
 
  
  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
  motorCCW(); 
  delay(3000);// wait for 3 seconds

  motorStop();
  delay(2000);
  
 // Rotate in CW direction
  motorCW();
  delay(3000);// wait for 3 seconds

 motorStop();
 delay(2000);
  
 Serial.println("===============");
          
}// loop end

/*
 * motorCCW() 
 * controls the relay so the motor rotates in CCW
 */
void motorCCW()
{
  digitalWrite(relay1, relayON);// turn relay 1 ON
  digitalWrite(relay2, relayOFF);// turn relay 2 OFF  
  Serial.println("Rotating in CCW");    
}// motorCCW()


/*
 * motorCW() 
 * controls the relay so the motor rotates in CW
 */
void motorCW()
{
  digitalWrite(relay1, relayOFF);// turn relay 1 OFF
  digitalWrite(relay2, relayON);// turn relay 2 ON 
  Serial.println("Rotating in CW");  
}//motorCW()


/*
 * motorStop()
 * controls the relay so the motor is stopped
 */
void motorStop()
{
  digitalWrite(relay1, relayOFF);// turn relay 1 OFF
  digitalWrite(relay2, relayOFF);// turn relay 2 OFF 
  Serial.println("Stopped");    
}

资源与参考

尚无可用资源。

文件📁

没有可用的文件。