Stap 6: Arduino DC motorische controle
Zoals u al weet moet gelijkstroommotor niet rechtstreeks verbinden met arduino pin omdat je arduino kunt branden. Dus moet u een transistor tussen arduino en motor verbinden. Laten we eerst bepalen een kleine DC motor met één transistor. Met behulp van één transistor weet je alleen snelheid controle kunnen zijn. PWM wordt gebruikt voor de controle van de snelheid van een DC-motor. Sluit uw circuit als figuur-1. Pin van de Arduino PWM moet worden aangesloten op de basis pin van de transistor.
/* Single transistor DC Motor control */ int motorPin = 3; int speed = 100; void setup() { pinMode(motorPin, OUTPUT); } void loop() { // analogWrite() function is used to generate PWM signal. // speed define the duty cycle of the PWM. // if the speed = 0 means duty cycle is 0 and motor is off // the maximum value of speed can be 255, then motor will run with maximum speed analogWrite(motorPin, speed); delay(1000); // wait 1 sec analogWrite(motorPin, 175); delay(1000); analogWrite(motorPin, 255); // maximum speed delay(1000); }
Sluit nu, de motor met behulp van H-Bridge IC (ik gebruikte L293 hier). Volg Fig-3 & Fig-4. We kunnen controleren zowel de snelheid en de richting nu. Pin 9 wordt gebruikt als PWM pin en een schakeloptie wordt toegevoegd aan de controle van de snelheid.
const int switchPin = 2; // switch input const int motor1Pin = 3; // H-bridge leg 1 (pin 2, 1A) const int motor2Pin = 4; // H-bridge leg 2 (pin 7, 2A) const int enablePin = 9; // H-bridge enable pin //In the setup(), set all the pins for the H-bridge as outputs, //and the pin for the switch as an input. The set the enable pin high //so the H-bridge can turn the motor on. void setup() { // set the switch as an input: pinMode(switchPin, INPUT); // set all the other pins you're using as outputs: pinMode(motor1Pin, OUTPUT); pinMode(motor2Pin, OUTPUT); pinMode(enablePin, OUTPUT); pinMode(ledPin, OUTPUT); // set enablePin high so that motor can turn on: digitalWrite(enablePin, HIGH); } //In the main loop() read the switch. If it’s high, //turn the motor one way by taking one H-bridge pin high and the other low. // If the switch is low, reverse the direction by reversing the states of // the two H-bridge pins. void loop() { // if the switch is high, motor will turn on one direction: if (digitalRead(switchPin) == HIGH) { digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high } // if the switch is low, motor will turn in the other direction: else { digitalWrite(motor1Pin, HIGH); // set leg 1 of the H-bridge high digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low } }
L293 is een dual H brug IC. Dus, kunt u twee motor door één IC. Twee motor verbinden met de IC als zoals in figuur 5 en gebruik de volgende code. Doorvoeren van wijziging volgens uw behoefte.
/* * created by Md. Khairul Alam * Control 2 DC motors with arduino * 2015 */ int motor1Pin1 = 3; // pin 2 on L293D IC int motor1Pin2 = 4; // pin 7 on L293D IC int motor1EnablePin = 6; // pin 1 on L293D IC int motor2Pin1 = 8; // pin 10 on L293D IC int motor2Pin2 = 9; // pin 15 on L293D IC int motor2EnablePin = 11; // pin 9 on L293D IC int Speed = 100; void setup() { // sets the pins as outputs: pinMode(motor1Pin1, OUTPUT); pinMode(motor1Pin2, OUTPUT); pinMode(motor1EnablePin, OUTPUT); pinMode(motor2Pin1, OUTPUT); pinMode(motor2Pin2, OUTPUT); pinMode(motor2EnablePin, OUTPUT); // sets enable1Pin and enable2Pin high so that motor can turn on: //digitalWrite(Motor1EnablePin, HIGH); //digitalWrite(Motor2EnablePin, HIGH); // initialize serial communication at 9600 bits per second: Serial.begin(9600); } void loop() { //write your code here } void forword(){ // run two motor in forward direction digitalWrite(motor1Pin1, HIGH); digitalWrite(motor1Pin2, LOW); digitalWrite(motor2Pin1, LOW); digitalWrite(motor2Pin2, HIGH); analogWrite(motor1EnablePin, Speed); analogWrite(motor2EnablePin, Speed); //Serial.println("Go Forward!"); } void backword(){ // run two motor in reverse direction digitalWrite(motor1Pin1, LOW); digitalWrite(motor1Pin2, HIGH); digitalWrite(motor2Pin1, HIGH); digitalWrite(motor2Pin2, LOW); analogWrite(motor1EnablePin, Speed); analogWrite(motor2EnablePin, Speed); //Serial.println("Go Reverse!"); } void turnRight(){ // motor 1 off, motor 2 forward digitalWrite(motor1Pin1, LOW); digitalWrite(motor1Pin2, LOW); digitalWrite(motor2Pin1, LOW); digitalWrite(motor2Pin2, HIGH); analogWrite(motor1EnablePin, Speed); analogWrite(motor2EnablePin, Speed); //Serial.println("Turn Right"); } void turnLeft(){ // motor 2 off, motor 1 forward digitalWrite(motor1Pin1, HIGH); digitalWrite(motor1Pin2, LOW); digitalWrite(motor2Pin1, LOW); digitalWrite(motor2Pin2, LOW); analogWrite(motor1EnablePin, Speed); analogWrite(motor2EnablePin, Speed); //Serial.println("Turn Left"); } void Stop(){ two motor off digitalWrite(motor1Pin1, LOW); digitalWrite(motor1Pin2, LOW); digitalWrite(motor2Pin1, LOW); digitalWrite(motor2Pin2, LOW); analogWrite(motor1EnablePin, Speed); analogWrite(motor2EnablePin, Speed); //Serial.println("Stop"); }
.