Anyone could help with dc motor rotation?

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);


} 
                  

Please help.

the h-bridge normally has an enable pin driven with a PWM signal (analogWrite()) and a direction pin (digitalWrite()).

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)

#include <IRremote.h>
const byte RECV_PIN = 7; //przypisanie portu nr7 dla odbiornika podczerwieni
const byte motor = 9;
const byte motor1 = 10;

IRrecv irrecv(RECV_PIN);
decode_results results;

void setup() {
  pinMode(motor, OUTPUT);
  pinMode(motor1, OUTPUT);
  irrecv.enableIRIn();
  Serial.begin(9600);
}
void loop() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    switch (results.value) {
      case 0xFF30CF:
        digitalWrite(motor, HIGH);
        digitalWrite(motor1, LOW);
        break;
      case 0xFF10EF:
        digitalWrite(motor, LOW);
        digitalWrite(motor1, HIGH);
        break;
      case 0xFF18E7:
        digitalWrite(motor, LOW);
        digitalWrite(motor1, LOW);
        break;
      case 0xFF7A85:
        digitalWrite(motor, HIGH);
        digitalWrite(motor1, HIGH);
        break;
      default:
        Serial.print(" unknown button   ");
        Serial.println(results.value, HEX);
        break;
    }
    irrecv.resume();
  }
}

i wont use it in this project because i want to make it as compact as possible

as i said in the post i dont have a h bridge that actually works and i wont use it in this project anyways

what do you have? discrete transistors?

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.

Ron

OK, the described circuit is not capable of operating the motor in both the CW or CCW mode. The 2N7000 can be visualized as a simple on / off switch.

To reverse the motor you would have to exchange the motor + and - in your circuit.

Somewhere I missed the schematic, how do I find it?

@gamon2000 did you understand a single transistor or a single MOS-FET is

unable

to reverse polarity.

with discrete components you would need four transistors to reverse polarity
(building a H-bridge with four transistors)

is this compact enough?
a H-bridge in a DIP 8-housing

Be the change you want to see in the world
best regards Stefan

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

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.