Motor spinns of when powering up the Mega

Hi Everybody,

I´m on a solar tracker project and have the same problem as in that old thread here.

A motor start working crazy when I turn on my robot - Using Arduino / Motors, Mechanics, Power and CNC - Arduino Forum

I´m controlling 2 DC Motors over a Dual-Controller WSDC2412D on a Mega-Board (not original). One of the motors is starting to turn for 1 or 2 seconds when I power on the Mega and then stops again.

In the void setup () I´m writing 0 analogWrite(enA, 0); on all PWM-pins and today I´ve tried the recommended solution with the 10K resistors to ground on the PWM-pins. Unfortunately nothing has changed.

I also have changed the PWM frequency to 4000Hz but this did not affect the problem.

With the same code on the Uno, I had no problem at all.

I would appreciate your help a lot :smiley:

Have a great evening!

Ben

Always show us a good schematic of your proposed circuit.
Show us good images of your ‘actual’ wiring. Give links to components.


In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.

Use the < CODE / > icon from the ‘posting menu’ to attach the copied sketch.

Hi Larry,

I don´t have a schematic available but it is pretty straight forward on the DC-Motors.

The overall project is getting more and more complex since I´m adding more and more functionality and sensors.

I´m using this Mega board here…

Arduino MEGA 2560 With WiFi Built-in - ESP8266 : 10 Steps - Instructables

I also use a second DC Motor Controller (IBT2) for a linear actuator but this one gives me no trouble.

I´ve also a joystick, 4 photo resistors, the compass module HMC5883L as well as one interrupt push button and an LED on the prototype board included.

So the code has grown a lot and has currently 400 lines, so I don´t want to post it all in the thread. Or is this not a problem?

The DC-Controller which gives me the problem is connected to the Pins 8 – 13 (4x digital + 2x PWM being 9-10)

This is the definition of pins and variables...

/ for switch control automatic vs manual mode
int mode = 0;                       //0 = manual, 1 = automatic
int modePin = 2;                    //input switch interrupt
int LEDPin = 5;                     //output LED with 330 Ohm resistor
int modeOld = 1;                    //1 = if switch is not pressed with 10K Ohm pull-up resistor, 0 = if switch is presssed
int modeNew;                        //will be read

// for compass modul HMC5883L
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_HMC5883_U.h>
float headingDeg;
Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(777);       //Assign a unique ID to this sensor in ()
// V5 = red
// GND = blue
//SDA pin = brown --> A4
//SCL pin = white --> A5

// for automatic mode with photo resistors
int PRs = A0;                      //Photorisitor South with 100K Ohm resistor
int PRsVal;
int PRn = A1;                      //Photorisitor North with 100K Ohm resistor
int PRnVal;
int PRe = A2;                      //Photorisitor East with 100K Ohm resistor
int PReVal;
int PRw = A3;                      //Photorisitor West with 100K Ohm resistor
int PRwVal;
int dv = 100;                       //delay 100
int dif = 50;                       //start adjusting when difference is greater than ...
int night = 300;                    // constant for night modus
int cloudy = 500;                   // constant for cloudy modus
int time = 10;                      // constant for time delay in automatic modus
int count = 0;                      // counter for time delay in automatic modus

String com = "Hello Day!";
String dir = "Lets chase the Sun";
String lev = "and Level up!";

// for manual mode with joystick
int xWert;
int yWert;

// for motor control via WSDC2412D (dual DC Motor Controler for driving wheels)
int enA = 9;                        //Motor speed left PWM
int in1 = 8;                        //Motor direction left
int in2 = 13;                       //Motor direction left
int enB = 10;                       //Motor speed right PWM
int in3 = 12;                       //Motor direction right
int in4 = 11;                       //Motor direction right

int motLSpeed, ml;
int motRSpeed, mr;

// for motor control via IBT2 (single DC Motor Controler for Linear Actuator)
int IBT2a = 6;                       //PWM up
int IBT2b = 7;                       //PWM down

// for PMW frequency change
int myEraser = 7;                     // this is 111 in binary and is used as an eraser
int myPrescaler = 2;                  // this could be a number in [1 , 6] [2] is equal to 4000Hz on Pins 2,3,5,6,7,8,9,10,11,12 and 7800Hz on Pins 4,13

This is the complete void setup ()...

void setup() {
  Serial.begin (9600);
  pinMode (modePin,INPUT);          // for switch control automatic vs manual mode
  pinMode (LEDPin,OUTPUT);          // for switch control automatic vs manual mode
  pinMode (PRs,INPUT);              // for automatic mode with photo resistors top left
  pinMode (PRn,INPUT);              // for automatic mode with photo resistors top right
  pinMode (PRe,INPUT);              // for automatic mode with photo resistors bottom left
  pinMode (PRw,INPUT);              // for automatic mode with photo resistors bottom right
  pinMode (enA, OUTPUT);            // for motor control via WSDC2412D
  pinMode (enB, OUTPUT);            // for motor control via WSDC2412D
  pinMode (in1, OUTPUT);            // for motor control via WSDC2412D
  pinMode (in2, OUTPUT);            // for motor control via WSDC2412D
  pinMode (in3, OUTPUT);            // for motor control via WSDC2412D
  pinMode (in4, OUTPUT);            // for motor control via WSDC2412D
  pinMode (IBT2a, OUTPUT);          // for motor control via IBT2
  pinMode (IBT2b, OUTPUT);          // for motor control via IBT2

  analogWrite(enA, 0);              // Set 0 at start for safety
  analogWrite(enB, 0);              // Set 0 at start for safety
  analogWrite(IBT2a, 0);            // Set 0 at start for safety
  analogWrite(IBT2b, 0);            // Set 0 at start for safety

  attachInterrupt(digitalPinToInterrupt(modePin),checkSwitch, CHANGE);  // Attach Interrupt to Interrupt Service Routine

  TCCR2B &= ~myEraser;              // this operation (AND plus NOT), set the three bits in TCCR2B to 0 (timer 2 controls pin 10, 9);
  TCCR4B &= ~myEraser;              // this operation (AND plus NOT), set the three bits in TCCR4B to 0 (timer 4 controls pin 8, 7, 6);
  TCCR2B |= myPrescaler;            // this operation (OR), replaces the last three bits in TCCR2B with our new value
  TCCR4B |= myPrescaler;            // this operation (OR), replaces the last three bits in TCCR4B with our new value

Try:

  digitalWrite(ena, LOW);           // <-----<<<<
  pinMode (enA, OUTPUT);            // for motor control via WSDC2412D

  digitalWrite(enB, LOW);           // <-----<<<<
  pinMode (enB, OUTPUT);            // for motor control via WSDC2412D

  digitalWrite(in1, LOW);           // <-----<<<<
  pinMode (in1, OUTPUT);            // for motor control via WSDC2412D

  digitalWrite(in2, LOW);           // <-----<<<<
  pinMode (in2, OUTPUT);            // for motor control via WSDC2412D

  digitalWrite(in3, LOW);           // <-----<<<<
  pinMode (in3, OUTPUT);            // for motor control via WSDC2412D

  digitalWrite(in4, LOW);           // <-----<<<<
  pinMode (in4, OUTPUT);            // for motor control via WSDC2412D

  digitalWrite(IBT2a, LOW);         // <-----<<<<
  pinMode (IBT2a, OUTPUT);          // for motor control via IBT2

  digitalWrite(IBT2b, LOW);         // <-----<<<<
  pinMode (IBT2b, OUTPUT);          // for motor control via IBT2

Hi Larry,
thanks a lot. I´ll try tomorrow :smiley:

Have a good night

Ben

nope, that didn´t work...

void setup() {
  Serial.begin (9600);
  pinMode (modePin,INPUT);          // for switch control automatic vs manual mode
  pinMode (LEDPin,OUTPUT);          // for switch control automatic vs manual mode
  pinMode (PRs,INPUT);              // for automatic mode with photo resistors top left
  pinMode (PRn,INPUT);              // for automatic mode with photo resistors top right
  pinMode (PRe,INPUT);              // for automatic mode with photo resistors bottom left
  pinMode (PRw,INPUT);              // for automatic mode with photo resistors bottom right
  digitalWrite(enA, LOW);           // Set 0 at start for safety
  pinMode (enA, OUTPUT);            // for motor control via WSDC2412D
  digitalWrite(enB, LOW);           // Set 0 at start for safety
  pinMode (enB, OUTPUT);            // for motor control via WSDC2412D
  digitalWrite(in1, LOW);           // Set 0 at start for safety
  pinMode (in1, OUTPUT);            // for motor control via WSDC2412D
  digitalWrite(in2, LOW);           // Set 0 at start for safety
  pinMode (in2, OUTPUT);            // for motor control via WSDC2412D
  digitalWrite(in3, LOW);           // Set 0 at start for safety
  pinMode (in3, OUTPUT);            // for motor control via WSDC2412D
  digitalWrite(in4, LOW);           // Set 0 at start for safety
  pinMode (in4, OUTPUT);            // for motor control via WSDC2412D
  digitalWrite(IBT2a, LOW);         // Set 0 at start for safety
  pinMode (IBT2a, OUTPUT);          // for motor control via IBT2
  digitalWrite(IBT2b, LOW);         // Set 0 at start for safety
  pinMode (IBT2b, OUTPUT);          // for motor control via IBT2

  attachInterrupt(digitalPinToInterrupt(modePin),checkSwitch, CHANGE);  // Attach Interrupt to Interrupt Service Routine

  TCCR2B &= ~myEraser;              // this operation (AND plus NOT), set the three bits in TCCR2B to 0 (timer 2 controls pin 10, 9);
  TCCR4B &= ~myEraser;              // this operation (AND plus NOT), set the three bits in TCCR4B to 0 (timer 4 controls pin 8, 7, 6);
  TCCR2B |= myPrescaler;            // this operation (OR), replaces the last three bits in TCCR2B with our new value
  TCCR4B |= myPrescaler;            // this operation (OR), replaces the last three bits in TCCR4B with our new value
}

I´ve also unplugged the shield and everything else and plugged in the Contorller directly to the Mega. Still the same result...right motor starts spinning off...

Any other ideas of how to solve that issue?

Have a nice sunday

Ben

I had an success but still don´t understand it.

I changed the pins to 2-7 and now the spinning wheels is gone when I power on the Mega.

On the Uno I had the same issue with the pins 8-13.

So there must be an hardware related issue / bug / feature on that pins...

I would be still interessted to understand this problem so if anybody has an idea, it would be great.

Cheers Ben

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