PWM noob question

Hi all,

I’ve just discovered the library, and I’m having a tough time with PWM. I’m running Processing on a Raspberry Pi Zero, and I’m wanting to interface with it’s IO pins.

The page says to “Use the SoftwareServo class below for the time being. Hardware PWM has yet to be made available by the hardware platforms we support.” Does that mean that this code doesn’t work anywhere, and that I should be working with the SoftwareServo class instead?

I’ve used the SoftwareServo class to dim a light with the following code:

SoftwareServo servo;

int count = 0;

void setup() {
  servo = new SoftwareServo(this);
  // On the Raspberry Pi, GPIO 4 is pin 7 on the pin header,
  // located on the fourth row, above one of the ground pins

void draw() {
  // Here we are using the Software Servo class as a Kludge (as recommended by Processing) for PWM.
  // The only software PWM available in the library is
  // SoftwareServo, intended for servo motors, not LEDs, or DC motors.
  // Therefore:
  //    100% PWM == 180 degrees.
  //     50% PWM == 90 degrees.
  //      0% PWM == 0 degrees.

  float angle = 0 + count;

void updateCount() {
  count = count + 45;
  if (count > 180) {
    count = 0;

It works reasonably well, but 100% PWM (190 degrees) is nowhere near the LED’s full capacity, and at 0% PWM (0 degrees) the LED is still on/dimly lit.

Furthermore, the light visibly pulsates.

Contrast this with the steps I followed in this tutorial to do Hardware PWM, and the LED goes to 100% brightness, to 0% (off), with no issues, and the light doesn’t pulsate.


Suggestions below are for a PWM hack using SoftwareServo and not for an actual servo motor. :)

Try setting the defaults to match your frameRate; be sure to display this to see what the actual frameRate is.

Otherwise there is (see note at bottom) a long gap between servo writes; default state of GPIO pin may be a LOW on RPi (TBD I do not know at this time).

Defaults (sending servo write once per frame):
T = 2400 µs
f = 1/T = 416 Hz

Assuming frameRate is 60 fps:
f = 60 Hz
T = 1/60 = 0.01666666666 s = 16666 µs

minPulse = 0; //Use a higher value if it does not like 0
maxPulse = 16666;

Let me know what the outcome is.


I have not tried this and do not use Processing on a RPi; I may just fire it up and try this.
When I work with hardware I always like to see the the actual outputs on an oscilloscope.

I will try this tomorrow.
I need an excuse to clean my workstation and fire up my RPi.


Hi glv,

Thanks so much for your help! I’ve implemented what you’ve suggested, and here’s what I’ve found:

  • Your suggestion completely solved the brightness issue: at 100% (180 degrees) the light is as bright as it can be.

  • minPulse = 0; doesn’t solve the problem of a low light being emitted when there should be none. I tried minPulse = 1 and minPulse = 10. Still the same issue.

  • The visible pulsating problem is solved for anything at or above about 45 degrees. Below that you can notice pulsating still.

I printed out the actual frameRate and it fluctuates in the neigbourhood of 60.

Any suggestions on how to solve the two remaining issues?

I’m so grateful for your help glv :slight_smile: