I have been lately working on a ball tracker robot using image processing and I have interfaced Arduino and Raspi via USB.

While the servo.write() function works perfectly when i program the Arduino via Raspi, The problem occurs when I send a serial character to arduino (also via raspi) and make the arduino read the character and subsequently drive the servo to a certain angle according to the serial character received. The servo just doesn't turn this time. I didn't ground obviously as the USB will share the ground.The following are the python and Arduino codes. python serial code

arduino serial recieve code


It would probably be useful to see the whole Arduino sketch but from the screen shot, the logic appears to be:

  set servo to 135;
  wait for data from the serial port;
  if the data received is "4" then
    set the servo to 170;

If this is indeed the case, then if we look carefully at the logic, as soon as you set the servo to be 170 we end the loop at start immediately back at the start of the loop which then sets the servo to be 135. What that means is that the servo would be asked to turn to position 170 and then almost instantly asked to turn back to 135 with the result that you wouldn't perceive any actual motion.


Most servos expect a 50Hz PWM signal, with the duty cycle varying from 7.5% to 12.5% for the two extreme positions. (Equivalently, a 20ms period, with 1.5ms ON for the extreme left position, 2ms for the middle, 2.5ms for the extreme right). (Yes, I know about PPM, but getting into that would confuse the OP without adding clarity).

Most servos are also pretty finicky on those timings. You can't just feed them a 7.5% duty-cycle signal at 1kHz and expect them to work.

So the answer really depends on what servo library you are using, and, of course, which particular arduino (or arduino-lookalike) board you are using, and whether you are using a dedicated servo driver chip. Some boards can only do PWM (pulse-width modulation) output by bit-banging, while some have hardware PWM output, and you usually set that by doing an analogWrite(). Even then, your particular processor may not support arbitrary PWM frequencies, and those it does may be too high for your servos. And since it's pretty hard to get really steady PWM output with bit-banging, you may end up in a situation where your servo is trembling, so to speak, around its target location.

The situation for a low-end processor (e.g., Digispark, or Trinket) may be even worse, as the serial/usb connection would definitely be bit-banged,and so would the PWM output. Or there may be a situation where the serial code is disabling interrupts and really interfering with the bit-banged pwm output.

i could go on forever. If you post your exact hardware setup, and what servo libraries you are using, I may be able to help you more.


  • Also, don't even think of driving a servo from the π`s GPIO pins. Use something like this adafruit.com/products/2327 and save yourself a lot of trouble.
    – JayEye
    Feb 10 '16 at 5:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.