Hi all! I'm new on arduino's world and i'm trying to build my first project. A basic avoiding obstacles robot made with 2 motors + Arduino one + Arduino motor shield + Sharp 0A41SK as a proximity sensor.
I'm using the GP2Y0A21YK library to control the sensor.
But I have an odd problem that i cannot undestand:
If I turn off the motors, sensor seems to work well, I tried something like that:
void loop() {
// read the value from the sensor:
distance = Dist.getDistanceCentimeter();
if (distance>44 && statusmotor==0) {
//Motor A
digitalWrite(12, LOW);
digitalWrite(9, LOW);
//analogWrite(3, 128);
//Motor B
digitalWrite(13, LOW);
digitalWrite(8, LOW);
//analogWrite(11, 128);
statusmotor=1;
Serial.print("GO AHEAD:");
Serial.print(distance);
Serial.print("\n");
}
if (distance<=44 && statusmotor==1) {
digitalWrite(9, HIGH);
digitalWrite(8, HIGH);
// turning a little bit
//Motor B backward
digitalWrite(13, HIGH);
digitalWrite(8, LOW);
//analogWrite(11, 128);
statusmotor=0;
Serial.print("TURNING BACK: ");
Serial.print(distance);
Serial.print("\n");
}
... with this code I can check via serial output that proximity sensor works, i tested it with my hand and no problem, but if I uncomment the analogWrite lines (to make both motors run) seems that proximity sensor goes crazy, returning odd distance values jumping all the time from forward to turning backwards state without sense.
I'm totally lost, anybody knows where or what is the problem? maybe some code that helps me?
It is not code it is the interference from the motor that is causing problems. You supply needs to be decoupled with large capacitors. http://www.thebox.myzen.co.uk/Tutorial/De-coupling.html
It is a common problem.
There is also a chance that your supply cant provide enough for the motors that happens sometimes as well.
Grumpy_Mike:
It is not code it is the interference from the motor that is causing problems. You supply needs to be decoupled with large capacitors. http://www.thebox.myzen.co.uk/Tutorial/De-coupling.html
It is a common problem.
There is also a chance that your supply cant provide enough for the motors that happens sometimes as well.
Thanks a lot!! I'll read the tutorial, as you say, I also noticed that when the device was attached to the computer usb seems to work 'less crazy' (not at all) than when it runs with 9V battery ('totally crazy'), I don't know if it is also common. I'm a programmer and not electronic expert, but I'm trying to learn
Do you know any electronic schema for that specific configuration (9DV battery powering 2 Motors+proximity sensor) to avoid de-coupling?
I'm reading about capacitators de de-coupling and I don't undestand why arduino motor shield capacitators don't work to do the de-coupling. Maybe I must power arduino and the motor shield separately cutting the Vin connector?
Decoupling is not a one shot fixes all soloution, it is an art, you need to have enough to combat your problem. Capacitors are not the only wepon, you can use inductors as well and you have to in stubborn cases.