python-3.xraspberry-pi2roboticsgpiozero

how can I use gpiozero robot library to change speeds of motors via L298N


In my raspberry pi, i need to run two motors with a L298N. I can pwm on enable pins to change speeds. But i saw that gpiozero robot library can make things a lot easier. But When using gpiozero robot library, how can i alter speeds of those motors by giving signel to the enable pins.


Solution

  • To alter the speeds you need a PWM signal which can be done without using any library.

    To create a PWM instance:
    
    p = GPIO.PWM(channel, frequency)
    To start PWM:
    
    p.start(dc)   # where dc is the duty cycle (0.0 <= dc <= 100.0)
    To change the frequency:
    
    p.ChangeFrequency(freq)   # where freq is the new frequency in Hz
    To change the duty cycle:
    
    p.ChangeDutyCycle(dc)  # where 0.0 <= dc <= 100.0
    To stop PWM:
    
    p.stop()