I am a very new to raspberry pi and hardware. I am trying to spin a brushless motor with ESC. below is the code (taken from a youtube video) that spins the motor till 9 and then slows it down till 4. but I want to run the motor without loops and without changing the duty cycles. Means when i run the program the motor should spin and when i stop the program the motor should stop spinning. Please update the below code.
Sorry for my bad English. I hope I am clear what I need.
Below is the code that I have taken from a video.
Code:
p = GPIO.PWM(7, 50)
p.start(0)
print ("starting 0")
time.sleep(3)
p.ChangeDutyCycle(3)
print("start")
time.sleep(5)
while True:
i = 4
while i<10:
print(i)
p.ChangeDutyCycle(i)
time.sleep(.05)
i +=.02
while i>4:
print(i)
p.ChangeDutyCycle(i)
time.sleep(.05)
i -=.05
I am assuming the motor you want to control is a brushed DC motor and not a brushless, otherwise you would need a driver between the RPI and the motor. For a Brushed DC motor, you control the speed of the motor by changing the duty cycle of the PWM output. If you don't want to change the speed, simply set a dutycycle and stay with it. Check this example based on https://sourceforge.net/p/raspberry-gpio-python/wiki/PWM/:
Here we are using GPIO 7 with a switching frequency of 50Hz and 10% of dutycycle. Adjust the values to suit your needs.