简体   繁体   English

使用 Arduino Uno 控制直流电机

[英]Controlling a DC motor with an Arduino Uno

I have a simple H-bridge circuit set up following this schematic:我按照这个原理图设置了一个简单的H 桥电路:

在此处输入图像描述

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.我试图通过这个带有Arduino Uno的 H 桥来控制小型直流电机,但我以前从未对这些控制器中的一个进行过编程。 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.然而,我在修改PWM以使电机保持开启时遇到了问题,而且 output 引脚也没有按照我指定的方式设置。

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. 解决问题的方法(使电动机保持运转)不是PWM,而是以正确的组合设置引脚状态。 Be aware that code you use is for all-NPN transistors H-Bridge. 请注意,您使用的代码适用于全NPN晶体管H桥。 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). 您的电路是由PNP-NPN组合构建的,因此控制方式有所不同(NPN晶体管在饱和时传递电流,而PNP晶体管在饱和时禁止电流)。

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. 首先将一对设置为LOW,然后再将另一对设置为HIGH,否则将使函数调用之间的电路短路。

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. 注意:您可以将PWM与analogWrite()函数一起使用来控制电机速度,但是您需要稍作修改:将另外的NPN晶体管置于接地之前(集电极位于H桥上,发射极接地),将其基极连接到具有PWM功能Arduino引脚通过限流电阻器。

Explanation of vcc2gnd's answer vcc2gnd答案的说明

Assuming 5V is given to H-Bridge circuit, PNP's turn ON when they have 0v to their base. 假设给H桥电路提供了5V,则当PNP的基极电压为0v时,PNP就会接通。 NPN's turn ON when they have 5v to their base. 当它们的基准电压为5v时,NPN就会打开。 When transistor is ON (saturated) it conducts current. 当晶体管导通(饱和)时,它会导通电流。

When Q4 and Q1 ON while others OFF(cutoff), motor turns in one direction. 当Q4和Q1接通而其他Q4和Q1断开(切断)时,电机向一个方向转动。 To have that direction, R1,R2,R3,R4 should given 5v,5v,0v,0v respectively. 要具有该方向,R1,R2,R3,R4应分别指定为5v,5v,0v,0v。

Schematic for H-bridge circuit using IRF7105 dual channel Mosfet使用 IRF7105 双通道 Mosfet 的 H 桥电路原理图

使用 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.基于 Mosfet 的 H 桥,微型尺寸,适用于使用 20V 的高达 2 安培的连续电流,原理图已在 Proteus 仿真软件中测试并复制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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