[Diao Ye learns programming] Arduino hands-on (154)---AFMotor motor expansion board module 2

The reference to 37 sensors and actuators has been widely circulated on the Internet. In fact, there must be more than 37 sensor modules compatible with Arduino. In view of the fact that I have accumulated some sensor and actuator modules on hand, according to the concept of practice to get true knowledge (must be done), for the purpose of learning and communication, I am going to try a series of experiments one by one, regardless of success (the program goes through) or not, They will be recorded - small progress or unsolvable problems, hoping to inspire others.

[Arduino] 168 kinds of sensor module series experiments (data code + simulation programming + graphics programming)
Experiment 154: L293D four-way motor drive expansion board motor control shield motor board Adafruit Motor Shield module Arduino AFMotor motor expansion board

insert image description here
Experiment 2: Engine Test—Use the serial port to check the operation of the No. 2 DC motor

Arduino experiment open source code

/*

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

实验一百五十四:L293D四路电机驱动扩展板 motor control shield 马达板 

Adafruit Motor Shield模块 Arduino AFMotor 电机扩展板

 1、安装库:百度搜索“AFMotor库”— 下载 — 拷贝到Arduino-libraries 文件夹中

 2、实验之二:引擎测试—用串口查看2号直流电机运转情况

*/



#include "AFMotor.h"

AF_DCMotor motor(2);

void setup() {
    
    

 Serial.begin(9600);      // set up Serial library at 9600 bps

 Serial.println("引擎测试 Motor test!");

 // turn on motor

 motor.setSpeed(200);

 motor.run(RELEASE);

}

void loop() {
    
    

 uint8_t i;

 Serial.println("向前 FORWARD!");

 motor.run(FORWARD);

 for (i = 0; i < 255; i++) {
    
    

  motor.setSpeed(i);

  delay(10);

 }

 for (i = 255; i != 0; i--) {
    
    

  motor.setSpeed(i);

  delay(10);

 }

 Serial.println("向后 BACKWARD!");

 motor.run(BACKWARD);

 for (i = 0; i < 255; i++) {
    
    

  motor.setSpeed(i);

  delay(10);

 }

 for (i = 255; i != 0; i--) {
    
    

  motor.setSpeed(i);

  delay(10);

 }

 Serial.println("停止 RELEASE!");

 motor.run(RELEASE);

 delay(1000);

}

Experimental serial port return

insert image description here

AF_DCMotor class functions

The AFMotor class works with the Adafruit Motor Shied to control the speed and direction of up to 4 DC motors simultaneously. To use these functions, first add the library file at the beginning:

#include <AFMotor.h>

AF_DCMotor motorname(portnum,freq)

This is a function to build a DC motor. Each motor needs to be declared once in the program. Each motor must use a different name as in the example below.

parameter:

port num - Select the port (1-4) where your motor is connected to the motor driver board

freq - Select the PWM frequency. If you do not select this option, the default setting is 1KHZ.

Frequency for channels 1 and 2:

MOTOR12_64KHZ

MOTOR12_8KHZ

MOTOR12_2KHZ

MOTOR12_1KHZ

Frequency for channels 3 and 4:

MOTOR34_64KHZ

MOTOR34_8KHZ

MOTOR34_1KHZ

For example:

AF_DCMotor motor4(4);//Channel 4, the default frequency is 1KHZ

AF_DCMotor left_motor(1, MOTOR12_64KHZ);//channel 1, 64KHZ frequency

Note: A higher frequency will reduce the noise of the motor during motion, but it will also reduce the torque.

setSpeed(speed)

Set the speed of the motor

parameter:

speed - ranges from 0 to 255, with 0 being stopped and 255 being full speed.

Note: The feedback of a DC motor is not typically linear, so the actual speed will not be directly proportional to the speed set in the program.

run(cmd)

Set the operation mode of the motor

parameter:

cmd - select the motor operation mode you want

Available modes:

FORWARD - Forward rotation (the actual direction of rotation depends on the wiring of your motor)

BACKWARD - Reverse (the direction of rotation is opposite to forward rotation)

RELEASE - stop. Power off the motor, same function as setSpeed(0) function. After calling this function, it will take some time for the motor to stop completely.

When using the AFMotor motor expansion board module, it is necessary to pay attention to the motor interference. Add 3 small capacitors of 0.1u (104) to each motor. The specific connection is shown in the figure below

insert image description here
drive DC motor
insert image description here

Drive servo motor (steering gear)

First, you need to install the library: IDE—Tool—Manage Library—Search for "servo"—Install

insert image description here
Experiment 3: Driving a single servo motor (No. 2 servo)

Arduino experiment open source code

/*

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

实验一百五十四:L293D四路电机驱动扩展板 motor control shield 马达板 

Adafruit Motor Shield模块 Arduino AFMotor 电机扩展板

 1、安装库:IDE—工具—管理库—搜索“Servo”—安装

 2、实验之三:驱动单只伺服电机(2号舵机)

*/



#include <Servo.h>

Servo myservo; // create servo object to control a servo

// twelve servo objects can be created on most boards

int pos = 0;  // variable to store the servo position

void setup() {
    
    

 myservo.attach(9); // attaches the servo on pin 9 to the servo object

}

void loop() {
    
    

 for (pos = 0; pos <= 180; pos += 1) {
    
     // goes from 0 degrees to 180 degrees

  // in steps of 1 degree

  myservo.write(pos);       // tell servo to go to position in variable 'pos'

  delay(15);            // waits 15ms for the servo to reach the position

 }

 for (pos = 180; pos >= 0; pos -= 1) {
    
     // goes from 180 degrees to 0 degrees

  myservo.write(pos);       // tell servo to go to position in variable 'pos'

  delay(15);            // waits 15ms for the servo to reach the position

 }

}

Experiment 4: Drive two servo motors (No. 1 and No. 2 servo motors)

Arduino experiment open source code

/*

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

实验一百五十四:L293D四路电机驱动扩展板 motor control shield 马达板 

Adafruit Motor Shield模块 Arduino AFMotor 电机扩展板

 1、安装库:IDE—工具—管理库—搜索“Servo”—安装

 2、实验之四:驱动二只伺服电机(1号与2号舵机)

*/



#include <Servo.h>

Servo servo1;  //建立舵机对象servo1

Servo servo2;  //建立舵机对象servo2

 

int pos = 0;   

void setup() {
    
    

 servo1.attach(10);  //servo1对象接在扩展板servo1端口。

            //servo1端口是由Arduino的10号引脚来控制的。

 servo2.attach(9);   //servo2对象接在扩展板servo2端口。

            //servo2端口是由Arduino的9号引脚来控制的。 

}

 

void loop() {
    
    

 //以下程序将控制servo1输出轴左右旋转180度

 for (pos = 0; pos <= 180; pos += 1) {
    
     

  servo1.write(pos);        

  delay(15);            

 }

 for (pos = 180; pos >= 0; pos -= 1) {
    
     

  servo1.write(pos);   

  delay(15);    

 }

  

 //以下程序将控制servo2输出轴左右旋转180度

 for (pos = 0; pos <= 180; pos += 1) {
    
     

  servo2.write(pos);        

  delay(15);            

 }

 for (pos = 180; pos >= 0; pos -= 1) {
    
     

  servo2.write(pos);   

  delay(15);    

 }  

}

Servo library

Allows Arduino/Genuino development boards to control various servo motors (rudders). This library can control a large number of servos. It uses timers carefully: the library can control 12 servos using only 1 timer. On the Arduino Due, up to 60 servos can be controlled.

The library is compatible with avr, megaavr, sam, samd, nrf52, stm32f4, mbed architectures. To use this library, open the library manager in the Arduino IDE and install it from there.

This library allows Arduino boards to control RC (hobbyist) servo motors. Servo systems have integrated gears and axes that can be precisely controlled. Standard servos allow the axis to be positioned at various angles, usually between 0 and 180 degrees. Continuous rotation servos allow the rotation of the shaft to be set at various speeds.

The servo library supports up to 12 motors on most Arduino boards and up to 48 motors on the Arduino Mega. On boards other than the Mega, using this library disables the analogWrite() (PWM) functionality on pins 9 and 10, regardless of whether there is a Servo on those pins. On the Mega, up to 12 servos can be used without affecting the PWM functionality. Using 12 to 23 motors will disable the PWM on pins 11 and 12.

To use this library:

#include <Servo.h>

circuit diagram

A servo motor (rudder) has three wires: power, ground, and signal. The power wire is usually red and should be connected to the 5V pin on the Arduino board. The ground wire is usually black or brown and should be connected to the ground pin on the Arduino board. The signal pins are usually yellow, orange or white and should be connected to digital pins on the Arduino board. Note that servos draw a lot of power, so if you need to drive more than one or two, you may need to power them from a separate power supply (ie, not the +5V pin on the Arduino). Make sure to connect the ground of the Arduino and the external power supply together.

insert image description here
Using the Servo library

1. Servo-attach() function (connection)

Attach servo variables to pins. Note that in Arduino 0016 and earlier, the Servo library only supports servos on two pins: 9 and 10.

syntax

servo.attach(pin)

servo.attach(pin, min, max)

Parameter

Servo: variable of type Servo

pin: the pin number the server is connected to

min (optional): pulse width in microseconds, corresponding to the minimum (0 degree) angle on the servo (default 544)

max (optional): pulse width in microseconds, corresponding to the maximum (180 degree) angle on the servo (default 2400)

example

#include <Servo.h>

Servo myservo;

void setup()

{
    
    

  myservo.attach(9);

}

void loop() {
    
    }

2. Servo-write (x) function (write)

Write the value to the servo, which controls the axis accordingly. On a standard servo this will set the angle (in degrees) of the axis to move the axis in that direction. On a continuous rotation servo (360 degree servo), this will set the speed of the servo (0 means full speed in one direction, 180 means full speed in the other direction, and a value around 90 means no motion).

syntax

servo.write(angle)

Parameter

Servo: A variable of type Servo

angle: the value written to the servo, from 0 to 180

example

#include <Servo.h>

Servo myservo;

void setup()

{
    
    

  myservo.attach(9);

  myservo.write(90);  // set servo to mid-point

}

void loop() {
    
    }

3. Servo-writeMicroseconds() function (precise control)

Write a value in microseconds (uS), thereby controlling the axis accordingly. On a standard servo, this will set the angle of the axis. On a standard server, a parameter value of 1000 is completely counterclockwise, 2000 is completely clockwise, and 1500 is in between.

Note that some manufacturers do not follow this standard very closely, so servos will typically respond to values ​​between 700 and 2300. Feel free to increase these endpoints until the server no longer continues to increase its range. Note, however, that attempting to drive a servo drive through its terminals (often indicated by a hissing sound) is a high current state and should be avoided. A continuously rotating servo will respond to the writeMicrosecond function in a similar way to the write function.

syntax

servo.writeMicroseconds(uS)

Parameter

Servo: A variable of type Servo

us: parameter value in microseconds (int)

example

#include <Servo.h>

Servo myservo;

void setup()

{
    
    

  myservo.attach(9);

  myservo.writeMicroseconds(1500);  // set servo to mid-point

}

void loop() {
    
    }

4. Servo-read() function (read)

Read the current angle of the servo (the value passed to the last call to write()).

syntax

servo.read()

Parameter

Servo: variable of type Servo

the feedback

The servo angle is 0 to 180 degrees.

5. Servo-Attached () function (additional)

Check if the Servo variable is attached to the pin.

syntax

servo.attached()

Parameter

Servo: variable of type Servo

the feedback

true if the servo is connected to the pin; false otherwise.

6. Servo-detach() function (separation)

Remove the servo variable from its pins. If all servo variables are decoupled, you can use AnalogWrite() to use pins 9 and 10 for PWM output.

syntax

servo.detach()

Parameter

Servo: variable of type Servo

insert image description here

Drive 28BYJ-48 stepper motor

The AFMotor motor expansion board can drive up to two 28BYJ-48 stepper motors. Below we will introduce you how to use the two motor expansion boards. The first is the method of use when you develop the project, and the second is the method when the expansion board is used to drive the motor after the project is developed. The biggest difference between these two methods is that the Arduino development board is connected to the computer through a USB data cable during development. But the development board is not connected to the computer during work.

Stepper motors can be used for precise control and are well suited for many robotic or CNC fabrications. This motor driver board supports up to 2 stepper motors. Both bipolar and unipolar motors are suitable for this library. For bipolar motors (42 two-phase four-wire motors): There are two phases, the two wires of phase 1 are connected to M1 or M3 on the driver board, and the two wires of phase 2 are connected to M2 or M4 on the driver board. . Running a stepper motor is a little more complicated than running a DC motor, but overall it's relatively simple.

/*

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

实验一百五十四:L293D四路电机驱动扩展板 motor control shield 马达板 

Adafruit Motor Shield模块 Arduino AFMotor 电机扩展板

 1、安装库:百度搜索“AFMotor库”— 下载 — 拷贝到Arduino-libraries 文件夹中

 2、实验之五:驱动28BYJ-48步进电机

*/

#include <AFMotor.h> // 本程序中使用AFMotor库

 

AF_Stepper motor1(2048, 1); // 这2条语句的作用是建立2个步进电机对象,它们的名称分别是:motor1/2。

AF_Stepper motor2(2048, 2); // 对象名称后面括号中的两个参数分别代表了步进电机旋转一周的步数以及

               // 步进电机连接在扩展板上的端口号。如AF_Stepper motor1(2048, 1)语句中

               // 参数2048代表motor1旋转一周需要走2048步。假如您的电机旋转一周需要32步,

               // 那么请在第一个参数位置输入32这一数字参数。

               // 括号中第二个参数1代表motor1连接在M1和M2端口。

               // 对于motor2对象,它括号中的参数2代表该电机连接在M3和M4端口。

 

void setup() {
    
    

 Serial.begin(9600);     //启动串口通讯

  

 motor1.setSpeed(10);    // 这2条语句的作用是通过setSpped库函数设置电机运行速度。

 motor2.setSpeed(10);    // setSpped库函数中的参数是运行速度参数。

               // 速度参数越大,运转速度越快。参数为0时电机停止转动。

}

 

/*

以下的loop函数中有4段程序语句。它们的内容十分类似,主要语句都是是通过step库函数来控制

步进电机的运行模式。step库函数一共有3个参数。如loop函数的第二行语句:motor1.step(2048, FORWARD, SINGLE)。

括号中的第一个参数是控制电机走的步数,这里的参数2048就是让电机走2048步。

接下来的关键字参数"FORWARD"作用是控制电机旋转方向。"FORWARD"是控制电机"正转",

这里也可以使用关键字"BACKWARD"让电机"反转"。

最后一个关键字参数是用于控制步进电机运转模式的。这里可选的关键字参数有:

SINGLE - 全步进模式(单线圈)。电机每走一步,扩展板只给一相线圈供电。

DOUBLE - 全步进模式(双线圈)。电机每走一步,扩展板会同时给两相线圈供电。

     此模式运行的电机比SINGLE模式下的扭矩要更大,但是电机耗电也会更多。

INTERLEAVE - 半步进模式。这种模式是SINGLE和DOUBLE的混合。电机每走一步,扩展板对线圈供电方式

       在一相和两相之间交替切换。举例来说,电机走第一步时,只有一相线圈通电。

       走第二步时,会有两相线圈供电,然后又是一相,再两相......这样交替通电。

       这种控制模式的优点是电机运行更流畅,但是缺点是运行速度大打折扣。

MICROSTEP - 微步进模式。此模式下的电机运行更光滑,但缺点是扭矩会打折扣。   

*/

 

void loop() {
    
    

 Serial.println("Single Mode");    //串口监视器输出当前运行模式为“Single”

 motor1.step(2048, FORWARD, SINGLE);  //步进电机以SINGLE模式"正转"2048步

 motor1.step(2048, BACKWARD, SINGLE);  //步进电机以SINGLE模式"反转"2048步

 

 Serial.println("Double Mode");    //串口监视器输出当前运行模式为“Double”

 motor2.step(2048, FORWARD, DOUBLE);  //步进电机以DOUBLE模式"正转"2048步

 motor2.step(2048, BACKWARD, DOUBLE);  //步进电机以DOUBLE模式"反转"2048步

 

 Serial.println("Interleave Mode");    //串口监视器输出当前运行模式为“Interleave”

 motor1.step(2048, FORWARD, INTERLEAVE);  //步进电机以INTERLEAVE模式"正转"2048步

 motor1.step(2048, BACKWARD, INTERLEAVE);  //步进电机以INTERLEAVE模式"反转"2048步

 

 Serial.println("Micrsostep Mode");     //串口监视器输出当前运行模式为“Micrsostep”

 motor2.step(2048, FORWARD, MICROSTEP);   //步进电机以MICROSTEP模式"正转"2048步

 motor2.step(2048, BACKWARD, MICROSTEP);   //步进电机以MICROSTEP模式"反转"2048步

}


Functions related to the stepper motor
After making sure to import the AFMotor library file and #include <AFMotor.h>, there are several functions that need to be used

1. AF_Stepper steppername(steps, portnumber)
(1) steppername
gives the motor a name, and then this name is the code name of the motor in the program
(2) steps
set the number of steps per revolution of the motor, for example, if it is set to 36, then each step Take 1/36 turn
(3) portnumber
to select the channel of the motor, the range is 1 (channel 1, 2) and 2 (channel 3 and 4)

2. Step(steps,direction,style)
(1)
The number of step rotation steps
(2)
The direction of direction rotation FORWARD or BACKWARD
(3) The style
stepping mode, the parameters that can be selected:
a\SINGLE - only one at a time Phase Coil Power
b\DOUBLE - Powers both collars at once for more torque
c\INTERLEAVE - The motor will run smoother as the number of steps is doubled while the speed is cut in half
d\MICROSTEP - Motor It will run smoother and with higher precision, but the torque will also be reduced

If you want to control the stepper motor more finely, you can use the AccelStepper library, which has the function of motor acceleration and deceleration.

/*

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

实验一百五十四:L293D四路电机驱动扩展板 motor control shield 马达板 

Adafruit Motor Shield模块 Arduino AFMotor 电机扩展板

 1、安装库:百度搜索“AFMotor库”— 下载 — 拷贝到Arduino-libraries 文件夹中

 2、实验之六:连接每转48步(7.5度)的步进电机,电机端口2#(M3和M4)

*/

#include <AFMotor.h>

// Connect a stepper motor with 48 steps per revolution (7.5 degree)

// to motor port #2 (M3 and M4)

AF_Stepper motor(48, 2);

void setup() {
    
    

 Serial.begin(9600);      // set up Serial library at 9600 bps

 Serial.println("Stepper test! 测试步进电机!");

 motor.setSpeed(10); // 10 rpm

}

void loop() {
    
    

 Serial.println("Single coil steps 单线圈步");

 motor.step(100, FORWARD, SINGLE);

 motor.step(100, BACKWARD, SINGLE);

 Serial.println("Double coil steps 双线圈步");

 motor.step(100, FORWARD, DOUBLE);

 motor.step(100, BACKWARD, DOUBLE);

 Serial.println("Interleave coil steps 交错线圈台阶");

 motor.step(100, FORWARD, INTERLEAVE);

 motor.step(100, BACKWARD, INTERLEAVE);

 Serial.println("Micrsostep steps 微步前进");

 motor.step(100, FORWARD, MICROSTEP);

 motor.step(100, BACKWARD, MICROSTEP);

}


Experimental serial port return

insert image description here
Arduino AFMotor motor expansion board experiment scene diagram

insert image description here

Guess you like

Origin blog.csdn.net/weixin_41659040/article/details/131597301