Arduino PWMを使ってL293DモータードライバでDCモーターを制御する
このプロジェクトは、L293DモータードライバーチップとArduinoのパルス幅変調(PWM)機能を使用して、DCモーターの速度と方向を制御する方法を示しています。このセットアップにより、正確なモーター制御が可能になり、さまざまなアプリケーションに適しています。
このセットアップを使ったプロジェクトのアイデアをいくつか紹介します:
- ロボティクス:小さなロボットの車輪の動きを制御する。
- 自動化装置:自動カーテン、ブラインド、または小型コンベヤーベルトを作成します。
- ファン制御:温度測定に基づいて冷却ファンの速度を調整してください。
- サーモトラッカー:小型の太陽光パネルを日中に太陽に合わせて配置します。
ハードウェア/コンポーネント
- Arduino Uno(または互換ボード)
- L293D モータードライバIC
- DCモーター(L293Dの電圧と電流の制限に対して定格、最大600mA)
- 9V電源(モーター用)
- 接続ワイヤー
- ブレッドボード(推奨)
配線ガイド
L293Dチップは、ArduinoとDCモーターのインターフェースとして機能します。これにより、Arduinoはモーターの速度と方向の両方を制御できます。このチップはモーター用に別の電源供給を必要とします。なぜなら、Arduinoはほとんどのモーターに十分な電流を供給できないからです。(ビデオ内では04:04に)
%%配線%%
コードの説明
ArduinoのコードはL293Dドライバーを制御し、それがDCモーターを制御します。このコードはPWMを使用してモーターの速度を変化させ、デジタル信号を使用して方向を切り替えます。
コード内の主要な設定可能なパラメータは次のとおりです。
#define P1A 10 // Arduino pin connected to L293D input 1A
#define P2A 11 // Arduino pin connected to L293D input 2A
#define EN12 9 // Arduino pin connected to L293D enable pin (1,2EN)
const int speedStep = 15; // Increment for speed changes
const int speedDelay = 1000; // Delay between speed steps (in milliseconds)
そのL293D()モーターの方向と速度を制御します。 'L'はモーターを時計回りに回転させ、 'R'は反時計回りに設定します。spdパラメーターは速度(0-255)を制御します。enモーターを有効(1)または無効(0)にします。(動画の09:24で)
void L293D(char dir,int spd, int en)
{
if(dir =='L') // Clockwise rotation
{
// ...
analogWrite(P1A,spd); // Set speed using PWM
digitalWrite(P2A,LOW);
}else{ // Counter-clockwise rotation
// ...
digitalWrite(P1A,LOW);
analogWrite(P2A,spd); // Set speed using PWM
}
}
ライブプロジェクト/デモンストレーション
ビデオはプロジェクトの動作を示しており、モーターの速度と方向の変化を映し出しています。オシロスコープの読み取り値はモーターを制御するPWM信号を可視化しています。(ビデオの11:07にて)
章
- [00:00] L293DによるDCモーター制御の紹介
- [00:30] プロジェクトのデモンストレーションと説明
- [01:12] L293Dモータードライバーの概要
- [02:08] DCモーターの仕様
- [02:30] L293Dデータシートの説明
- [04:40] 配線の説明とデモンストレーション
- [07:05] Arduinoコードの説明
- [11:07] プロジェクトデモンストレーションとオシロスコープの測定値
/*
* This is the Arduino code to control the speed of a motor using an L293D DC motor driver.
Watch instructions for this video: https://youtu.be/akQoGNUzhHI
// Written for Robojax.com video
* Code is available at http://robojax.com/learn/arduino
*
// Written by Ahmad S. for Robojax.com on
// August 11, 2018 at 13:23 in Ajax, Ontario, Canada
// This code is AS IS without warranty. You may share it if you keep this note with the code.
*/
// DC motor control
#define P1A 10 // define pin 10 as P1A
#define P2A 11 // define pin 11 as P2A
#define EN12 9 // define pin 9 as the 1,2EN enable
const int speedStep =15;
const int speedDelay = 1000;// delay between speed increment
void setup() {
// L293 Motor Control Code by Robojax.com 20180811
Serial.begin(9600);// setup Serial Monitor to display information
pinMode(P1A, OUTPUT);// define pin as OUTPUT for P1A
pinMode(P2A, OUTPUT);// define pin as OUTPUT for P2A
pinMode(EN12, OUTPUT);// define pin as OUTPUT for 1,2EN
Serial.println("Robojax");
Serial.println("L293D Motor Speed Control");
// L293 Motor Control Code by Robojax.com 20180811
}
void loop() {
// L293D Motor Control Code by Robojax.com 20180811
for(int speed=0; speed<=255; speed +=speedStep)
{
L293D('L',speed, 0);//
delay(speedDelay);// delay between each step
}
delay(50);// 50 millisecond delay
// L293D Motor Control Code by Robojax.com 20180810
}//loop end
/*
* L293D(char dir,int spd, int en)
* dir is a character: 'L' for clockwise direction
* or 'R' for counter-clockwise direction
* en is an integer: 1 to rotate, 0 to stop
* spd is the speed value from 0 to 255
*/
void L293D(char dir,int spd, int en)
{
if(dir =='L')
{
if(en ==0){
Serial.println("CW Motor Stopped");
}else{
Serial.print("Rotating CW: ");
Serial.println(spd);//print actual speed value
}
digitalWrite(EN12 ,en);// Enable 1A and 2A
analogWrite(P1A,spd);// send PWM with spd value to P1A
digitalWrite(P2A,LOW);// LOW signal to P2A
}else{
if(en ==0){
Serial.println("CCW Motor Stopped");
}else{
Serial.print("Rotating CCW: ");
Serial.println(spd);//print actual speed value
}
digitalWrite(EN12 ,en);// Disable 1A and 2A
digitalWrite(P1A,LOW);// Keep this pin LOW
analogWrite(P2A,spd);// send PWM with spd value to P2A
}
}//L293D end
++
/*
* This is the Arduino code to control the speed of a motor using an L293D DC motor driver.
* This code will control the motor with different directions and speed.
Watch instructions for this video: https://youtu.be/akQoGNUzhHI
// Written for Robojax.com video
* Code is available at http://robojax.com/learn/arduino
*
// Written by Ahmad Shamshiri for Robojax.com on
// August 11, 2018 at 13:23 in Ajax, Ontario, Canada.
// This code is AS IS without warranty. You may share it if you keep this note with the code.
*/
// DC motor control
#define P1A 10 // define pin 10 as P1A
#define P2A 11 // define pin 11 as P2A
#define EN12 9 // define pin 9 as the 1,2EN enable
const int speedStep =15;
const int speedDelay = 1000;// delay between speed increment
void setup() {
// L293 Motor Control Code by Robojax.com 20180811
Serial.begin(9600);// setup Serial Monitor to display information
pinMode(P1A, OUTPUT);// define pin as OUTPUT for P1A
pinMode(P2A, OUTPUT);// define pin as OUTPUT for P2A
pinMode(EN12, OUTPUT);// define pin as OUTPUT for 1,2EN
Serial.println("Robojax");
Serial.println("L293D Motor Speed Control");
// L293 Motor Control Code by Robojax.com 20180811
}
void loop() {
// L293D Motor Control Code by Robojax.com 20180811
L293D('L',255, 1);// maximum speed to the LEFT
delay(3000);// wait for 3 seconds
L293D('L',255, 0);// stop motor
delay(2000);// wait for 2 seconds
L293D('L',127, 1);// half speed to the LEFT
delay(3000);// wait for 3 seconds
L293D('L',255, 0);// stop motor
delay(2000);// wait for 2 seconds
L293D('R',90, 1);// slow to the RIGHT
delay(3000);// wait for 3 seconds
L293D('L',255, 0);// stop motor
delay(2000);// wait for 2 seconds
// L293D Motor Control Code by Robojax.com 20180810
}//loop end
/*
* L293D(char dir,int spd, int en)
* dir is a character: 'L' for clockwise (CW) direction
* or 'R' for counter-clockwise (CCW) direction.
* en is an integer: 1 to rotate, 0 to stop.
* spd is the speed value from 0 to 255.
*/
void L293D(char dir,int spd, int en)
{
if(dir =='L')
{
if(en ==0){
Serial.println("CW Motor Stopped");
}else{
Serial.print("Rotating CW: ");
Serial.println(spd);//print actual speed value
}
digitalWrite(EN12 ,en);// Enable 1A and 2A
analogWrite(P1A,spd);// send PWM with spd value to P1A
digitalWrite(P2A,LOW);// LOW signal to P2A
}else{
if(en ==0){
Serial.println("CCW Motor Stopped");
}else{
Serial.print("Rotating CCW: ");
Serial.println(spd);//print actual speed value
}
digitalWrite(EN12 ,en);//enable 1A and 2A
digitalWrite(P1A,LOW);// Keep it LOW P1A
analogWrite(P2A,spd);// send PWM with spd value to P2A
}
}//L293D end
リソースと参考文献
ファイル📁
ファイルは利用できません。