Between 0 and 1 – PWM with Raspberry Pi

Between 0 and 1 – PWM with Raspberry Pi

Switch on and off an LED and let run a motor at one speed and direction is easy, but what if I want to e.g. control the brightness of an LED or control motor-speed?

A kind of analog signal is needed:
This is done with Pulse-width modulation (PWM). PWM is a “modulation technique that conforms the width of the pulse, formally the pulse duration, based on modulator signal information.” (Wikipedia).

In a nutshell:
The average value of voltage is depending on the frequency the load is switched on and off.

Basically there are two ways to create PWM:

Hardware PWM

  • very fast (max. possible frequency / period)
  • software independent, independent from program flow
  • clean signal

BUT:

  • not all pins might support HW PWM

Software PWM

  • each I/O pin can be used for SW PWM

BUT:

  • software controlled timing has to be implemented
  • implementation and timing have to respect CPU usage
  • not a real clean signal

In other words: HW PWM is clearly prefered!

But unfortunately there is only one HW PWM pin (#18) available on the Rasperry Pis GPIO header (also its shared with the Audio system. That means that you can’t use PWM or Servo output and play audio through the 3.5mm jack at the same time).

If a RasPi project needs more than 1 PWM it has to use soft-pwm.
However thank god all the hard work is almost done by using Gordon Henderson’s wiringPi-lib and its Java-API Pi4J.

Pi4J provides a class called “SoftPwm”. As it uses wiringPi functions, the GPIO subsytem has to be initialzed first.
Therefore the programm has to executed as “sudo”!

Here is a simple example:

public class WiringPiSoftPWMExample {
    
    public static void main(String[] args) throws InterruptedException {
        
        // initialize wiringPi library
        Gpio.wiringPiSetup();

        // create soft-pwm pins (min=0 ; max=100)
        SoftPwm.softPwmCreate(0, 0, 100);

        IntegerProperty value1 = new SimpleIntegerProperty();
        value1.addListener((ObservableValue<? extends Number> observable, Number oldValue, Number newValue) -> {
            SoftPwm.softPwmWrite(0, newValue.intValue());
        });

        // continuous loop
        while (true) {
            // fade LED to fully ON
            for (int i = 0; i <= 100; i++) {
                value1.set(i);
                Thread.sleep(25);
            }

            // fade LED to fully OFF
            for (int i = 100; i >= 0; i--) {
                value1.set(i);
                Thread.sleep(25);
            }
        }
    }
}

LED blink

Works!
What’s next?

IMG_3176

3 x optical coupler + RGB LED stripe triggered by JavaFX UI contolled SoftPwms:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 thought on “Between 0 and 1 – PWM with Raspberry Pi”

  1. Greetings! Welcome to SourcingIC.com!!

    We are Hong Kong distributor of the following IC brand names for 15 years:

    TOSHIBA
    ALTERA
    ANALOG DEVICES
    TEXAS INSTRUMENTS
    ST
    FAIRCHILD
    ATMEL
    XILINX
    SILICON CHIP
    SAMSUNG
    SAYING
    NEC
    NXP
    ON SEMI
    PHILIPS
    FREESCALE
    INFINEON
    INTERSIL
    STRAIGHT INNOVATION
    MOTOROLA

    I understand you might require to discover ICs.

    The major objective for us is sourcing ICs. As you recognize more than 99% ICs available in this earth could be discovered in China, however it is time-costly to situate or send queries to hundreds of distributors or firms one at a time to find that obtained supplies as well as get great quote!

    Step 1: Skype or email our teammate: sales@sourcingic.com
    Step 2: Tell us part number and the amount or other requirements
    Action 3: I will source from providers as well as our teammate will select the reliable distributor to price estimate

    Step 4: Our seasoned IC technicans will examine the ICs to confirm they are genius and working well

    Step 5: Well loaded as well as delivered

    Payment Technique:
    Paypal/ Western Uniuon/ TT

    See you!

    Best Regards,
    Sourcing IC

    To provide every uncommon ICs

    Business History:

    Souring IC Ltd is a Hong Kong based firm with China offices in various provinces. We provide a wide variety of digital components, specialized in IC including Sound IC, Power Monitoring IC, Consumer Electronic IC, etc. Our goal is to supply any kind of components which are hard to be sourced in the electronic market, particularly the one discountinued by manufacturing facilities. With the aid of newest A.I system and also the elite teammates we had, we have the ability to resource every ICs that is not commonly utilized by the co-operation with thousands factories as well as agencies in China. We have the ability to supply the most affordable cost without MOQ.