Arduino rebooting when i run 12v dc motor

I am working on automation project its simply an vending machine.
Every thing is working fine but when dc motor start spin arduino start reboot and attached relays stop worked for few seconds.
i have also used pwm signal for smooth operation but its also get problem in 100 out of approx 5 times its stopped working . what should i do ? I have also researched on fly back diode and capacitor but this motor run clock wise and anticlock wise so no idea how to attach diode on it, if i choose cyramic capacitor what value should be used for 12v dc geared motor.

Hello
Post your sketch well formated and in code tags "</>" and the complete schematic.



#include "Arduino.h"

const int DELAY = 2;
const int DELAY2 = 200;

int loop_empty;
// sound data send
int LIFT_SND = 2;

// setup for IR sensor value in functions//
int iotreq = 6; // iot request sensor
int LIFT_EMPTY_COMM = A3;// empty signal to communication server


const int motorL =  A4;
const int motorLL = A5;
const int motorR =  A1;
const int motorRR = A0;
const int PWM = 5;  // USE DIGITAL PIN 5 AS PWM FOR MOTOR COLD START

// Empty IR Sensor data
int emptyL = 4;
int emptyR = 3;

// Limit Switch setup
int switchh1 = 10;
int switchh2 = 13;
int switchh3 = 11;
int switchh4 = 12;

bool switch1;
bool switch2;
bool switch3;
bool switch4;

//ACTIVATE THE WATER TIMER
int wat_actv_lift = A2;

int switch_left1;
int switch_left2;
int switch_right1;
int switch_right2;

bool payflag;

void left()
{
  switch_left1 = digitalRead(switchh1);
  switch_left2 = digitalRead(switchh2);

  if (switch_left1 == LOW)
  {
    switch1 = true;
    switch2 = false;
    //    Serial.println("Switch 1 is on");
    //    Serial.println("Switch 2 is off");
  }
  if (switch_left2 == LOW)
  {
    switch1 = false;
    switch2 = true;
    //    Serial.println("Switch 1 is off");
    //    Serial.println("Switch 2 is on");
  }
}

void right()
{
  switch_right1 = digitalRead(switchh3);
  switch_right2 = digitalRead(switchh4);
  if (switch_right1 == LOW)
  {
    switch3 = true;
    switch4 = false;
    //    Serial.println("Switch 3 is on");
    //    Serial.println("Switch 4 is off");
  }
  if (switch_right2 == LOW)
  {
    switch3 = false;
    switch4 = true;
    //    Serial.println("Switch 3 is off");
    //    Serial.println("Switch 4 is on");
  }
}


void setup()
{
  Serial.begin(9600);

  pinMode(iotreq, INPUT_PULLUP);

  pinMode(motorL, OUTPUT);
  pinMode(motorLL, OUTPUT);
  pinMode(motorR, OUTPUT);
  pinMode(motorRR, OUTPUT);

  pinMode(emptyL, INPUT_PULLUP);
  pinMode(emptyR, INPUT_PULLUP);

  pinMode(LIFT_EMPTY_COMM, OUTPUT);
  pinMode(LIFT_SND, OUTPUT);
  pinMode(wat_actv_lift, OUTPUT);

  pinMode(switchh1, INPUT_PULLUP);
  pinMode(switchh2, INPUT_PULLUP);
  pinMode(switchh3, INPUT_PULLUP);
  pinMode(switchh4, INPUT_PULLUP);


  digitalWrite(wat_actv_lift, HIGH);
  digitalWrite(LIFT_EMPTY_COMM, HIGH);
  digitalWrite(LIFT_SND, HIGH);

  digitalWrite(motorL, LOW);
  digitalWrite(motorLL, LOW);
  digitalWrite(motorR, LOW);
  digitalWrite(motorRR, LOW);


  left();
  right();

  // setup for returning first pusher to its home position
  if (switch1 == false) {
    for (int i = 0; i <= 150; i ++) {
      left();
      analogWrite(PWM, i);
      digitalWrite(motorL, LOW);
      digitalWrite(motorLL, HIGH);
      Serial.println("Pusher1 going to default position");
      delay(DELAY);
      if (switch1 == true)
      {
        digitalWrite(motorL, LOW);
        digitalWrite(motorLL, LOW);
        digitalWrite(motorR, LOW);
        digitalWrite(motorRR, LOW);
        Serial.println("Pusher1 reached at default position");
        break;
      }
    }
    while (switch1 == false)
    {
      left();
      Serial.println("Pusher1 still going to Home Position");
    }
    digitalWrite(motorL, LOW);
    digitalWrite(motorLL, LOW);
    digitalWrite(motorR, LOW);
    digitalWrite(motorRR, LOW);
    Serial.println("Pusher1 reached at default position");
    delay(DELAY2);
  }

  // End of setup for returning first pusher to its home position


  // sending pusher2 to default position
  if (switch3 == false) {
    for (int i = 0; i <= 150; i ++) {
      right();
      analogWrite(PWM, i);
      digitalWrite(motorR, LOW);
      digitalWrite(motorRR, HIGH);
      Serial.println("Pusher2 going to default position");
      if (switch3 == true)
      {
        digitalWrite(motorL, LOW);
        digitalWrite(motorLL, LOW);
        digitalWrite(motorR, LOW);
        digitalWrite(motorRR, LOW);
        Serial.println("Pusher2 reached to default position");
        break;
      }
      delay(DELAY);
    }
    while (switch3 == false)
    {
      right();
      Serial.println("Pusher2 still going to home position");
    }
    digitalWrite(motorL, LOW);
    digitalWrite(motorLL, LOW);
    digitalWrite(motorR, LOW);
    digitalWrite(motorRR, LOW);
    Serial.println("Pusher2 reached to default position");
  }

  // end of sending pusher 1 to home position if its not at place
  digitalWrite(motorL, LOW);
  digitalWrite(motorLL, LOW);
  digitalWrite(motorR, LOW);
  digitalWrite(motorRR, LOW);
}

void loop()
{
  int empty_L = digitalRead(emptyL);
  // Serial.println(empty_L);
  int empty_R = digitalRead(emptyR);
  // Serial.println(empty_R);
  int iot_req = digitalRead(iotreq);


  default_loop();
  //  Serial.print(" IOT REQUEST : ");
  //  Serial.println(iot_req);
  //
  //  Serial.print(" Empty L  : ");
  //  Serial.println(empty_L);
  //
  //  Serial.print(" Empty R  : ");
  //  Serial.println(empty_R);


  if (empty_L == 1 && empty_R == 1)
  {
    delay(3000);
    digitalWrite(LIFT_EMPTY_COMM, LOW); // change it to low
    Serial.println("Send comm server to empty data");
    digitalWrite(LIFT_SND, LOW);
    Serial.println("Empty Sound signal send");
    delay(200);
    digitalWrite(LIFT_SND, HIGH);
    Serial.println("Empty Sound signal end");
    delay(100);
    loop_empty = 0;
  }

  if (empty_L == 0 && empty_R == 0 && loop_empty <= 1)
  {
    digitalWrite(LIFT_EMPTY_COMM, HIGH); // change it to low
    Serial.println(" STOP Sending comm server to empty data");
    loop_empty++;
  }
  if (empty_L == 1 && empty_R == 0 && loop_empty <= 1)
  {
    digitalWrite(LIFT_EMPTY_COMM, HIGH); // change it to low
    Serial.println(" STOP Sending comm server to empty data");
    loop_empty++;
  }
  if (empty_L == 0 && empty_R == 1 && loop_empty <= 1)
  {
    digitalWrite(LIFT_EMPTY_COMM, HIGH); // change it to low
    Serial.println(" STOP Sending comm server to empty data");
    loop_empty++;
  }
  if (iot_req == 0)
  {
    Serial.println("IOT REQUEST FOUND");
    delay(500);
    payflag = true;
    process();
  }
}



void left_lift()
{

  left();
  if (switch2 == false) {
    for (int i = 30; i <= 120; i ++) {
      left();
      analogWrite(PWM, i);
      digitalWrite(motorL, HIGH);
      digitalWrite(motorLL, LOW);
      delay(DELAY);
      Serial.println("Pusher 1 going to target position");
      if (switch2 == true)
      {
        digitalWrite(motorL, LOW);
        digitalWrite(motorLL, LOW);
        break;
      }
    }
    while (switch2 == false) {
      left();
      Serial.println("Pusher 1 still going to target position");
    }
    digitalWrite(motorL, LOW);
    digitalWrite(motorLL, LOW);
    delay(DELAY2);
  }
  digitalWrite(wat_actv_lift, LOW);
  delay(200);
  digitalWrite(wat_actv_lift, HIGH);
  delay(200);


  if (switch1 == false) {
    for (int i = 30; i <= 120; i ++) {
      left();
      analogWrite(PWM, i);
      digitalWrite(motorL, LOW);
      digitalWrite(motorLL, HIGH);
      delay(DELAY);
      Serial.println("Pusher 1 going to home position");
      if (switch1 == true)
      {
        digitalWrite(motorL, LOW);
        digitalWrite(motorLL, LOW);
        payflag = false;
        break;
      }
    }
    while (switch1 == false)
    {
      left();
      Serial.println("Pusher 1 still going to home position");
    }
    digitalWrite(motorL, LOW);
    digitalWrite(motorLL, LOW);
    payflag = false;
    delay(DELAY2);
  }
}

void right_lift()
{
  right();
  if (switch4 == false) {
    for (int i = 30; i <= 150; i ++) {
      right();
      analogWrite(PWM, i);
      digitalWrite(motorR, HIGH);
      digitalWrite(motorRR, LOW);
      delay(DELAY);
      Serial.println("Pusher 2 going to target position");
      if (switch4 == true)
      {
        digitalWrite(motorR, LOW);
        digitalWrite(motorRR, LOW);
        break;
      }
    }
    while (switch4 == false) {
      right();
      Serial.println("Pusher 2 still going to target position");
    }
    digitalWrite(motorR, LOW);
    digitalWrite(motorRR, LOW);
    delay(DELAY2);
  }
  digitalWrite(wat_actv_lift, LOW);
  delay(200);
  digitalWrite(wat_actv_lift, HIGH);
  delay(200);


  if (switch3 == false) {
    for (int i = 30; i <= 150; i ++) {
      right();
      analogWrite(PWM, i);
      digitalWrite(motorR, LOW);
      digitalWrite(motorRR, HIGH);
      delay(DELAY);
      Serial.println("Pusher 2 going to home position");
      if (switch3 == true)
      {
        digitalWrite(motorR, LOW);
        digitalWrite(motorRR, LOW);
        payflag = false;
        break;
      }
    }
    while (switch3 == false)
    {
      right();
      Serial.println("Pusher 2 still going to home position");
    }
    digitalWrite(motorR, LOW);
    digitalWrite(motorRR, LOW);
    payflag = false;
    delay(DELAY2);
  }
}


void process()
{
  int empty_L = digitalRead(emptyL);
  // Serial.println(empty_LL);
  int empty_R = digitalRead(emptyR);
  // Serial.println(empty_RR);
  int iot_req = digitalRead(iotreq);

  right();
  left();


  if (empty_L == 0 && empty_R == 0) {
    Serial.println("Both stack is full");
    left_lift();
  }
  if (empty_L == 1 && empty_R == 0) {
    Serial.println("Right stack is full");
    right_lift();
  }
  if (empty_L == 0 && empty_R == 1) {
    Serial.println("Left stack is full");
    left_lift();
  }
}


void default_loop()
{
  // setup for returning first pusher to its home position
  if (switch1 == false && payflag == true) {
    for (int i = 0; i <= 150; i ++) {
      left();
      analogWrite(PWM, i);
      digitalWrite(motorL, LOW);
      digitalWrite(motorLL, HIGH);
      Serial.println("Pusher1 going to default position");
      delay(DELAY);
      if (switch1 == true)
      {
        payflag == false;
        digitalWrite(motorL, LOW);
        digitalWrite(motorLL, LOW);
        digitalWrite(motorR, LOW);
        digitalWrite(motorRR, LOW);
        Serial.println("Pusher1 reached at default position");
        break;
      }
    }
    while (switch1 == false)
    {
      left();
      Serial.println("Pusher1 still going to Home Position");
    }
    payflag == false;
    digitalWrite(motorL, LOW);
    digitalWrite(motorLL, LOW);
    digitalWrite(motorR, LOW);
    digitalWrite(motorRR, LOW);
    Serial.println("Pusher1 reached at default position");
    delay(DELAY2);
  }

  // End of setup for returning first pusher to its home position


  // sending pusher2 to default position
  if (switch3 == false && payflag == true) {
    for (int i = 0; i <= 150; i ++) {
      right();
      analogWrite(PWM, i);
      digitalWrite(motorR, LOW);
      digitalWrite(motorRR, HIGH);
      Serial.println("Pusher2 going to default position");
      if (switch3 == true)
      {
        payflag == false;
        digitalWrite(motorL, LOW);
        digitalWrite(motorLL, LOW);
        digitalWrite(motorR, LOW);
        digitalWrite(motorRR, LOW);
        Serial.println("Pusher2 reached to default position");
        break;
      }
      delay(DELAY);
    }
    while (switch3 == false)
    {
      right();
      Serial.println("Pusher2 still going to home position");
    }
    payflag == false;
    digitalWrite(motorL, LOW);
    digitalWrite(motorLL, LOW);
    digitalWrite(motorR, LOW);
    digitalWrite(motorRR, LOW);
    Serial.println("Pusher2 reached to default position");
  }

  // end of sending pusher 1 to home position if its not at place
  digitalWrite(motorL, LOW);
  digitalWrite(motorLL, LOW);
  digitalWrite(motorR, LOW);
  digitalWrite(motorRR, LOW);
  payflag == false;
}

i am using just 1 pwm signal for both motor hence its working fine. But problem rise after motor start and during running time

Use separate power supplies for the motor and the computer, and the problem will go away.

Be sure to connect all the grounds.

thanks for quick reply but i am already using 12v 5 A external power supply with comman ground

Can you confirm it's wired right? Have you tried powering the Arduino as well as the L298N with the 12V jack?

Yes everything is fine and well connecterd. All the connection are mounted on pcb

Hello
You may check logic of these while() ´s:

Line 144:     while (switch1 == false)
	Line 179:     while (switch3 == false)
	Line 280:     while (switch2 == false) {
	Line 310:     while (switch1 == false)
	Line 340:     while (switch4 == false) {
	Line 370:     while (switch3 == false)
	Line 432:     while (switch1 == false)
	Line 469:     while (switch3 == false)

All of them are able to block the execution of the sketch.

Have you tried powering the Arduino as well as the L298N with the 12V jack?

I ask this because I'm using 12V DC motors and an L298N in one of my projects. This is what I'm doing and it works.

Simply use a common 12V power as well as a common ground.

2 Likes

yes, i know that its a part of programme this one is not causing problem

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.