Buddhika Bandara
Buddhika Bandara

Reputation: 371

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.

Upvotes: 1

Views: 1030

Answers (2)

user13526151
user13526151

Reputation:

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()

Upvotes: 0

Nick
Nick

Reputation: 165

I have exactly the same situation. You can of course program the motors separately but it is nice to use the robot class. Looking into the gpiocode for this, I find that in our case the left and right tuples have a third parameter which is the pin for PWM motor speed control. (GPIO Pins 12 13 18 19 have hardware PWM support). The first two outout pins in the tuple are to be signalled as 1, 0 for forward, 0,1 for back. So here is my line of code: Initio = Robot(left=(4, 5, 12), right=(17, 18, 13))

Hope it works for you! I have some interesting code on the stocks for controlling the robot's absolute position, so it can explore its environment.

Upvotes: 1

Related Questions