Robot Motor Power Issues

I’m working on making a robot that combines functionality for a motor, servo, and range sensor. I’m getting some odd results and I was hoping for some expertise from someone on the forum.

I actually think this might be more of a hardware issue than an XOD issue but I’m not sure. I’m using a robot I bought on Amazon that uses an Adafruit Motor Shield.

I’ve programmed the robot to use the range sensor to detect objects and turn away from them. When I’ve tested it it seems to work fine while connected to my computer via USB. However when I disconnect the USB the range sensor logic no longer seems to work properly. I’ve tried adding a seperate 6V power supply for the Arduino so that the motor shield and controller each have their own power. I’ve also tried brand new batteries.

Is there any reason XOD would not function without being connected via USB?

Any suggestions on managing power? Should I add a 3rd battery pack? Or should I look into a newer motor shield?

Here’s the attached xodball project if anyone wants to take a look.
fullrobot.xodball (58.1 KB)

I’m assuming the watch node doesn’t cause problems when USB is not connected…

What is happening that you don’t think the range sensor is working correctly?

Note that the ultrasonic range-finder code will not always return valid distance if you try to ping too often. It will just keep the old value until an internal timer expires after 60ms. Probably not an issue for you since you are waiting 0.4 seconds for ping value, but might be if you ping while servo is still in motion (so you don’t receive echo), then your 0.4 time expires before you get a valid update. You might also get invalid distances if the distance gets updated while you are turning (since your range-finder changes directions it is looking for ping response). A more fool-proof method would be to use a delay timer to move the servo, then pulse the range-finder to update & delay long enough for it to get a response, then process the distance returned; this still doesn’t help if the robot is turning while you are pinging…

Another issue I ran into was the PWM for one motor would quit working when I activated the servo. I believe this was related to a timer conflict, and the problem went a way when I moved the motor to a different PWM pin. If this is your problem, it wouldn’t work while USB is connected, though.

From what I can determine from your code, you spend 0.4 of every 1 second moving forward at 1/2 speed. If the sensor detects something too close or too far, you spend the remaining 0.6 seconds still moving forward 1/2 speed. Even if there is nothing in front of robot, you will turn left or right if something is within the specified distance when you check left or right. If there is something within the specified distance when you check in front, then you will spin at full speed for 0.6 seconds.

Hello, @warner ! Did you succeed in solving the issue?

To add to @gweimer thoughts, the USB cable should not affect the behavior anyhow except the power conditions change. You can try to measure the voltage between 5V pin and GND, is it close to 5 volts? The HC-SR04 is sensitive to power voltage when generating sound bursts. I remember it refused to work properly below 4.8 volts or so. Electric motor noise on the ground bus may affect it as well.

I would recommend to try:

  • Increase Vin voltage, that is use five-six battery cells instead of four
  • Put capacitors (hundreds of uF) between Vcc and GND of HC-SR04
  • Put unipolar capacitors between motor terminals

Thanks to both of your quick responses on this.

What is happening that you don’t think the range sensor is working correctly?

When USB is connected the motors respond to something being near the range sensor. When it is disconnected the motors just run forward.

As for the structure of the project you’re correct on the functionality. It’s still a work in progress and I’ll definitely have to fine tune it some more. Thanks for your advice on range sensor. I’ll take a look at it once I can figure out the power issues.

Also I see you’ve worked up a similar project. Thanks for sharing - it’s been nice to be able to compare structure to see multiple ways to program within XOD.

@nkrkv I’ve not solved it yet but I’ll definitely try your recommendations and see how that works. I’ll post an update on progress soon.

Thanks again to both of you. I’m always impressed with the help from the community on the XOD forum!

I’ve had a chance to test the voltages now and as you anticipated they are low with 4 AA batteries. I was getting readings between 4.2 and 4.5V which is apparently enough to not get accurate values from the range-sensor.

I added a 5th battery to the pack and everything “works” properly now.

I put “works” because as @gweimer anticipated there are some issues with my structure of the XOD project. Right now when it turns the range-sensor is not getting a good reading so it continues turning too long. It still needs some debugging and I’ll continue to work on that. I’ll post my results once I get it figured out.