Digital Ground/Analog Ground, Arduino Gets Stuck!

I have a Arduino Mega 2560 connected to

L293D Motor Driver IC connected to two small size dc motors.
MPU-9150 using i2c
Xbee v1.1 arduino shield by seeed
Li-po battery, 7.4 volts, 2200 mah.
all components are tested separately, they all are working fine, the sensor readings showing data on serial monitor, the l293 drives the motors, xbee sends wireless serial data on pc, and li-po is also working fine.

i have checked the wiring alot of times. it all seems to be correct.

the problem arises when i connect all of them together, since i am using a single power source, i power the l293d with the Li-po and the Arduino also with the Li-po.

When the L293D is powered using Li-po and it starts drawing current from the battery, the serial monitor/arduino gets stuck. it doesnt show sensor readings anymore, while the dc motors start spinning. and they continue to spin, until i break connection with the battery.

without the l293d powered, the sensor readings are working fine, everything is going normal, as it should, but as soon as the l293d is connected, arduino is stuck..

i have searched alot of forums for this, found some info, perhaps something to do analog and digital ground? mpu-9150 is digital ground. so that is why it stops the arduino mega as the l293d starts drawing current. all hell breaks loose.

can anybody please help me with this? it is giving me headaches ever since!

Try this simple thing : Use separate power supplies :wink: :wink:
Power your arduino through USB and L293D from your Li-Po.. After this test tell us if you notice any changes :smiley: :smiley:

the problem arises when i connect all of them together

That sounds like you have a power supply / decoupling problem.

Can you power supply provide all the current asked of it. Measure the output voltage when it is all running, does it sag?

If that is OK then you need more supply decoupling.
http://www.thebox.myzen.co.uk/Tutorial/De-coupling.html

Mike i recently saw a post where is was suggested a decoupling capacitor to the point where power supply connects to the IC..
It sounds correct right?

are your GND's connected?

I have seen some odd behavior powering servos from batteries.. where the GND's were not connected to the Arduino as well..

Also what does 'stuck' mean? Like it doesnt boot? it does some odd behavior? get to certain point then wont respond any more?

i have tried using two sources.
one instance i connected the usb to arduino, and the lipo to the l293d.
PROBLEM PERSISTS

and then i connected the 9volt battery to arduino, and lipo to the l293d.
PROBLEM PERSISTS

and yes all my grounds are connected. i have checked, here is a diagram showing the circuit, what does decoupling capacitor mean?
where should i place it? Please on the diagram if someone can guide.

Maybe we should take a look at your code? :roll_eyes:

Talal:
it doesnt show sensor readings anymore, while the dc motors start spinning. and they continue to spin, until i break connection with the battery.

I read your initial post again, i think this doesn't seems to be a wiring problem!!!

without the battery to L298N connected, the code works fine. Dont know whats the problem.
thanks for replying.
Heres the code

integration1fail.ino (7.92 KB)

Talal:
without the battery to L298N connected, the code works fine. Dont know whats the problem.
thanks for replying.
Heres the code

So when L298N is not working.. If your code for movement is wrong it would be possible to be facing this problem...

previously i had L293D, but then i thought the current is limiting or somethings not working. so i switched to L298N motor shield.

i tried to RUN THE MOTORS separately, it worked. it goes forwards backwards, right or left. but with the sensor connected. although the MOTORS keep on running, the code stops, it just stops displaying on serial monitor, the led stops blinking, the motors are in a forever loop to run in one direction.

Inside your loop you are constantly calling the function DriveF(255); which means your motors will keep rotating forever.. :wink: :wink: :wink:
It was as simple as that.. Correct me if i am wrong XD XD XD

    //navigation
   DriveF(255);
   if(dist>=1)
      {
      STOP();
      delay(5000);
      }

Are you using an Android app to control your project?

yes true, but inside the LOOP function i also have the read accelerometer option, so what it should do is it should read the accelerometer, drive the robot, read accelerometer, drive the robot.

ATTACHED with this is the screenshot i took of the serial monitor, at dist:0.35 it STOPS the readings, no more real time data coming in. the motors keep rotating.

according to my if else condition, when dist=1 it should stop the motors.

and the moment it stops the readings is not fixed, it can be anywhere, its not reliable. this time when i ran it it stopped at dist=0.35.

On your code, you say:

 velf=veli+(average)*(dt);
    dist=veli*dt+(0.5)*(average)*(dt*dt);
    veli=velf;

What is the value of "veli" ? None, just a random float number because your variable is not initialized with a value:

float veli;

SO first you have to :
initialize veli
and then:

 velf=veli+(average)*(dt);
    dist=veli*dt+(0.5)*(average)*(dt*dt);
    veli=velf;

And what is that "veli" ?? :astonished:

oh, these are the three kinematic equations i used to integrate the values of acceleration to calculate the distance.

the equations are:
v=u+at
s=ut+1/2at^2

where u(initial velocity) becomes v(final velocity) after every iteration.

is there something wrong? maybe i need to initialize float veli=0?

because the algorithm seems to be fine..

When you initialize a variable like that : float veli; then the variable equals to a random float number..
Thats why your distance is not reliable.. Initialize it with zero? Really? Come on, then this

dist=veli*dt+(0.5)*(average)*(dt*dt);

|---->0
Will become zero too!!! 8) 8) 8) I cant understand the use of variable veli, just figure out the initial value of it...
Hope i helped, let me know if you have more problems...

Just test your code... I can't do that for you!!! XD XD XD

Talal:
what does decoupling capacitor mean?
where should i place it?

It means that your circuit will never work reliably without it.
Read:-
http://www.thebox.myzen.co.uk/Tutorial/De-coupling.html

So did you figure out your problem?

isolating the microcontroller,

it seems the current is being drawn by h-bridge through the microcontroller, for some reason in the world yet uknown, although only grounds are common, and PWM is provided by arduino, it seems current is being drawn.

thats why the serial monitor and arduino hangs.

i think i need to buy a optocoupler, to isolate the circuits.

I think we figured out why your motors spin forever... Because of wrong Code..!
The distance sensor is also not reliable because of bad programming too...!

Did you try to resolve anything of the above? You should go step by step.. You can't resolve everything at once !!!!

Still i cant understand what you mean by "arduino hangs" !!! What happens to arduino ? If serial monitor wont display anything this doesn't mean that arduino stuck !!! It could be part of bad programming too !!!

I would advise you to take the distance sensor out.. Write down a simple code just trying to move dc motors and leave anything not essential out...