---
title: L298N 串接 2個直流馬達(ZK-2WD)
tags: 硬體元件
description: View the slide with "Slide Mode".
---
# L298N 串接 2個直流馬達(ZK-2WD)
<!-- Put the link to this slide here so people can follow -->
## 使用元件
1. 車子 ZK-2WD ([官方](http://www.pu-yang.com.tw/download.html)、[說明書](http://www.pu-yang.com.tw/PDF/1153.pdf))
2. 馬達驅動 L298N
3. 9V電池
4. 樹莓派
## 車子電路與元件串接
[教學](https://sites.google.com/site/studyarduino/04mblock/00-7zu-zhuang-zhi-neng-xiao-che)
## Code
### 程式碼出處
[Raspberry Pi 寵物小車學習套件](https://www.raspberrypi.com.tw/tag/l298n/)
### 1. move_car
```python=
#!/usr/bin/python3
#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
#|R|a|s|p|b|e|r|r|y|P|i|.|c|o|m|.|t|w|
#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# Copyright (c) 2019, raspberrypi.com.tw
# All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
# move_car.py
# control car with argument [w=forward/a=left/s=backward/d=right]
# usage: sudo python3 move_car.py [w/a/s/d]
#
# Author : sosorry
# Date : 08/01/2015
import RPi.GPIO as GPIO
import time
import readchar
Motor_R1_Pin = 16
Motor_R2_Pin = 18
Motor_L1_Pin = 11
Motor_L2_Pin = 13
t = 0.2
GPIO.setmode(GPIO.BOARD) #照版子上面的 pin 腳順序
GPIO.setup(Motor_R1_Pin, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(Motor_R2_Pin, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(Motor_L1_Pin, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(Motor_L2_Pin, GPIO.OUT, initial=GPIO.LOW)
def stop():
GPIO.output(Motor_R1_Pin, GPIO.LOW)
GPIO.output(Motor_R2_Pin, GPIO.LOW)
GPIO.output(Motor_L1_Pin, GPIO.LOW)
GPIO.output(Motor_L2_Pin, GPIO.LOW)
def forward():
GPIO.output(Motor_R1_Pin, GPIO.HIGH)
GPIO.output(Motor_R2_Pin, GPIO.LOW)
GPIO.output(Motor_L1_Pin, GPIO.HIGH)
GPIO.output(Motor_L2_Pin, GPIO.LOW)
time.sleep(t)
stop()
def backward():
GPIO.output(Motor_R1_Pin, GPIO.LOW)
GPIO.output(Motor_R2_Pin, GPIO.HIGH)
GPIO.output(Motor_L1_Pin, GPIO.LOW)
GPIO.output(Motor_L2_Pin, GPIO.HIGH)
time.sleep(t)
stop()
def turnRight():
GPIO.output(Motor_R1_Pin, GPIO.HIGH)
GPIO.output(Motor_R2_Pin, GPIO.LOW)
GPIO.output(Motor_L1_Pin, GPIO.LOW)
GPIO.output(Motor_L2_Pin, GPIO.LOW)
time.sleep(t)
stop()
def turnLeft():
GPIO.output(Motor_R1_Pin, GPIO.LOW)
GPIO.output(Motor_R2_Pin, GPIO.LOW)
GPIO.output(Motor_L1_Pin, GPIO.HIGH)
GPIO.output(Motor_L2_Pin, GPIO.LOW)
time.sleep(t)
stop()
if __name__ == "__main__":
print("Press 'q' to quit...")
while True:
ch = readchar.readkey()
if ch == 'w':
forward()
elif ch == 's':
backward()
elif ch == 'd':
turnRight()
elif ch == 'a':
turnLeft()
elif ch == 'q':
print("\nQuit")
GPIO.cleanup()
quit()
```
## Datasheet
### 1. Raspberry 4 Model B

### 2. L298N

[L298N有聲音,但馬達不會動](https://geek-workshop.com/thread-3159-1-1.html)
[電壓說明](http://bugworkshop.blogspot.com/2013/02/diy-l298n.html)