Need help in coding Control DC Motor with 2 Limit Switches

Need help in coding Control DC Motor with 2 Limit Switches

Hi souhil,

In order for others to help you, it’s important to provide more details about your project and the functionality you’re seeking.
Cheers,
Anat

1 Like

@souhil can you Please be more specific about what you are dealing with.
Thank you
Jush

hello broth yes i wont to motor change direction by 2 switch
and thankx for repley

Are you planning on controlling the motors via an H-bridge or MOSFET? If it’s via an H-bridge, the L298N dual H-bridge would make life very easy for you. Here’s a circuit with the two motors, the L298N to control the motors, and four limit switches in the form of micro switches:

https://www.circuito.io/app?components=514,11022,305155,305155,305155,305155,7654321

Is this a suitable start for us to go from?

I couldn’t help myself; I started fiddling with the sample code and here’s the firmware I modified from the sample code. It runs the motors at quarter speed. If you hold both button 1 and 2, the motors are stopped. Holding buttons 3 and 4 will start them again. Pressing a button will change the direction of a motor. Buttons 1 and 2 change the direction of motor 1 and buttons 3 and 4 change the direction of motor 2. It should be enough for you to see if everything is connected properly. You can monitor the state of the buttons in the serial monitor.

NB. This is untested code, use at your own risk.

// Include Libraries
#include "Arduino.h"
#include "DCMDriverL298.h"
#include "Button.h"

// Pin Definitions
#define DCMOTORDRIVERL298_PIN_ENA	3
#define DCMOTORDRIVERL298_PIN_INT1	2
#define DCMOTORDRIVERL298_PIN_INT2	4
#define DCMOTORDRIVERL298_PIN_ENB	5
#define DCMOTORDRIVERL298_PIN_INT3	6
#define DCMOTORDRIVERL298_PIN_INT4	7
#define MICROSWITCH_1_PIN_COM	8
#define MICROSWITCH_2_PIN_COM	9
#define MICROSWITCH_3_PIN_COM	10
#define MICROSWITCH_4_PIN_COM	11

// object initialization
DCMDriverL298 dcMotorDriverL298(DCMOTORDRIVERL298_PIN_ENA,DCMOTORDRIVERL298_PIN_INT1,DCMOTORDRIVERL298_PIN_INT2,DCMOTORDRIVERL298_PIN_ENB,DCMOTORDRIVERL298_PIN_INT3,DCMOTORDRIVERL298_PIN_INT4);
Button microSwitch_1(MICROSWITCH_1_PIN_COM);
Button microSwitch_2(MICROSWITCH_2_PIN_COM);
Button microSwitch_3(MICROSWITCH_3_PIN_COM);
Button microSwitch_4(MICROSWITCH_4_PIN_COM);

bool motor1Clockwise;
bool motor2Clockwise;
int motor1Speed;
int motor2Speed;

// Setup the essentials for your circuit to work. It runs first every time your circuit is powered with electricity.
void setup() 
{
    Serial.begin(9600);
    while (!Serial) ; // wait for serial port to connect. Needed for native USB
    Serial.println("Starting");

    microSwitch_1.init();
    microSwitch_2.init();
    microSwitch_3.init();
    microSwitch_4.init();

    motor1Clockwise = true;
    motor2Clockwise = true;
    motor1Speed = 64;
    motor2Speed - 64;

    // L298N Motor Driver Board Module
    // Start both motors. note that rotation direction is determined by the motors connection to the driver.
    // You can change the speed by setting a value between 0-255, and set the direction by changing between 1 and 0.
    startMotors();
}

void loop() 
{
    //Stop both motors
    dcMotorDriverL298.stopMotors();

    bool microSwitch_1Val = microSwitch_1.read();
    bool microSwitch_2Val = microSwitch_2.read();
    bool microSwitch_3Val = microSwitch_3.read();
    bool microSwitch_4Val = microSwitch_4.read();

    Serial.print(F("Buttons: "));
    Serial.print(microSwitch_1Val);
    Serial.print(microSwitch_2Val);
    Serial.print(microSwitch_3Val);
    Serial.print(microSwitch_4Val);
    Serial.println();

    if (microSwitch_1Val || microSwitch_2Val) {
        motor1Clockwise = !motor1Clockwise;
        startMotors();
    }

    if (microSwitch_3Val || microSwitch_4Val) {
        motor2Clockwise = !motor2Clockwise;
        startMotors();
    }

    if (microSwitch_1Val && microSwitch_2Val) {
        dcMotorDriverL298.stopMotors();
    }

    if (microSwitch_3Val && microSwitch_4Val) {
        startMotors();
    }
}

void startMotors()
{
    dcMotorDriverL298.setMotorA(motor1Speed, motor1Clockwise ? 1 : 0);
    dcMotorDriverL298.setMotorB(motor1Speed, motor2Clockwise ? 1 : 0);
}
1 Like