1

I am currently busy with a project where i need a servo to turn, i wrote a script in WiringPi (Python & .NET) that does this:

        //Set GPIO scheme.
        WiringPi.WiringPiSetupGpio();

        //Set Pwm settings.
        WiringPi.PwmSetMode((int)PwmMode.Ms);
        WiringPi.PwmSetClock(192);
        WiringPi.PwmSetRange(2000);

        //Setup Servo
        WiringPi.PinMode(18, (int)PinMode.PwmOutput);

        //Test all components.
        WiringPi.PwmWrite(18, 150);
        WiringPi.Delay(500);
        WiringPi.PwmWrite(18, 170);
        WiringPi.Delay(500);
        WiringPi.PwmWrite(18, 150);
        WiringPi.Delay(500);
        WiringPi.PwmWrite(18, 130);
        WiringPi.Delay(500);
        WiringPi.PwmWrite(18, 150);
        WiringPi.Delay(500);

The weird thing is, it works in Python, but that is the only place where it works it seems. Controlling it every other way, will result in the servo only turning to it's maximum right position (and beyond that, a lot of noise and it will probably break it self).

I tested it with GPIO (commandline) too, it does simply not take the correct positions like it does in Python.

When i google the issue, i only find people that say the servo might be broken, this is not the case as it still works perfectly in Python. I use the same clock and range settings in all the solutions, with the position value's that work for me in Python.

Does anyone have a clue what is going on here? It's been holding me up for days already and i'm pretty much out of idea's...

  • 1
    Possibly a better solution is to use (my) pigpio or servoblaster which handle multiple servos on the general GPIO. For example to send servo pulses of length 1730µs on GPIO17 do (sudo pigpiod to start daemon) pigs s 17 1730. – joan Aug 21 '17 at 7:28
  • @joan I have seen that suggestion more than once, but not with an explaination why it would be better yet. It's simply a pwm (or ppm) signal which can be set with any library. Not sure why one would be better for it than the other as it's hardware that handles it(?) Mind to explain why it would be better? – Viezevingertjes Aug 21 '17 at 8:04
  • Multiple servos, in this case more than 2 as that is all you can drive from the two hardware PWM channels. pigpio and servoblaster can provide accurately (hardware) timed servo pulses on all accessible GPIO. – joan Aug 21 '17 at 9:54
0

I finally found out why this happening, it's all in the order of setting the pin's output mode, and the pwm settings. The function names suggest that they are global settings but they are not, so a little design flaw there.

I don't know why this works in any order on the Python version, but it is for GPIO and the .NET wrapper of WiringPi.

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.