![JAR search and dependency download from the Maven repository](/logo.png)
.pi4j.pi4j-example.1.2.M1.source-code.PwmExample Maven / Gradle / Ivy
/*
* #%L
* **********************************************************************
ORGANIZATION : Pi4J
PROJECT : Pi4J :: Java Examples
FILENAME : PwmExample.java
This file is part of the Pi4J project. More information about
this project can be found here: http://www.pi4j.com/
**********************************************************************
* %%
* Copyright (C) 2012 - 2018 Pi4J
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* .
* #L%
*/
import com.pi4j.io.gpio.*;
import com.pi4j.util.CommandArgumentParser;
import com.pi4j.util.Console;
/**
*
* This example code demonstrates how to setup a hardware supported PWM pin GpioProvider
*
*
* @author Robert Savage
*/
public class PwmExample {
/**
* [ARGUMENT/OPTION "--pin (#)" | "-p (#)" ]
* This example program accepts an optional argument for specifying the GPIO pin (by number)
* to use with this GPIO listener example. If no argument is provided, then GPIO #1 will be used.
* -- EXAMPLE: "--pin 4" or "-p 0".
*
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
// create Pi4J console wrapper/helper
// (This is a utility class to abstract some of the boilerplate code)
final Console console = new Console();
// print program title/header
console.title("<-- The Pi4J Project -->", "PWM Example");
// allow for user to exit program using CTRL-C
console.promptForExit();
// create GPIO controller instance
GpioController gpio = GpioFactory.getInstance();
// All Raspberry Pi models support a hardware PWM pin on GPIO_01.
// Raspberry Pi models A+, B+, 2B, 3B also support hardware PWM pins: GPIO_23, GPIO_24, GPIO_26
//
// by default we will use gpio pin #01; however, if an argument
// has been provided, then lookup the pin by address
Pin pin = CommandArgumentParser.getPin(
RaspiPin.class, // pin provider class to obtain pin instance from
RaspiPin.GPIO_01, // default pin if no pin argument found
args); // argument array to search in
GpioPinPwmOutput pwm = gpio.provisionPwmOutputPin(pin);
// you can optionally use these wiringPi methods to further customize the PWM generator
// see: http://wiringpi.com/reference/raspberry-pi-specifics/
com.pi4j.wiringpi.Gpio.pwmSetMode(com.pi4j.wiringpi.Gpio.PWM_MODE_MS);
com.pi4j.wiringpi.Gpio.pwmSetRange(1000);
com.pi4j.wiringpi.Gpio.pwmSetClock(500);
// set the PWM rate to 500
pwm.setPwm(500);
console.println("PWM rate is: " + pwm.getPwm());
console.println("Press ENTER to set the PWM to a rate of 250");
System.console().readLine();
// set the PWM rate to 250
pwm.setPwm(250);
console.println("PWM rate is: " + pwm.getPwm());
console.println("Press ENTER to set the PWM to a rate to 0 (stop PWM)");
System.console().readLine();
// set the PWM rate to 0
pwm.setPwm(0);
console.println("PWM rate is: " + pwm.getPwm());
// stop all GPIO activity/threads by shutting down the GPIO controller
// (this method will forcefully shutdown all GPIO monitoring threads and scheduled tasks)
gpio.shutdown();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy