Arduino and problem with DC motor

I want to control DC motor with Arduino and I have idea but i dont know how can i do it :slight_smile:

I try transfer to picture ( but i am newbie in Arduino and also in Fritzing )

On the picture is question mark and this is the problem area . I need a component which connent a circuit beetwen battery and DC motor and when Arduino send a signal ( or something else ) the dc motor interrupst connenection between a battery and conversely . I hope that you recommend me and you understand what I want to say ( English is not my native language ) . But if you have better idea which do the same thing , just tell me it :smiley: .

Use an NPN transistor between the motor and the Ground (-) side of the battery. Here is an example:

http://itp.nyu.edu/physcomp/Tutorials/HighCurrentLoads

You will need a protection diode as well - a motor is an inductive load. The example circuit on that link is WRONG - the protection diode is in the wrong place - it needs to be across the motor, not across the transistor.

Actually thinking about it, I'd go for both places for diodes to be extra-safe.

I decide that I will need to use 2 DC motors and on the page ( link above ) I find H-bridge and I think that it is good idea for my purpuse . I hope that I will find shop in my city with H-bridge :smiley: . Anyway, I want to show you my idea ( I am not sure about it , can I use 4,5V battery or for 2 motors are better 9V )

And what about code ? Is there everything OK ?

const int motor1Pin = 3; // H-bridge leg 1 (pin 2, 1A)
const int motor2Pin = 4; // H-bridge leg 2 (pin 7, 2A)
const int enable1Pin = 8; // H-bridge enable pin (1DC)
const int motor3Pin = 5; // H-bridge leg 3 (pin 15, 3A)
const int motor4Pin = 6; // H-bridge leg 4 (pin 10, 4A)
const int enable2Pin = 9; // H-bridge enable pin (2DC)
int incomingByte;

void setup() {
Serial.begin(9600);

// set all the other pins you're using as outputs:
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(enable1Pin, OUTPUT);
pinMode(motor3Pin, OUTPUT);
pinMode(motor4Pin, OUTPUT);
pinMode(enable2Pin, OUTPUT);

// set enablePin high so that motor can turn on:
digitalWrite(enablePin, HIGH);
digitalWrite(enaglePin, HIGH);
}

void loop() {
if (Serial.available() > 0) {
incomingByte = Serial.read();
// if the switch is high, motor will turn on one direction:
if (incomingByte == "W") { // One direction for both motors
digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low
digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high
digitalWrite(motor3Pin, LOW); // set leg 3 of the H-bridge low
digitalWrite(motor4Pin, HIGH); // set leg 4 of the H-bridge high

}
else if (incomingByte == "A") { // on the left , one stop , other run
digitalWrite(motor1Pin, HIGH);
digitalWrite(motor2Pin, HIGH);
digitalWrite(motor3Pin, LOW);
digitalWrite(motor4Pin, HIGH);
}
else if (incomingByte == "D") { // on the right , one run , other stop
digitalWrite(motor1Pin, LOW);
digitalWrite(motor2Pin, HIGH);
digitalWrite(motor3Pin, HIGH);
digitalWrite(motor4Pin, HIGH);
}
}
else { // both motors stop
digitalWrite(motor1Pin, LOW);
digitalWrite(motor2Pin, LOW);
digitalWrite(motor3Pin, LOW);
digitalWrite(motor4Pin, LOW);
}
}

I hope that there is no mistake , but for the peace of mind :smiley: I'd rather show it to you :smiley: