Motor Drives Pro mini crazy

I have a project that basically takes input from some pushbuttons and if the combination is correct it drives a DC motor in one direction for one second, then waits, then drives it in the other direction for one second. This project works fine without the motor attached, but once the motors are attached the pro mini does erratic things. I have disabled the regulator and LED on the pro mini as this is a battery-operated application and therefore needs ultralow power. The wires on the motor feed are twisted and only about 4 inches long.
Any suggestions?

unsigned long pTime=0; //timer start compared to mTime/millis() time for long keypress 
unsigned long mTime=0; // holds value from millis() time for long keypress
unsigned long SpTime=0; //Session timer start compared to mTime/millis() time
unsigned long SmTime=0; // holds value from millis() time for session
int but1C=0;      // button 1 counter
int but2C=0;
int but3C=0;
int but1;
int but2;
int but3;
int unlock=0;
const int longPress=1500; //time needed to press all 3 buttons at end of key sequence to unlock (@16mhz)
const int timeRLYon=1000; //leave on relay time to run motor either direction 
const int openDoorTime=2000;  // time between unlock relay run and lock run
const int sessionTime=8000; //once woke, time to run script and enter door code before sleep
#include <LowPower.h>

void wakeUp(){
    // Just a handler for the pin interrupt.
}

void setup() {         // put your setup code here, to run once:
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
    Serial.begin(9600);
  }

void loop() {         // put your main code here, to run repeatedly:

 
 if (SmTime-SpTime >= sessionTime){
   
    attachInterrupt(0, wakeUp, LOW);    // Allow wake up pin 0 to trigger interrupt on LOW.
   
    LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF); // Enter power down state with ADC and BOD module disabled.
   
    detachInterrupt(0);    // Disable external pin interrupt on wake up pin.
   //delay(5);
   //but1C=0; //when wake, 0 out button counters and:
   //but2C=0;
   //but3C=0;

    SmTime=millis(); // move clock to SmTime 
   SpTime=SmTime;   // move clock time (millis/SmTime) to SpTime for comparison
  
  }
  but1=digitalRead (2);  //read button state every clock cycle
  but2=digitalRead (3);
  but3=digitalRead (4);

  if ((but1==0) && (but2==1) && (but3==1) && (but1C==0)){  //if but1 and only 1 LOW, increment but1C and wait for next keypress
   but1C=1;
   but2C=0;
   but3C=0;
 
 }  

 if ((but1== 1) && (but2==1) && (but3==0) && (but1C==1)){  //if but3 pressed not but2, start over
   but1C=0;
   but2C=0;
   but3C=0;
 }  

 if ((but1== 1) && (but2==0) && (but3==1) && (but1C==1)){  //if but2 pressed proceed
   but1C=0;
   but2C=1;
   but3C=0;
 }  

  if ((but1==1) && (but2==1) && (but3==0) && (but2C==1)){  //if but3 pressed proceed
   but1C=0;
   but2C=0;
   but3C=1;
  } 

 if ((but1== 1) && (but2==0) && (but3==1) && (but3C==1)){  //if but2 is pressed instead of all 3 start over
   but1C=0;
   but2C=0;
   but3C=0;
 }  
 
 if ((but1==0) || (but2==0) || (but3==0) && (but3C==1 ) && (unlock==0)){ //allow time for all 3 buttons to make contact before next
  //delay (500);
 }
 if ((but1==0) && (but2==0) && (but3==0) && (but3C==1 ) && (unlock==0)){  //if all 3 buttons pressed set unlock to 1 and:
   pTime=mTime; //move mTime (millis) to pTime for comparison with mTime below
   SpTime=SmTime; // restart session time counter
    unlock=1;
 }  

 if ((but1==1 || but2==1 || but3==1) && (unlock==1) && (mTime-pTime < longPress)){ //if all buttons not held for longPress set unlock=0 and start over
     unlock=0;
   but1C=0;
   but2C=0;
   but3C=0;
 } 

 if ((but1==0) && (but2==0) && (but3==0) && (but3C==1) && (mTime-pTime >= longPress) && (unlock==1)){  //if all buttons held for >= longPress 
   digitalWrite(5, HIGH);                                                                          //then start open door sequence
    delay(timeRLYon);
   digitalWrite(5, LOW);
    delay (openDoorTime);
   digitalWrite(6, HIGH);
    delay(timeRLYon);
   digitalWrite(6, LOW);
   but1C=0;
   but2C=0;
   but3C=0;
   unlock=0;
 }  

 delay(20);
  mTime=millis();  //move clock current time to mTime
  SmTime=millis();
//delay (10);
 Serial.print ("but1C");
 Serial.print (" ");
 Serial.print ( but1C);
 Serial.print ("   ");

 Serial.print ("but2C");
 Serial.print (" ");
 Serial.print ( but2C);
  Serial.print ("   ");


 Serial.print ("but3C");
  Serial.print (" ");
 Serial.print ( but3C);
   Serial.print ("   ");

 Serial.print ("Unlock");
 Serial.print (" ");
 Serial.println ( unlock);
 
  } //loop end

what kind of battery?

2 Likes

I cannot say for sure that the the problem is EMI but conversely I cannot say it is not without more information. Most MOS logic is fast and definitely edge sensitive. Keeping your leads (they are antennas) connected to the processor less then 10"/25cm which you did is good. What is needed is a description and or links to technical information. The power source could be collapsing or it could be coupling noise to the processor. The battery is important.

1 Like

Diodes across the relay coils, but not across the motor.
Use two diodes from each motor terminal to VCC and two diodes from each terminal to ground.
Or a bridge rectifier, two AC terminals to motor, + to VCC, - to ground.

Not sure why you have used four relay contact pairs, while two would do.
Leo..

Circuit is driven by three AA batteries so 4.5 V.

I tried the bridge fix and that did not fix the problem, about the same.

I tried putting the motor power on one power source and the mini and relays on another and that did fix the problem.

I tried running the motor continuously on one power source and putting it right next to the mini, then put the mini and relays on another power source and circuit worked fine.

So I'm guessing that the noise is coming down the wire, not over the air.

I am using 4 contact relay pairs because there is another device driving the motor when this circuit is not in play and it needs to be isolated from this circuit.

Any other ideas for filtering options?

The diodes across the motor are also there to prevent relay contact burn, so use them.

Motor and MCU on the same (weak battery) supply is usually a bad idea. Voltage could dip briefly when motor uses more current during start-up (stall current), which could upset the MCU.

You can try powering the ProMini through a filter, consisting a Schottky diode in series and a ≥1000uF cap on the MCU side.
Leo..

I would use optocouplers to drive the transistor bases, thus isolating the processor from any ground loops that could exist and also shorten the wiring from the battery to the processor, also filter the processor power.

Sounds like all good ideas, how about replacing the relay drive transistor with a small 2A version SSR like this?

space is very limited on this project

Are you using the 5V 16MHz pro mini?
If so you need at least 3.8V for reliable operation, 4V would be a good margin
So 3AA batteries can be anywhere between 4.5V and 3V while they are discharging and may drop below the 328P BOD voltage when a motor turns on

I have both versions and can use either, but was planning on using the 3.3v version.
I am concerned that with low battery voltage I want the circuit to still function.
and as such I am using 3.3v mechanical relays.
without the voltage regulator in circuit what is the difference besides clock speed in the 2 versions?

sorry jim-p I was typing while you were.
The motor I am using only draws 22ma @4.5v unloaded and it will have a very small load on it when functioning. I understand start up currents can be much higher.
I suppose once I have this working I should test with some dead batts!

As far as I know nothing. The only ICs on the board are the regulator and the 328P

@mcuboy
The relays probably draw more that the motor, looks like maybe 120mA at 3.3V, more at 4.5V
That's your problem!

correct 110ma measured at 4.5v, So back to my question of, replacing the drive transistor with just the SSR?
so output of Pro Mini drives SSR which drives relay.
this gives me the isolation and smaller component count.

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