NewPing Library: HC-SR04, SRF05, SRF06, DYP-ME007, Parallax PING))) - v1.7

crispy:

teckel:
The ATmega328 has limited timers (3, but Timer0 is used for millis and delay so you really only have 2 available). When doing a project where motors and servos are involved, you're really cutting it close when using an ATmega328. You can still do it, using a serial motor controller or I2C motor controller (which are cheap, small, use fewer pins and allow for control of many motors using multiple controllers). Or, you can use a Teensy/Leonardo (ATmega32u4) or ATmega2560 based microcontroller systems that have additional timers/PWM pins.

I looked into the motor controllers you linked, and frankly serial communication is something I'd rather avoid if possible on this project. I'm trying to keep the scope somewhat limited so I actually have a chance of getting the whole thing to work.

I've looked at my pinout list and I show 2 pins (5 and 6) will still have PWM available even if I use both the Servo and NewPing libraries. I will also have enough pins free to drive a conventional motor shield. So this is the route I would prefer to go. However, I haven't found much in the way of discussion about using the "official" motor shield un-stacked from the Arduino.

The serial and I2C suggestions also solves the problem with needing more than 2 motors (you can daisy chain many of these controllers for control of many motors using only 2 Arduino pins in total). When you want to control 4 motors as well as doing anything else, it's a real challenge with the limited pins of the ATmega328. There's other advantages also, like ultrasonic PWM to avoid motor hum and whine.

If you only want motor control and want to use the Arduino PWM pins to control speed, you can use the TB6612FNG Dual Motor Driver Carrier which uses the same TB6612FNG MOSFET-based H-bridge as the serial controller but without serial control (you control it directly with Arduino pins). The TB6612FNG is a better H-bridge than the L298N used in the Arduino motor shield (it's also very cheap). While the Arduino motor shield says it can take 2A, the L298N really can't deal with that well and gets very hot. The TB6612FNG is rated for 1A with 3A max, but it can actually handle two motors going at 1A each at the same time as well as 3A spikes without causing a fire like the L298N will try to do. If your motors are even close to a 1A rating, the TB6612FNG would be a better option than the L298N.

Or, you can hack up your motor shield to use pins 5 and 6. I'd probably cut traces and wire new jumpers to keep the shield basically as-is.

Tim