• AVR Freaks

Hot!To run servo sg90 from PIC16F1938 through PCA9685 controller

Author
softlab
Super Member
  • Total Posts : 306
  • Reward points : 0
  • Joined: 2011/03/30 09:26:30
  • Location: 0
  • Status: offline
2019/10/10 23:54:00 (permalink)
0

To run servo sg90 from PIC16F1938 through PCA9685 controller

Hi,
I need to turn the 3 Servos (sg90) in desired angles independently to one another. I am using PIC16F1938 and PCA9685 controller. I can able to move the servo in one direction. But, I am not fully clear on how to move to the desired angles for example, how to center the servo (0 degree), or move to 45 degree, 90 degree, etc.

In the PCA data sheet, it says 0 to 4096 steps available for On & Off LEDS. In sg90 data sheet, 1.5 ms for 0 degree(center the Servo), 2ms for 90 degree and 1 ms for -90 degree. I am little bit confused.
Can you please provide any sample code in C or any other valuable information to achieve my task.
Thank you all.
#1

2 Replies Related Threads

    Ian.M
    Super Member
    • Total Posts : 13267
    • Reward points : 0
    • Joined: 2009/07/23 07:02:40
    • Location: UK
    • Status: offline
    Re: To run servo sg90 from PIC16F1938 through PCA9685 controller 2019/10/11 04:10:57 (permalink)
    +1 (1)
    I wouldn't do it that way* as you only get about 200 possible servo position between the 1ms minimum pulse position and the 2ms maximum pulse position.
     
    However if you must, the method is to set up the PCA9685 prescaler for as close as possible to a 50Hz PWM period then limit the PWM duty cycles you send to it to keep the output pulse widths in the range 1ms to 2ms (or whatever your servo's limits actually are if they exceed the original Futaba servo control signal specs).
     
    Adafruit's  page for their PCA9685 board has links to a tutorial and Arduino library.  A quick look through the library source and examples gives me the impression that it wont be too much trouble to flatten the Arduino C++ classes and methods to C functions and port it to XC8 using a *FIXED* choice of I2C port and Microchip's I2C functions in place of the Arduino ones. 
     
    * Using a PIC a CCP module in output compare mode, and a free-running 16 bit timer (odd-numbered), you can generate jitter free servo pulses with 1us or better precision and very low CPU overhead on any CCP pin.  Add an external 74HC238 3 to 8 line decoder, controlled by three I/O pins + the CCP pin. and you can easily control up to eight servos concurrently from a single CCP module.
     
    Edit: corrected Futaba pulse limits.
    post edited by Ian.M - 2019/10/13 09:36:59

    --
    NEW USERS: Posting images, links and code - workaround for restrictions.
    I also support http://picforum.ric323.com because this forum is sometimes too broken to use!
    #2
    Ian.M
    Super Member
    • Total Posts : 13267
    • Reward points : 0
    • Joined: 2009/07/23 07:02:40
    • Location: UK
    • Status: offline
    Re: To run servo sg90 from PIC16F1938 through PCA9685 controller 2019/10/13 06:59:31 (permalink)
    0
    Re: my comment above.  I've just updated my PIC16F88 CCP module single servo example to handle 8 servos (or 10 if you relax the framerate slightly and put up with a fugly DEMUX circuit).   Code attached to the same post as the previous version: 
    https://www.microchip.com/forums/FindPost/819452
     
     

    --
    NEW USERS: Posting images, links and code - workaround for restrictions.
    I also support http://picforum.ric323.com because this forum is sometimes too broken to use!
    #3
    Jump to:
    © 2019 APG vNext Commercial Version 4.5