Arduino freeze while working with MOSFET and Compass

Hi,

I'm using Arduino Nano (3v, ATmega328) .

Trying to read Compass data (HMC5883L sensor) by this library : Arduino and HMC5883L (With Auto Calibration)
This library using Analog pins 4 & 5 (SDA and SCL), and "Wire.h" lib.

And moving a motor by MOSFET and Digital pin ( analogWrite() ) on pins 8 & 9 (PWM).

Also i'm reading times by the function millis() .

When I'm running my code the arduino hangs and freeze, not in one special place in code.
BUT when I put comments at analogWrite to the MOSFET, it does NOT freeze - so this eliminates the option that I have bugs.

The values that I send to the PWM pins are 100-200.

I'm using extra battery for the MOSFET.

Please help me to solve my problem,
Thanks!

Welcome to the Forum. Please read the two posts

How to use this forum - please read.
and
Read this before posting a programming question ...

It's impossible to determine your problem without seeing all of your code. Please read the two posts at the top of this Forum on guidelines for posting here, especially the use of code tags which make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don't do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons.

Many questions can be answered by simply reading the documentation which is provided with the IDE, available under the help tab, or online here.

There are many other things that programmers do to make their code understandable. Please do them, as a courtesy to the members who volunteer their time to help you here. One is to use a standard indentation to clearly show the code blocks. Never put more than one statement per line. Place any brackets by themselves on a separate line. Before posting the code, use Ctrl-T in the IDE to reformat the code in a standard format, which makes it easier for us to read.

This is the main idea of my code:

#include "Wire.h"
#include "compass.h"

#define RightWheelDigitalPin 9
#define LeftWheelDigitalPin 10


void setup(){
  pinMode(RightWheelDigitalPin, OUTPUT);
  pinMode(LeftWheelDigitalPin, OUTPUT);
  Serial.begin(115200);

  Wire.begin(); // join I2C bus (I2Cdev library doesn't do this automatically)

  compass_init(2);
  
  Serial.println("OK");
}


void loop(){

 ...
 
 if( Status == Forward ) {
 finished=millis();
 elapsed = finished - start;
 if (elapsed > wantedTime) {
 // Stop Forward
 WheelPower(0,0);
 Status = Stop;
 Serial.println("*** STOPED ***");
 }
 else {
 // Forward
 CalculateHeading();   
 WheelPower(200, 200);
 }
 }
}

void WheelPower(byte r, byte l) {
 analogWrite(RightWheelDigitalPin, r);
 analogWrite(LeftWheelDigitalPin,  l);
}

void CalculateHeading() {
 compass_scalled_reading();
 compass_heading();
}

When I commented the WheelPower function - everything works just fine.

We need to see the circuit you are using to drive those MOSFETs and motors - sounds like you have
severe interference problems - this is a hardware issue I think.

Are you really trying to drive a motor with a coin cell battery?

no.. :sweat_smile: the sketch program has no other battery, I'm using 3.7v battery of some quad copter.

the sketch program has no other battery,

That is why Fritzing is hated here. If you do not use the correct symbols it is like lying. You are much better off just drawing it with a pencil and paper.
What else on that diagram is not true?
Pull up resistors on the I2C lines? Where are they?

A motor generates lots of interference so it needs to be suppressed with capacitors. Also there is no flyback diode on that diagram, that will cause large amounts of interference.

Look at decoupling:-
http://www.thebox.myzen.co.uk/Tutorial/De-coupling.html
And also the attached about capacitors.

Don't forget the diode.

solder_caps.pdf (299 KB)

There is no gate resistor on the mosfet, there is no free-wheel diode across the motor, its
not clear if there is any decoupling on the motor supply...

MarkT:
There is no gate resistor on the mosfet, there is no free-wheel diode across the motor, its
not clear if there is any decoupling on the motor supply...

Since a MOSFET is a voltage controlled device is the resistor needed here?

MOSFET gate looks like a discharged or a fully charged cap to an IO pin. The resistor limits the current rush into the cap:
5V/.025A = 200 ohm or larger.

You could also do with a 10K gate pull down resistor.

Since a MOSFET is a voltage controlled device is the resistor needed here?

A power MOSFET gate is very capacitive - large currents flow to charge this up if you
don't limit them this could (in the worst case) overload the output transistors of the
Arduino.

Typical power MOSFETs have effective gate capacitances in the 1 to 30nF range, whereas
logic signals are designed to drive 30 to 100pF normally.

You'll find in industry MOSFETs are usually driven with a MOSFET driver chip whose
function is to boost logic signal to 100's or 1000's of mA, so MOSFETs can be switched
at their full speed.

Thanks for the resistor info Crossroads/MarkT.

I had read since they are voltage controlled devices that they could be fed directly. I've been reading quite a bit trying to get back up to speed in the past couple months so I don't know exactly the source. I do remember this source talking more about the difference between gate and Source/Drain being the key factor so adding much resistance would probably cause it not to turn on.

I guess I'll have to read a data sheet later and see if that sorts it out for me. Thanks again guys.

outofoptions:
I had read since they are voltage controlled devices that they could be fed directly.

Yes, but don't feed them after midnight. Oh wait, that's something else.

I'm not sure what you consider "directly", but a current limiting resistor is still "direct" in my book.

outofoptions:
so adding much resistance would probably cause it not to turn on.

Well yes, of course. Too much resistance would be a problem. But if 200ohms limits the current so much that the gate capacitance can't be overcome, then you can NOT use that FET with an Arduino. Of course, I'm not aware of a practical situation where this is a limitation. That MOSFET would be useless in a practical application.

outofoptions:
I guess I'll have to read a data sheet later and see if that sorts it out for me.

What do you think you'll be looking for in the datasheet?

[quote author=James C4S date=1450632233 link=msg=2529159]
What do you think you'll be looking for in the datasheet?[/quote]
The article/whatever I remember reading indicated that turn on voltage for a MOSFET was pretty close to full applied voltage across source/drain. Thinking this through I guess that after the capacitive phase the voltage drop across the resistor would be negligible because of the extremely low leakage current of a MOSFET?

outofoptions:
Thanks for the resistor info Crossroads/MarkT.

I had read since they are voltage controlled devices that they could be fed directly. I've been reading quite a bit trying to get back up to speed in the past couple months so I don't know exactly the source. I do remember this source talking more about the difference between gate and Source/Drain being the key factor so adding much resistance would probably cause it not to turn on.

The only effect resistance on the gate has is slowing down the turn on/turn off - the gate capacitance and
the gate resistor form an RC circuit. The gate is isolated from the rest of the device by silicon
dioxide (ie quartz), albeit a very very thin layer. Leakage currents are small (nA to uA for a power
MOSFET).

However if you are switching at high freq (say 100kHz PWM, for instance), then it is vital that the device
is switched fast, and power MOSFETs can switch in 30 to 100ns when driven hard, so you drive the gate
hard (with a MOSFET driver, maybe 0.5A or more). If you are only switching at DC or a kHz or so, then
switching losses are less critical and you can afford to switch slowly with a few 10's of mA.

If you are driving very high power (particularly high voltage) then you need a good strong gate driver
to prevent gate voltage going out-of-range due to drain-gate capacitance feeding back the high dV/dt on the
output to the gate. Otherwise the high voltage swings will destroy the gate oxide and whatever is driving
the gate. Thus you wouldn't switch 1kW at 100V using a logic level MOSFET directly driven by an Arduino,
you would be asking for everything to explode (yes, MOSFETs explode when they fail at high current, eye
protection is wise).

I guess I'll have to read a data sheet later and see if that sorts it out for me. Thanks again guys.

One thing to avoid being confused by is Vthr (threshold voltage)- this is all to do about when the
device turns off, not on. A device with a Vthr of 2--4V invariably needs 10V to turn on, not 4V.