Jump to content
 

Raspberry Pi Pico - PCA9685 Servo Controller


aleopardstail
 Share

Recommended Posts

Hi, have gotten round to uploading this (finally)

 

https://github.com/aleopardstail/PCA9685_Servo_Controller

 

this is a pair of C++ object classes for working with the wonderful little PCA9685 PWM controller board, with code classes designed to manage servos. Its a work in progress but should be able to drive point motor servos as is in a fast or a slower operating mode. Hope to add code for things like signal arm bounce etc as well eventually.

 

currently the "example" is a bit minimal on github as the test code I have been using also uses a few other things I've been working on, e.g. a SSD1306 OLED controller and a TTP229F touch pad controller, however the servo bits are separate and may be useful to someone who wants to use this sort of device to drive servos, or is just curious how to talk to the PCA9685 over the I2C serial bus.

 

this was written as "meware" initially so the comments are aimed at me but its not hard to actually use, the controller class is required and can drive servos directly if you manage the desired angles yourself - the optional servo class allows setting minimum, mid point and maximum angles per servo, managing servo calibration settings (currently set in the constructor, needs a control interface adding) and enabling slow speed operation (both a constant time to move and a constant angular velocity mode).

 

open to comments and suggestions, note the "readme" file is a bit mangled as it was written as plain text and then github tried to be cleverer than me in formatting it (it you read it as plain text is makes more sense).

 

I hope to develop this more and provide a few more examples

  • Like 2
Link to post
Share on other sites

1 hour ago, KingEdwardII said:

Are you in effect producing the equivalent of the Adafruit 16 channel servo controller described here:

 

https://learn.adafruit.com/adafruit-16-channel-servo-driver-with-raspberry-pi/using-the-adafruit-library

 

Yours, Mike.

 

wasn't aware of that but not directly no, that is for the Raspberry Pi computer (price varies but £60+), using the python language, mine is for the Raspberry Pi Pico microcontroller (£3.60 each) much better suited to being gently nailed to the underside of a baseboard and forgotten about.

 

additionally the code I have written can do a bit more, mostly because I was bored and curious - this started out as an exercise in "I wonder if I can..." and sort of grew, for example the Adafruit code is mostly concerned with moving servos - which makes sense, with functions to set a servo to a set angle, and set the range it can sweep through - looks aimed at driving basic robotics and components on small vehicles (e.g. moving the position of a sensor to change where it looks). And this being Adafruit it will do this in a very good and very robust way (I like their code a lot and love the way they do the articles around them).

 

where as mine can do this but can also allow the user to say "move from where you are now to +45 degrees, and take 3 seconds doing so", or "I wish you to move to +45 degrees at a speed of 2 degrees per second", which may be more useful for model railways (thinking point motors which is actually what I was doing this for and hope to use it for)

 

it is basically talking to the same bit of kit, Adafruit make a nice breakout board, there are a range of "clones" of it on eBay as well (its also good for controlling LEDs, which is what the chip on the board is actually designed to do)

 

I posted it in case someone finds it useful, I'm hoping to have a fiddle and get the Pi Pico to act as a DCC accessory decoder to drive these things at a reasonable cost but have yet to start with that bit - however the test example I have takes this code and sticks a small screen and a keypad with it to allow direct control - e.g. select up to 16 servos and set them to "throw" or "clear" with a single button press and then have them go and do the slow movement thing as configured.

 

thought the code may be useful and hadn't seen an existing library for the RP2040 based Pi Pico, so had a go at writing one.

 

if you are using an actual Raspberry Pi this code may or may not even compile, not tried it as I'm not using the Pi400 I have in that way

 

Link to post
Share on other sites

2 hours ago, aleopardstail said:

Raspberry Pi computer (price varies but £60+)

Not sure that is the right comparison point.

 

Pi 4B = £33.90 + SD card (~£5.00)

 

But the really direct comparison is with the Pi Zero = £4.80 + SD card (~£5.00)

The Pi Zero W for £9.30 adds in WiFi capability. The Pi Zero is a full computer (Linux, etc) not just a microcontroller like the Pico.

 

For both the Pi Pico and the Pi Zero you probably additionally need header pin sets, plus a power supply of some kind.

 

The Adafruit board with its library can do the kinds of movements that you describe (e.g. "move from where you are now to +45 degrees, and take 3 seconds doing so") and the CMME DCC servo controller that I've been working with has a programmed semaphore signal bounce, although their unit uses an Arduino rather than a Pi to drive the Adafruit board.

 

CMME have done the DCC accessory controller part that you mention.

 

I think that what you're doing is great - just wanted you to be aware that there is some other stuff out there that is working in this same area.

 

Yours,  Mike.

  • Like 1
Link to post
Share on other sites

went with the cost of a Pi4 last time I looked, seemed about £60 to get one working - the device itself is a bit, less, regardless the point there is its a very different (and even with the Pi Zero) way more powerful device - there are times though you want a microcontroller not a full computer (usually anything where you need full control). Horses for things horses run round of course.

 

the Adafruit C library is quite basic, but it can be used by an application to do pretty much anything, the Python one appears to have more utility stuff built it - both however are for the Arduino platform - though to be honest the bulk of any changes required to run on the RP2040 platform (or a Pi proper) will be reasonably basic and limited to the I2C interface code, the PCA9685 doesn't care 3.141 hoots what it talks to, just that they talk nicely.

 

nice to know what else is out there though, good to see different (and more robust) approaches, and very much thanks for the heads up.

 

Just curious here to how all this works, with the intention of producing a DCC accessory servo type decoder/controller for my own layout and in the process maybe teaching this old dog (cat?) a few new tricks

 

 

:)

Link to post
Share on other sites

  • 3 weeks later...

now I actually have the beginnings of a layout, here is a picture of the somewhat homebrew servo controller as it currently is

 

IMG_2234.JPG.90047cad3038490e8eb513a391a65595.JPG

 

The green board is the Pi Pico RP2040 microcontroller, black button is a reset button, which resets just the MCU, need to adapt to cut the power to the display and keypad to reset them as will (eventually will "reset" by actually cutting the power)

 

the PCA9685 is the blue board, then there is a 0.96" OLED and a touchpad for user input. It needs, but does not yet have, an EEPROM chip adding to be able to save settings. The software is very much in "test" mode currently but useful for making adjustments when installing servos.

 

also as yet no way to talk to the outside world, intended to provide a DCC interface to actually control the servos but also an RS485 interface to a C/MRI connection to a PC or other device so this can provide actual feedback.

 

Intended to have position switches on the points feeding back into this, probably via SN74HC165 shift registers, and likely some LEDs via SN74HC595 shift registers to show the "actual" position (driven by software though not directly by the switches themselves)

 

so far it works

  • Craftsmanship/clever 1
Link to post
Share on other sites

I'm keen to give this a shot.

 

Although the Pi is more functional, it also takes time to start and crucially, needs to be shutdown correctly. I have implemented my own homebrew approach using an Arduino but the Pico in combination with this code is appealing , so many thanks and much appreciated.

  • Like 1
Link to post
Share on other sites

2 hours ago, yellowp said:

I'm keen to give this a shot.

 

Although the Pi is more functional, it also takes time to start and crucially, needs to be shutdown correctly. I have implemented my own homebrew approach using an Arduino but the Pico in combination with this code is appealing , so many thanks and much appreciated.

 

the actual servo control is managed by the PCA board, the code to drive is isn't huge so should run fine on arduino as well - the Pico is better for running a screen with a full frame buffer.

 

note this is the RP2040 Pi Pico microcontroller, a £3.60 device, not the full on raspberry Pi computer (I've not tried but I suspect the code would run on a Pi4 without much alteration though)

 

mostly went with the Pi Pico as a learning exercise to be honest, they work and seem to work well

Link to post
Share on other sites

@aleopardstail

 

Thanks again for sharing your C code. I have painfully but successfully used it to get my micropython code working on the Raspberry Pi Pico. I am controlling 8 sets of points on my N scale layout using a rotary encoder with a 128 x 64 oled display. Had Bluetooth points control working too. Happy septuagenarian. On to the next challenge - throttle control over Bluetooth. Awe, shucks Hornby have beaten me to it!

Edited by Auchavan Yard
corrections and expansion
  • Like 2
Link to post
Share on other sites

glad it was useful to someone :)

 

have adapted the controller its running on slightly with an RS485 interface, it now speaks a slightly tweaked by of C/MRI code that also runs on the RP2040 board and can drive the points from JMRI.

 

I'm amazed it worked really, but work it does

 

IMG_2254.JPG.0217ec789df6720a59dfff2a4218a50e.JPG

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...