-example.1.4.source-code.PwmExample Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pi4j-example Show documentation
Show all versions of pi4j-example Show documentation
Pi4J Java Examples using the Pi4J Library
The newest version!
/*
* #%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: https://pi4j.com/
* **********************************************************************
* %%
* Copyright (C) 2012 - 2021 Pi4J
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #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 - 2024 Weber Informatics LLC | Privacy Policy