Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. # Simple demo of of the PCA9685 PWM servo/LED controller library.
  2. # This will move channel 0 from min to max position repeatedly.
  3. from __future__ import division
  4. import time
  5.  
  6. import Adafruit_PCA9685
  7.  
  8.  
  9. # Initialise the PCA9685 using the default address (0x40).
  10. pwm = Adafruit_PCA9685.PCA9685()
  11.  
  12. servo_min = 150 # Min pulse length out of 4096
  13. servo_max = 600 # Max pulse length out of 4096
  14.  
  15. def set_servo_pulse(channel, pulse):
  16. pulse_length = 1000000 # 1,000,000 us per second
  17. pulse_length //= 60 # 60 Hz
  18. print('{0}us per period'.format(pulse_length))
  19. pulse_length //= 4096 # 12 bits of resolution
  20. print('{0}us per bit'.format(pulse_length))
  21. pulse *= 1000
  22. pulse //= pulse_length
  23. pwm.set_pwm(channel, 0, pulse)
  24.  
  25. # Set frequency to 60hz, good for servos.
  26. pwm.set_pwm_freq(60)
  27.  
  28. print('Moving servo on channel 0, press Ctrl-C to quit...')
  29. while True:
  30.  
  31.  
  32. # Move servo on channel O between extremes.
  33. pwm.set_pwm(0, 0, servo_min)
  34. time.sleep(1)
  35. pwm.set_pwm(0, 0, servo_max)
  36. time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement