So I'm rather new to Arduino, but it seemed like a great way to re-introduce myself to the electronic projects I loved as a EE student. As is often the case, I finished school and put my lab kit and tools on the shelf to gather dust because my job requires me to physically build absolutely nothing . ![]()
But here I am with two little boys and an electric train set that needs some crossing gates and flashing lights. So I ordered up some parts and started tinkering. I settled on the WS2812 LEDs because they're small and addressable. Having RGB capability is neat but not really applicable to this project (though I'm considering some kind of Easter egg mode for my boys to discover). At this point I'm starting small and have 1 servo-enabled crossing gate, 2 WS2812 chips chained together, and a momentary push button to indicate when the train is approaching the crossing (this will be replaced with a reed switch and train-mounted magnet, but I'm still writing the code and working on a breadboard).
After creating some code that basically followed the servo library examples and NeoPixel library examples, I was left with a working system with one annoying flaw: the servo was jittery at 90°. With only 1 servo and 2 LEDs (set to brightness 32) I didn't think I would have issue running straight off my RedBoard, but after I saw the jitter power was the first thing I looked at. I added a 1000 ?F electrolytic cap across my power rails; that produced no change. I thought maybe my servo was bad so I tried three different servos; they all behaved the same. I tried a different Arduino board; no change. I thought maybe the momentary switch was noisy so I pulled it altogether; no change. Finally I commented out all the momentary switch code and just stepped through the two modes with a short delay between, and that finally solved the issue. So at that point I thought it had something to do with input. That turned out to be a red herring and I wasted some time inverting the logic of my input (to no effect).
Then I took a broader view. I basically have two states: train crossing or cars crossing. When cars are crossing you do nothing with the gate (leave it at 90, which is where the train crossing state left it and leave the signal lights off). In fact all I had in that block of code was set LED 0 to off (0, 0, 0), set LED 1 to off (0, 0, 0), and write the data to the string. I thought that perhaps looping through this code continuously might not be the best idea, so I added a 20ms delay. That helped immensely but did not completely solve the jittery servo. The continuous jitter was gone but it would still jump chronically, at a regular rate. I increased this delay to 100ms. That made the chronic jumps occur further apart. I thought maybe I should tell the servo to go to 90° in this function (even though it was already there) but that didn't help either.
Finally I dug into the full documentation of the servo library (I know...RTFM). Therein I saw the detach function. I decided that it would be a good idea to detach the servo in a function where I wasn't using it (cars crossing) and then attach it in a function where I was (train crossing). That solved my issue completely. There was no more jitter even with the push button in the circuit (and in the code).
Because I couldn't find anything useful when Googling for a solution I thought I would take the time to write this up. So if you have a "noisy servo with input", "noisy PWM output with input button", or just a "jittery servo" maybe this post will help you. Also if you have some wisdom to explain this behavior I'd love to learn where I went wrong. I suspect my fix is a hack and not necessarily the best solution. In any case thanks for reading my lengthy first post.