L298N怎么用pwm来控制小车速度



  • 如题,怎么控制小车速度呢?还有怎么接线和有示例代码吗?



  • 多谢版主回复,一直没顾得上回来看看。


  • administrators

    python也有pwm控制的,L298N的enable就是pwm的接线口。把它接到pi的任意GPIO口上,就可以了。下面是个控制LED灯的代码示例:

    import time
    import RPi.GPIO as GPIO
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(12, GPIO.OUT)
    
    p = GPIO.PWM(12, 50)  # channel=12 frequency=50Hz
    p.start(0)
    try:
        while 1:
            for dc in range(0, 101, 5):
                p.ChangeDutyCycle(dc)
                time.sleep(0.1)
            for dc in range(100, -1, -5):
                p.ChangeDutyCycle(dc)
                time.sleep(0.1)
    except KeyboardInterrupt:
        pass
    p.stop()
    GPIO.cleanup()
    
    

登录后回复

Looks like your connection to 首页 was lost, please wait while we try to reconnect.