Im trying to make dc motor rotate clockwise and anticlockwise using mosfet and battery(turorial i learned it from: How To Connect DC Motor + Code [Arduino Tutorial] - YouTube skip to 2:10 for design). I also added infrared sensor and everything works perfectly exept for one thing motor rotates only in one direction. Does anyone know how to change it so it can rotate in two directions? Also don't suggest L298N H bridge because i bought one already and its just a bit broken for some reason.
Here is my code:
#include <IRremote.h>
int RECV_PIN = 7; //przypisanie portu nr7 dla odbiornika podczerwieni
int motor = 9;
int motor1 = 10;
int x = 150;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup() {
pinMode(7, INPUT);
irrecv.enableIRIn();
Serial.begin(9600);
}
void loop() {
int i=0;
if (irrecv.decode(&results)) {
translateIR();
irrecv.resume();
}
}
void translateIR()
{
switch(results.value){
case 0xFF30CF:
analogWrite(motor, 0);
analogWrite(motor1, -150);
delay(1000);
break;
case 0xFF10EF:
analogWrite(motor, x);
analogWrite(motor1, 0);
delay(1000);
break;
case 0xFF18E7:
analogWrite(motor, 0);
analogWrite(motor1, 0);
delay(1000);
break;
case 0xFF7A85:
analogWrite(motor, 0);
analogWrite(motor1, x);
delay(1000);
break;
default:
Serial.print(" unknown button ");
Serial.println(results.value, HEX);
}
delay(500);
}
You could go more into the 21st century with a more modern bridge from Pololu, or more 19th century with a DPDT relay (though you'll also need transistor to buffer)
It works out this way. To reverse a DC motor you reverse the polarity to the motor. There are several ways to go about reversing polarity. You can use a DPDT switch, You can use a DPDT relay and the last option is an H bridge using discrete components or a single chip. Those are the basic options. How compact it is depends on things like motor voltage and locked rotor current draw. Also, depending on motor you may not want to go from full forward to full reverse instantly less a delay.
So, since you choose not to employ a bridge that leaves the remaining two options and since this is a uC Forum I assume the DPDT switch is out leaving only a DPDT relay which will instantly change direction. So you choose your method.
after looking trough what i have i tried again and realised that i needed to acually use my brain and use a screwdriver on L298N. Now i know more about the motor too i without this i would probably not even check H bridge again.
Thanks
Thanks for answer. I will actually use H bridge after reading the answers for my questions and useing brain for a sec. my H bridge wasn't faulty and i just had to use a screwdriver.
by the way i was sleeping and i couldn't read it so sorry for answering so late