Ardunio——ULN2003驱动板和直流电机风扇——控制风扇转速

先导知识

PWM
  • 通过数字技术实现模拟结果。通过数字控制创建方波,高电平相当于开关开,低电平相当于开关关,进行来回切换。通过控制方波出现的频率来实现对电压高低的模拟。就拿小灯泡来说,频率越低,灯越暗,相当于电压越低。频率越高,灯越亮,相当于电压高。类比,来控制风扇的快慢和方向。

ULN2003驱动板

  • ULN2003驱动的图示
    在这里插入图片描述
  • ULN2003驱动的电路图
    在这里插入图片描述
  • 对照这个电路图,就可以知道这个怎么利用ULN2003芯片,怎么实现接线,见下图。
    在这里插入图片描述

实际接线

  • 按照下图进行接线
    在这里插入图片描述
  • 运行代码
#define MotorPin D5

void setup() {
  // put your setup code here, to run once:
  pinMode(MotorPin,OUTPUT);
  Serial.begin(9600);
}

void ControlFans(int LastTime){
  analogWrite(MotorPin,1200);
  delay(LastTime * 1000);
  analogWrite(MotorPin,0);
}
void loop() {
  ControlFans(10);
}

结果

  • 最终风扇会运行10秒钟,然后停下,然后再运行10秒钟。

参考

  • https://blog.csdn.net/hypecycle/article/details/110674844
  • https://zhuanlan.zhihu.com/p/444385188
  • https://www.arduino.cn/thread-91049-2-1.html
以下是基于Arduino控制TB6612驱动四路闭环电机的示例代码: ``` #include <Wire.h> #include <Adafruit_MotorShield.h> #include "utility/Adafruit_MS_PWMServoDriver.h" Adafruit_MotorShield AFMS = Adafruit_MotorShield(); Adafruit_DCMotor *motor1 = AFMS.getMotor(1); Adafruit_DCMotor *motor2 = AFMS.getMotor(2); Adafruit_DCMotor *motor3 = AFMS.getMotor(3); Adafruit_DCMotor *motor4 = AFMS.getMotor(4); int motor1_encoder_pinA = 2; int motor1_encoder_pinB = 3; int motor2_encoder_pinA = 4; int motor2_encoder_pinB = 5; int motor3_encoder_pinA = 6; int motor3_encoder_pinB = 7; int motor4_encoder_pinA = 8; int motor4_encoder_pinB = 9; volatile int motor1_encoder_count = 0; volatile int motor2_encoder_count = 0; volatile int motor3_encoder_count = 0; volatile int motor4_encoder_count = 0; void motor1_encoder_interrupt() { if (digitalRead(motor1_encoder_pinA) == digitalRead(motor1_encoder_pinB)) { motor1_encoder_count++; } else { motor1_encoder_count--; } } void motor2_encoder_interrupt() { if (digitalRead(motor2_encoder_pinA) == digitalRead(motor2_encoder_pinB)) { motor2_encoder_count++; } else { motor2_encoder_count--; } } void motor3_encoder_interrupt() { if (digitalRead(motor3_encoder_pinA) == digitalRead(motor3_encoder_pinB)) { motor3_encoder_count++; } else { motor3_encoder_count--; } } void motor4_encoder_interrupt() { if (digitalRead(motor4_encoder_pinA) == digitalRead(motor4_encoder_pinB)) { motor4_encoder_count++; } else { motor4_encoder_count--; } } void setup() { pinMode(motor1_encoder_pinA, INPUT); pinMode(motor1_encoder_pinB, INPUT); pinMode(motor2_encoder_pinA, INPUT); pinMode(motor2_encoder_pinB, INPUT); pinMode(motor3_encoder_pinA, INPUT); pinMode(motor3_encoder_pinB, INPUT); pinMode(motor4_encoder_pinA, INPUT); pinMode(motor4_encoder_pinB, INPUT); attachInterrupt(digitalPinToInterrupt(motor1_encoder_pinA), motor1_encoder_interrupt, CHANGE); attachInterrupt(digitalPinToInterrupt(motor2_encoder_pinA), motor2_encoder_interrupt, CHANGE); attachInterrupt(digitalPinToInterrupt(motor3_encoder_pinA), motor3_encoder_interrupt, CHANGE); attachInterrupt(digitalPinToInterrupt(motor4_encoder_pinA), motor4_encoder_interrupt, CHANGE); AFMS.begin(); motor1->setSpeed(255); motor2->setSpeed(255); motor3->setSpeed(255); motor4->setSpeed(255); } void loop() { // Motor 1 control if (motor1_encoder_count < 10000) { motor1->run(FORWARD); } else { motor1->run(RELEASE); } // Motor 2 control if (motor2_encoder_count < 10000) { motor2->run(FORWARD); } else { motor2->run(RELEASE); } // Motor 3 control if (motor3_encoder_count < 10000) { motor3->run(FORWARD); } else { motor3->run(RELEASE); } // Motor 4 control if (motor4_encoder_count < 10000) { motor4->run(FORWARD); } else { motor4->run(RELEASE); } delay(1); } ``` 该代码中,我们使用了Adafruit Motor Shield库来控制TB6612驱动器。我们还使用了中断计数器来读取四个电机的编码器并控制电机的旋转。在此示例代码中,我们设置了电机旋转10000个脉冲后停止旋转,但您可以根据您的特定应用程序进行更改。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值