I have 2 DC motors (well, 4; but each pair is controlled by the same "signals") connected via a L298N, which is connected to the Arduino. In addition, I have a servo motor connected to the Arduino.
When I run the following program (in reality, it's a bigger program; however, I've managed to chiseled it down to the following):
#include <Servo.h> const int servoPin = 3; Servo servo; const int LMotorsSpeedPin = 5; const int RMotorsSpeedPin = 10; const int LMotorDirectionBit1Pin = 7; const int LMotorDirectionBit2Pin = 6; const int RMotorDirectionBit1Pin = 8; const int RMotorDirectionBit2Pin = 9; const int MotorSpeed = 150; void setup() { // 1 pinMode(servoPin, OUTPUT); servo.attach(servoPin); // 2 pinMode(LMotorsSpeedPin, OUTPUT); pinMode(RMotorsSpeedPin, OUTPUT); pinMode(LMotorDirectionBit1Pin, OUTPUT); pinMode(LMotorDirectionBit2Pin, OUTPUT); pinMode(RMotorDirectionBit1Pin, OUTPUT); pinMode(RMotorDirectionBit2Pin, OUTPUT); // 3 digitalWrite(LMotorDirectionBit1Pin, LOW); digitalWrite(LMotorDirectionBit1Pin, HIGH); analogWrite(LMotorsSpeedPin, MotorSpeed); // 4 digitalWrite(RMotorDirectionBit1Pin, LOW); digitalWrite(RMotorDirectionBit1Pin, HIGH); analogWrite(RMotorsSpeedPin, MotorSpeed); // 5 delay(10000); } void loop() {} 1Sets up servo.2Sets up L298N pins.3Sets up left motor data.4Sets up right motor data.5Tests the code above for 10 [s].
I assume that you'll need some experience with the L298N; however, here's the gist.
*MotorsSpeedPin sends each motor what speed (MotorSpeed 0-255) they must go (via PWM). Data into a pair of *MotorDirectionBit*Pin tell each motor what direction to go (i.e. back 01, stop 00 11, right 10).
When I run the program above, only the left motor runs. However, if I comment out 1 (which essentially renders the servo useless), both left and right motors start to work.
I find this problem quite odd. What does the servo code have to do with the motor code (and only one at that!)?