Page 1 of 1

Uno servo drive for camera mount from joystick

PostPosted: Mon May 11, 2015 3:28 pm
by Doug Coulter
Part of my lan of things project includes a camera on the Pi host. Well, it's nice to be able to point it at different places now and then, and AdaFruit has a nice pan/tilt mount for a camera, so...here's an arduino sketch to move the camera around with a joystick input. I used the old switch type joystick for this. I just wired the joystick black wire to ground and made the inputs 2-5 input pullups - a no-parts connection.
I did add a 7805 regulator running off the "Vin" of the arduino so its tiny regulator wouldn't have to drive servos - it can barely do the little ones, and gets hot really fast doing it. The other motivation was that I might want the Arduino's 5v rail really stable since it's also the a/d reference and I may add things to those pins later. The servos bounce the 5v line around if you use it to power them, now they have no effect as long as the main Vin is high enough so that the board regulator doesn't drop out. I'm using your basic wall wart at the moment, but I do have a 7.65v regulated supply in the basement running the other arduino in my lashup, and could use that instead, saving a wart and its vampire drain - the basement supply is from a switcher and a lot more efficient, since the supply UPS for 13v nominal is already running anyway. May as well draw a little more from it and have this UPS in the bargain. Remember, I'm off the grid and every watt hour counts here. Waste not...

Realizing that at some point this will be an additional slave unit on the Pi, using my protocol (it'll be the second one) and that there might be other things I want to do with the other arduino pins (soil temperature/moisture, solar insolation etc) and that it might all be time-sensitive, I rewrote the sample code to not use delay(), but use state machines and keep track of time to decide when to do some things. As well as speed of motion, allowing time for the servo to catch up to the most recent command, it's a good idea (as I found out the hard way) to detach the servo interface when it's not in use. If you command a move - or sometimes even if you don't, the lost motion in the servo gearing might allow the load to "coast" inertially to the point where the servo built-in logic wants to move it back. Whereupon it slams into the other stop, and you have an oscillation. Rather than add friction to prevent this (unreliable), this code simply waits a bit after the last command (all the limits and times are #defines) and then disconnects from the servo, rendering it non power drawing and incapable of oscillating.

Later I will add the "command" state machine to this to accept commands over the shared Pi serial bus, but in this simple form, it's more reusable for other things, so I thought I'd put it up here for us all. I'm only doing this in degrees at this point, but if you need more precision, you can do it in microseconds - see the documentation for the servo library.

Campan.zip
Arduino sketch to control two servos and not waste time/cycles so there's more left for other code you or I might add.
(1.41 KiB) Downloaded 279 times


Here's what it looks like on my desk (in the new digs) along with in the lower right, what I'm vaping with right now (darn glad I'm not smoking, this isn't perfect but it costs less and does a LOT less harm - I can tell in increased lung capacity since I quit cigarettes).
CamPan.JPG
What it looks like on the desk.