Arduino: L293D for a DC motor

L293D for a DC motor


A L293D motor controller chip and a DC motor. L293D and a DC motor

I got fed up with indecipherable pinout diagrams within datasheets, so have designed my own that I think gives more pertinent information.

There are 3 wires connected to the arduino, 2 wires connected to the motor, and 1 wire connected to a battery.

A L293D motor controller chip - pinout diagram.
Green - connected to arduino.
Blue - connect to motor
Pink - connect to a battery +ve, eg 12 volt.
L293D and a DC motor

To use this pinout:
The left hand side deals with the first motor, the right hand side deals with a second motor.
Yes, you can run it with only one motor connected.

Arduino Connections
M1 PWM - connect this to a PWM pin on the arduino. They're labelled on the Uno, pin 3 is an example. Output any integer between 0 and 255, where 0 will be off, 128 is half speed and 255 is max speed.

M1 direction 0/1 and M1 direction 1/0 - Connect these two to two digital arduino pins. Output one pin as HIGH and the other pin as LOW, and the motor will spin in one direction. 
Reverse the outputs to LOW and HIGH, and the motor will spin in the other direction.

A L293D motor controller chip - pinout diagram.
Green - connected to arduino.
Blue - connect to motor
Pink - connect to a battery +ve, eg 12 volt.
L293D and a DC motor

To use this pinout:
The left hand side deals with the first motor, the right hand side deals with a second motor.
Yes, you can run it with only one motor connected.

Arduino Connections
M1 PWM - connect this to a PWM pin on the arduino. They're labelled on the Uno, pin 3 is an example. Output any integer between 0 and 255, where 0 will be off, 128 is half speed and 255 is max speed.

M1 direction 0/1 and M1 direction 1/0 - Connect these two to two digital arduino pins. Output one pin as HIGH and the other pin as LOW, and the motor will spin in one direction. 
Reverse the outputs to LOW and HIGH, and the motor will spin in the other direction.


The code below does not use a separate power supply (ie a battery), it uses instead the 5v power from the arduino. Note that this would be risky without the L293D controlling it. 
You should _never_ connect a motor directly to the arduino, because when you switch a motor off you get an electrical feedback. With a small motor, this will damage your arduino, and with a large motor, you can watch an interesting flame and sparks effect.

Wired up.
L293D Pin : Arduino Pin
1	:	3
2	:	5
3	(DC motor +5V) 
4	(GND)
5	(GND)
6	(DC motor GND)
7	:	4
8	(+5V)

9	:	6
10	:	8
11	(DC motor #2 +5V)
12	(GND)
13	(GND)
14	(DC motor #2 GND)
15	:	7
16	(+5V)
joystick

This is the code that I used.

A L293D motor controller chip - pinout diagram.
Green - connected to arduino.
Blue - connect to motor
Pink - connect to a battery +ve, eg 12 volt.
L293D and a DC motor

To use this pinout:
The left hand side deals with the first motor, the right hand side deals with a second motor.
Yes, you can run it with only one motor connected.

Arduino Connections
M1 PWM - connect this to a PWM pin on the arduino. They're labelled on the Uno, pin 3 is an example. Output any integer between 0 and 255, where 0 will be off, 128 is half speed and 255 is max speed.

M1 direction 0/1 and M1 direction 1/0 - Connect these two to two digital arduino pins. Output one pin as HIGH and the other pin as LOW, and the motor will spin in one direction. 
Reverse the outputs to LOW and HIGH, and the motor will spin in the other direction.


/************************
Exercise the motor using
the L293 chip
************************/

#define ENABLE 3
#define DIRB 4
#define DIRA 5


int i;
 
void setup() {
  //---set pin direction
  pinMode(ENABLE,OUTPUT);
  pinMode(DIRA,OUTPUT);
  pinMode(DIRB,OUTPUT);

 
Serial.begin(9600);

}

void loop() {
  //---back and forth example

Serial.println("One way, then reverse");
  digitalWrite(ENABLE,HIGH); // enable on
  for (i=0;i<5;i++) {
    digitalWrite(DIRA,HIGH); //one way
    digitalWrite(DIRB,LOW);
    delay(500);
    digitalWrite(DIRA,LOW);  //reverse
    digitalWrite(DIRB,HIGH);
    delay(500);
  }
  digitalWrite(ENABLE,LOW); // disable
  delay(4000);

Serial.println("fast Slow example");
  //---fast/slow stop example
  digitalWrite(ENABLE,HIGH); //enable on
  digitalWrite(DIRA,HIGH); //one way
  digitalWrite(DIRB,LOW);
  delay(1000);
  digitalWrite(ENABLE,LOW); //slow stop
  delay(3000);
  digitalWrite(ENABLE,HIGH); //enable on
  digitalWrite(DIRA,HIGH); //one way
  digitalWrite(DIRB,LOW);
  delay(1000);
  digitalWrite(DIRA,LOW); //fast stop
  delay(3000);

//Serial.println("PWM full then slow");
  //---PWM example, full speed then slow
  digitalWrite(ENABLE,HIGH); //enable on
  digitalWrite(DIRA,HIGH); //one way
  digitalWrite(DIRB,LOW);
  delay(2000);
  analogWrite(ENABLE,128); //half speed
  delay(2000);
  digitalWrite(ENABLE,LOW); //all done
  delay(10000);
}
   

All the above is in the public domain. It's included here to show which PIN settings work with which code.



原文链接:
http://www.goodliffe.org.uk/arduino/dcmotor.php


A L293D motor controller chip - pinout diagram.
Green - connected to arduino.
Blue - connect to motor
Pink - connect to a battery +ve, eg 12 volt.
L293D and a DC motor

To use this pinout:
The left hand side deals with the first motor, the right hand side deals with a second motor.
Yes, you can run it with only one motor connected.

Arduino Connections
M1 PWM - connect this to a PWM pin on the arduino. They're labelled on the Uno, pin 3 is an example. Output any integer between 0 and 255, where 0 will be off, 128 is half speed and 255 is max speed.

M1 direction 0/1 and M1 direction 1/0 - Connect these two to two digital arduino pins. Output one pin as HIGH and the other pin as LOW, and the motor will spin in one direction. 
Reverse the outputs to LOW and HIGH, and the motor will spin in the other direction.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值