Arduino UNO 的電路圖如下:
硬體材料:
1. Arduino UNO R3 板子 x 1
2. 公對母杜邦線 x 3
3. 公對公杜邦線 x 11
4. 5V 直流馬達 x 2
5. 紅外線接收器 x 1
6. L298N 馬達驅動板 x 1
7. 小車底盤 x 1
8. 小車輪子 x 2
9. 萬向輪 x 1
10. 緊固件 x 4
11. 長螺絲 x 4
12. 短螺絲 (長8根、短2根)
13. 螺帽 x 10
14. 行動電源 x 1
15. USB Cable線 x 1
16. 橡皮筋多條
L298N馬達驅動板
IN1 接 Arduino D5
IN2 接 Arduino D6
IN3 接 Arduino D9
IN4 接 Arduino D10
12V 接 Arduino 5V
GND 接 Arduino GND
5V 接 Arduino Vin
紅外線接收器
GND接Arduino GND
訊號接 Arduino D11
Vcc接 Arduino 3.3V
硬體完成圖:
在接馬達的線部分,是用杜邦線接成下圖的樣子:
用某第四台遙控器的上下左右與OK鍵來控制小車:
按鍵說明:
向上鍵是前進。
向下鍵是後退。
向左鍵是左轉。
向右鍵是右轉。
OK鍵是停止。
程式碼是參考Cooper Maa的文章使用L298N 模組控制直流馬達
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <IRremote.h> | |
int RECV_PIN = 11; | |
IRrecv irrecv(RECV_PIN); | |
decode_results results; | |
/* Input for leftMotor: | |
IN1 IN2 Action | |
LOW LOW Stop | |
HIGH LOW Forward | |
LOW HIGH Backward | |
HIGH HIGH Stop | |
*/ | |
const int leftMotorIn1 = 5; | |
const int leftMotorIn2 = 6; | |
/* Input for rightMotor: | |
IN3 IN4 Action | |
LOW LOW Stop | |
HIGH LOW Forward | |
LOW HIGH Backward | |
HIGH HIGH Stop | |
*/ | |
const int rightMotorIn3 = 9; | |
const int rightMotorIn4 = 10; | |
void setup() | |
{ | |
pinMode(leftMotorIn1, OUTPUT); | |
pinMode(leftMotorIn2, OUTPUT); | |
pinMode(rightMotorIn3, OUTPUT); | |
pinMode(rightMotorIn4, OUTPUT); | |
irrecv.enableIRIn(); // Start the receiver | |
} | |
void loop() | |
{ | |
if (irrecv.decode(&results)) { | |
irrecv.resume(); // Receive the next value | |
} | |
if( results.value == 0xFF0AF5 ) { | |
forward(); | |
} | |
if( results.value == 0xFF8A75 ) { | |
backward(); | |
} | |
if( results.value == 0xFF4AB5 ) { | |
left(); | |
} | |
if( results.value == 0xFFCA35 ) { | |
right(); | |
} | |
if( results.value == 0xFF2AD5 ) { | |
motorstop(); | |
} | |
} | |
void motorstop() | |
{ | |
digitalWrite(leftMotorIn1, LOW); | |
digitalWrite(leftMotorIn2, LOW); | |
digitalWrite(rightMotorIn3, LOW); | |
digitalWrite(rightMotorIn4, LOW); | |
} | |
void forward() | |
{ | |
digitalWrite(leftMotorIn1, HIGH); | |
digitalWrite(leftMotorIn2, LOW); | |
digitalWrite(rightMotorIn3, HIGH); | |
digitalWrite(rightMotorIn4, LOW); | |
} | |
void backward() | |
{ | |
digitalWrite(leftMotorIn1, LOW); | |
digitalWrite(leftMotorIn2, HIGH); | |
digitalWrite(rightMotorIn3, LOW); | |
digitalWrite(rightMotorIn4, HIGH); | |
} | |
void right() | |
{ | |
digitalWrite(leftMotorIn1, HIGH); | |
digitalWrite(leftMotorIn2, LOW); | |
digitalWrite(rightMotorIn3, LOW); | |
digitalWrite(rightMotorIn4, LOW); | |
} | |
void left() | |
{ | |
digitalWrite(leftMotorIn1, LOW); | |
digitalWrite(leftMotorIn2, LOW); | |
digitalWrite(rightMotorIn3, HIGH); | |
digitalWrite(rightMotorIn4, LOW); | |
} |
最後附上Demo影片:
沒有留言:
張貼留言