Question about direct current motors

Greetings,
this might strike as a trivial question, but I did not find any info on this. I started my first project - car with ultrasonic sensor and two DC motors, that avoids obstacles in a simple manner. The problem Is that I have one Arduino Uno R3 and one Motor Shield L298P and I cannot make the two motors go the opposite directions at once. Is it because they are direct current, therefore I would need two separate circuits or am I doing something wrong?

I apologise if I didn't pick the right thread.

Thank you in advance for any responses.

It's probably because your code and/or your connections are wrong. But I can't see either of them.

Alternatively your motors are not well matched to the old L298 driver. Or maybe the power is wrong. But no details of the motor or how things are powered.

Steve

1 Like

Much more detail required

Which motor shield do you have ?
How is the Arduino powered ?
How are the motors powered ?

Please post your code and a schematic (a photo of a pencil and paper drawing is good enough)

The easier you make it to read and copy your code the more likely it is that you will get help

Please follow the advice given in the link below when posting code , use code tags and post the code here

If you get errors when compiling please copy them from the IDE using the "Copy error messages" button and paste the clipboard here in code tags

1 Like

Alright, I apologise again, not really experienced with how the questions and threads work, so here is the additional info:

I am using Deek-Robot Motor Shield L298P

Arduino is powered from a 9V rechargable battery, it is connected directly to the Motor Shield.

Motors are connected to the Motor Shield as well, more precisely through the A+/A- and B+/B- pins specified for the motors, so nothing is directly connected to Arduino.

The code is very simple:

#include <NewPing.h>

const int DirMotorA = 12;
const int SpeedMotorA = 3;
const int BrakeMotorA = 9;
const int DirMotorB = 13;
const int SpeedMotorB = 11;
const int BrakeMotorB = 8;

const int Clockwise = HIGH;
const int CClockwise = LOW;


#define trig_pin A1
#define echo_pin A2

#define maximum_distance 200
int distance = 100;

NewPing sonar(trig_pin, echo_pin, maximum_distance);


void setup() {

  pinMode(DirMotorA, OUTPUT);
  pinMode(SpeedMotorA, OUTPUT);
  pinMode(BrakeMotorA, OUTPUT);
  pinMode(DirMotorB, OUTPUT);
  pinMode(SpeedMotorB, OUTPUT);
  pinMode(BrakeMotorB, OUTPUT);

  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);

  Serial.begin(9600);
}

void loop() {

  delay(50);

  if (distance <= 10)
  {
    Serial.println("Sensor has detected a distance less than 10cm.");
    stopMove();
    delay(500);
    moveBackward();
    delay(1700);
    stopMove();
    delay(2000);
    turnRight();
    delay(1200);
    stopMove();
  }

  else
  {
    moveForward();
  }
  distance = readPing();
}


int readPing() {
  delay(70);
  int cm = sonar.ping_cm();
  if (cm == 0) {
    cm = 250;
  }
  return cm;
}

void stopMove() {

  digitalWrite(DirMotorA, LOW);
  analogWrite(SpeedMotorA, 0);
  digitalWrite(BrakeMotorA, HIGH);
  digitalWrite(DirMotorB, LOW);
  analogWrite(SpeedMotorB, 0);
  digitalWrite(BrakeMotorB, HIGH);
}

void moveForward() {


  digitalWrite(DirMotorA, Clockwise);
  analogWrite(SpeedMotorA, 100);
  digitalWrite(BrakeMotorA, LOW);
  digitalWrite(DirMotorB, CClockwise);
  analogWrite(SpeedMotorB, 100);
  digitalWrite(BrakeMotorB, LOW);
}

void moveBackward() {

  digitalWrite(DirMotorA, CClockwise);
  analogWrite(SpeedMotorA, 100);
  digitalWrite(BrakeMotorA, LOW);
  digitalWrite(DirMotorB, Clockwise);
  analogWrite(SpeedMotorB, 100);
  digitalWrite(BrakeMotorB, LOW);
}

void turnRight() {

  digitalWrite(DirMotorA, CClockwise);
  analogWrite(SpeedMotorA, 70);
  digitalWrite(BrakeMotorA, LOW);
  digitalWrite(DirMotorB, CClockwise);
  analogWrite(SpeedMotorB, 70);
  digitalWrite(BrakeMotorB, LOW);
}

void turnLeft() {

  digitalWrite(DirMotorA, Clockwise);
  analogWrite(SpeedMotorA, 70);
  digitalWrite(BrakeMotorA, LOW);
  digitalWrite(DirMotorB, Clockwise);
  analogWrite(SpeedMotorB, 70);
  digitalWrite(BrakeMotorB, LOW);
}

Here is the picture of the circuit, the sensor part is very poorly done, but that works as it should be anyway.

Hope I did everything correctly.

By the way no errors were encountered while compiling.

Thank you in advance for a response and for taking time to help, I appreciate it.

Try this code.
If it doesn't work your circuit is miswired or the supply power is too low.
This code has already been tested by other members and works.

/**
 * Bruno Santos, 2013
 * feiticeir0@whatgeek.com.pt
 * Small code to test DC motors - 2x with a L298 Dual H-Bridge Motor Driver
 * Free to share
 **/
 //Testing the DC Motors
//Define Pins
 //Motor A
 int enableA = 10;
 int pinA1 = 2;
 int pinA2 = 3;
//Motor B
 int enableB = 9;
 int pinB1 = 4;
 int pinB2 = 5;
void setup() {
Serial.begin (9600);
 //configure pin modes
 pinMode (enableA, OUTPUT);
 pinMode (pinA1, OUTPUT);
 pinMode (pinA2, OUTPUT);
pinMode (enableB, OUTPUT);
 pinMode (pinB1, OUTPUT);
 pinMode (pinB2, OUTPUT);
}
void loop() {
 //enabling motor A
 Serial.println ("Enabling Motor A");
 digitalWrite (enableA, HIGH);
//do something
 //forward
 Serial.println ("Forward");
 digitalWrite (pinA1, HIGH);
 digitalWrite (pinA2, LOW);
//5s forward
 delay (5000);
//reverse
 digitalWrite (pinA1,LOW);
 digitalWrite (pinA2,HIGH);
//5s backwards
 delay (5000);
//stop
 digitalWrite (enableA, LOW);
 delay (5000);
//enabling motor B
 //Since motor B is mounted reversed, PINs must be exchanged
 Serial.println ("Enabling Motor A");
 digitalWrite (enableB, HIGH);
//do something
 //forward
 Serial.println ("Forward");
 digitalWrite (pinB1, LOW);
 digitalWrite (pinB2, HIGH);
//5s forward
 delay (5000);
//reverse
 digitalWrite (pinB1,HIGH);
 digitalWrite (pinB2,LOW);
//5s backwards
 delay (5000);
//stop
 digitalWrite (enableB, LOW);
 delay (5000);
 }

Obviously, when you test it, there should be NO OTHER CODE .
Note the lines that reverse the direction.

Hi @walmartronin
I have a small suggestion

Going through your code you just forgot to trigger the trig pin , for triggering the trig pin you must add this to your program

[code]
analogWrite(trig, LOW);
delay(2);
analogWrite(trig,HIGH);
delay(10);
analogWrite(trig,LOW);
duration=pulseln(echo,HIGH);
distance=duration*0.034/2;
[/code]
and don't forget to declare the duration and distance in the pin definition
And wishing you luck for what are you doing 
Lucky
 

If this battery is not already a problem then it is going to be. A PP3 sized battery cannot supply enough current to run an Arduino for very long, let alone 2 motors and a motor shield. You need to upgrade the battery to at least something like 6 AA batteries in series

Can you measure the voltage of the battery when the project is powered ?

Okay, thank you guys for all the replies,

I suspected the 9V is going to be a problem, but as I've said, I'm starting basically from the blind. I will try the 6 AA batteries and the code that raschemmel sent.

About the code for the sensor:
I am not sure what that changes, the sensor worked the way it should and in the test the code progressed when the distance was less than 20 value, but I can of course try it.

I will update after I'll try all these methods.

Thank you all again!