小白一枚,瞎捣鼓
简介
L298N 是一种双H桥电机驱动芯片,可以通过树莓派的GPIO的高低电平来控制L298N的行为。
L298N:
接线
树莓派的引脚
root@raspberrypi:~# gpio readall +-----+-----+---------+------+---+-Model B2-+---+------+---------+-----+-----+ | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM | +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+ | | | 3.3v | | | 1 || 2 | | | 5v | | | | 2 | 8 | SDA.1 | IN | 1 | 3 || 4 | | | 5V | | | | 3 | 9 | SCL.1 | IN | 1 | 5 || 6 | | | 0v | | | | 4 | 7 | GPIO. 7 | IN | 1 | 7 || 8 | 1 | ALT0 | TxD | 15 | 14 | | | | 0v | | | 9 || 10 | 1 | ALT0 | RxD | 16 | 15 | | 17 | 0 | GPIO. 0 | IN | 0 | 11 || 12 | 0 | IN | GPIO. 1 | 1 | 18 | | 27 | 2 | GPIO. 2 | IN | 0 | 13 || 14 | | | 0v | | | | 22 | 3 | GPIO. 3 | IN | 0 | 15 || 16 | 1 | IN | GPIO. 4 | 4 | 23 | | | | 3.3v | | | 17 || 18 | 0 | IN | GPIO. 5 | 5 | 24 | | 10 | 12 | MOSI | IN | 0 | 19 || 20 | | | 0v | | | | 9 | 13 | MISO | IN | 0 | 21 || 22 | 0 | IN | GPIO. 6 | 6 | 25 | | 11 | 14 | SCLK | IN | 0 | 23 || 24 | 1 | IN | CE0 | 10 | 8 | | | | 0v | | | 25 || 26 | 1 | IN | CE1 | 11 | 7 | +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+ | 28 | 17 | GPIO.17 | IN | 0 | 51 || 52 | 0 | IN | GPIO.18 | 18 | 29 | | 30 | 19 | GPIO.19 | IN | 0 | 53 || 54 | 0 | IN | GPIO.20 | 20 | 31 | +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+ | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM | +-----+-----+---------+------+---+-Model B2-+---+------+---------+-----+-----+
L298N接口
- 12V:12V输入正极
- GND:电源地,接电源负极
- 5V:5V输入,暂时不太明白这个的作用
- ENA:A通道使能
- IN1:A通道逻辑输入1
- IN2:A通道逻辑输入2
- OUT1: A通道输出
- OUT2: A通道输出
- ENB:B通道使能
- IN3:B通道逻辑输入1
- IN4:B通道逻辑输入2
- OUT3: B通道输出
- OUT4: B通道输出
接线
树莓派和L298N的接线:
- GPIO.0 -- IN1
- GPIO.1 -- IN2
- GPIO.4 -- ENA
L298N 输入12v供电 OUT1,OUT2接马达
代码测试
代码如下:
import RPi.GPIO as GPIOimport timeGPIO.setmode(GPIO.BOARD)INTA = 16INT1 = 11INT2 = 12GPIO.setup(INTA,GPIO.OUT)GPIO.setup(INT1,GPIO.OUT)GPIO.setup(INT2,GPIO.OUT)# 使能信号GPIO.output(INTA,GPIO.HIGH)# 控制转动方向及制动,参考上图表格GPIO.output(INT2,GPIO.HIGH)GPIO.output(INT1,GPIO.LOW)time.sleep(10)GPIO.cleanup()