简体   繁体   中英

Controlling a DC motor with an Arduino Uno

I have a simple H-bridge circuit set up following this schematic:

在此处输入图像描述

I'm trying to control a small DC motor through this H-bridge with an Arduino Uno , but I have never programmed one of these controllers before. I need the motor to rotate in different directions when I press left and right on a keyboard. So far I have this code:

// Right Motor

/** Adjust these values for your servo and setup, if necessary **/
int resistor1     =  3;
int resistor2     =  5;
int resistor3     =  6;
int resistor4     =  10;
int moveServo;


void setup() {
    Serial.begin(9600);
    pinMode(resistor1, OUTPUT); // Set servo pin as an output pin
    pinMode(resistor2, OUTPUT);
    pinMode(resistor3, OUTPUT);
    pinMode(resistor4, OUTPUT);
}


void loop() {
  // Wait for serial input
  if (Serial.available() > 0) {
      // Read the incoming byte:
      moveServo = Serial.read();

      // ASCII left = 37, up = 38, right = 39, down = 40
      if (moveServo == 37)
      {
          digitalWrite(resistor4, HIGH);
          digitalWrite(resistor1, HIGH);
      }
  }

I am however, having trouble modifying the PWM so that the motor would stay on and also the output pins aren't being set as I specified.

How can I fix this problem?

如果要产生PWM输出,则必须使用AnalogWrite()函数。

The solution for your problem (keep the motor running) is not PWM, but to set pin state in correct combination. Be aware that code you use is for all-NPN transistors H-Bridge. Your circuit is built with PNP - NPN combination, thus the control is different (NPN transistor is delivering current when saturated while PNP transistor is prohibiting current when saturated).

Try modify your code like this:

if (moveServo == 37)
{
    digitalWrite(resistor1, LOW);
    digitalWrite(resistor2, LOW);
    digitalWrite(resistor3, HIGH);
    digitalWrite(resistor4, HIGH);
}
else if (moveServo == 39)
{
    digitalWrite(resistor3, LOW);
    digitalWrite(resistor4, LOW);
    digitalWrite(resistor1, HIGH);
    digitalWrite(resistor2, HIGH);
}

Note that the order is important. Set one pair to LOW first before set the other to HIGH, otherwise you'll short the circuit between function calls.

Note: You can use PWM with analogWrite() function to control motor speed, but you need to slightly modify your circuit: put additional NPN transistor before ground (collector on H-Bridge, emitter on ground), connect it's base with PWM-capable Arduino pin via a limiting resistor.

Explanation of vcc2gnd's answer

Assuming 5V is given to H-Bridge circuit, PNP's turn ON when they have 0v to their base. NPN's turn ON when they have 5v to their base. When transistor is ON (saturated) it conducts current.

When Q4 and Q1 ON while others OFF(cutoff), motor turns in one direction. To have that direction, R1,R2,R3,R4 should given 5v,5v,0v,0v respectively.

Schematic for H-bridge circuit using IRF7105 dual channel Mosfet

使用 IRF7105 双通道 Mosfet 的 H 桥电路原理图

Mosfet based H bridge, miniature size and good for up to 2 amps continuous current using 20V, The schematic is tested in Proteus simulation software and replicated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM