# Driving a motor freezes Arduino

I am trying to drive a Carrera Digital car (brushless DC motor) using an Arduino and an L293D h-bridge motor driver. My schematic is the following:

I have also connected an IMU (GY-521 MPU-6050) to the Arduino, and the "Arduino" I'm using it's a chinese "Arduino Nano". The Arduino has an isolated power supply, but the ground is connected with the ground of the 14.5V track.

The problem I'm having is that the Arduino freezes and stops responding after a few seconds attached to the track. If the 14.5V power supply isn't connected, the program runs OK for as long as I want. However, once I connect the 14.5V power supply, it freezes after 5-60 seconds.

When the Arduino freezes, it's still ON and outputs the last PWM signal it was instructed to. However, it doesn't run any additional code (Serial doesn't print, leds don't change, ...).

This is a snippet of the code:

#include "MPU6050.h"
#include "Wire.h"

const int controlPin1 = 2;
const int controlPin2 = 3;
const int enablePin = 9;

int ax, ay, az;

void setup(){
pinMode(controlPin1, OUTPUT);
pinMode(controlPin2, OUTPUT);
pinMode(enablePin, OUTPUT);

digitalWrite(controlPin1, HIGH); // So it turns clockwise
digitalWrite(controlPin2, LOW);

Serial.begin(9600);
Wire.begin();
mpu.initialize();
}

void loop(){
for(int motorSpeed=100; motorSpeed<255; motorSpeed=motorSpeed+10){
analogWrite(enablePin, motorSpeed);
delay(500);
}

mpu.getAcceleration(&ax, &ay, &az);
Serial.println(ax);
}


I think that this is a hardware issue related with the noise introduced by the motor or something like that. (The Carrera Digital track has signals on top of the 14.5V, but I have tested the system using an external power supply instead of the track, and it still freezes).

I tried adding a capacitor between the motor pins and another one between Arduino's VCC and GND, but it didn't work. Adding more capacitors seems to make the program last longer, but it eventually freezes.

Lowering the frequency of the PWM output seems to help a little and makes the program last longer. But it eventually freezes.

Using a mosfet instead of the L293D driver doesn't fix the problem.

If it's a noise problem, I thought about using a circuit like this, with an optocoupler, in order to isolate the Arduino even more, but I don't know if it will work:

I didn't attempt to remove the IMU (maybe the noise is entering through the IMU), but I think that it would have no difference.

Thanks in advance for all the help (And yes, I have tried adding more and more capacitors to the motor pins).

Edit:

After that many attempts to solve the problem, the soldering is very messy. However, here are some images of the setup:

The 14.5V cables are the red and black one from the top. The IMU has the purple, gray, blue and green cables of the top-right corner. The blue and gray cables of the top-left corner are the motor ones. And, on the top, there is a IR sensor with red, black and white cables. Hope this helps.

• Right now I don't see how your opto can turn off the MOSFET even if it didn't freeze. The 4N35 doesn't have any way to discharge the gate-source capacitance of the MOSFET to turn it off. It can only ever charge it. Is the code actually able to turn the motor off after it has been turned on? Doesn't really explain why the serial or LEDs stop though. – DKNguyen Nov 4 '19 at 22:32
• classic "EMI issues". Use heavy grounds and shielded or twisted pairs for all interface wires. Search for EMI solutions in search window. . Unused inputs. decoupling caps, motor current wire orientation, etc etc. Ground shift errors.... – Tony Stewart Sunnyskyguy EE75 Nov 4 '19 at 22:58
• Might be a problem with how your power wires are routed. Please show us a photo of your setup. – Bruce Abbott Nov 4 '19 at 23:34
• I don't see any decoupling capacitors. That by itself is very bad. In presence of an EMC source it is fatal. – Oldfart Nov 4 '19 at 23:44
• Reverse diode across motor. LARGE caps on both the motor supply and processor supply. Also small capas (0.1 uF OK) AT processor on VCC pin and AT L293 AT VCC pins. Possibly add a series R in motor lead with large cap on supply side of R (or one on each side. Aim is to keep motor noise in motor circuit. | Inductor (nise filter) in 5V lead to Arduino VCC. – Russell McMahon Nov 5 '19 at 8:31