Why not login to Qiita and try out its useful features?

We'll deliver articles that match you.

You can read useful information later.

0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RaspBerryPi3でLEDをPWMで光らせる (Pi4j)

Posted at

1、LEDの明るさを調整する!

 LEDをPWMで光らせることで明るさが調整できるらしいです。

 注意
 ・私は電子機器については電圧=抵抗×電流がわかる程度の全くの素人です。

2、本題

 (1)接続

 220Ωの抵抗器とLEDを接続しています。
 ①全体画像
image.png

 ②詳細
 image.png

 (2)Raspberry piの設定

 シリアルを有効にすることで、ピンを制御できるようになります。
image.png

 (3)実行プログラム

TestPwmLed.java
import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinPwmOutput;
import com.pi4j.io.gpio.Pin;
import com.pi4j.io.gpio.RaspiPin;
import com.pi4j.util.CommandArgumentParser;




public class TestPwmLed {

	public static void main(String[] args) throws InterruptedException {
		pwm(args);
	}


	/**
	 * 100Hzで出力
	 * @param args
	 * @throws InterruptedException
	 */
	public static void pwm(String[] args) throws InterruptedException{

		final GpioController gpio = GpioFactory.getInstance();
		
		Pin pin = CommandArgumentParser.getPin(
		RaspiPin.class,    
		RaspiPin.GPIO_00,  
		args);             
		GpioPinPwmOutput pwm = gpio.provisionSoftPwmOutputPin(pin);
		pwm.setPwmRange(100);//出力時間を100分割

		    
		int sleep = 1000;
		for(int i = 0 ;i<10;i++){
			//常にON
			pwm.setPwm(100);//デューティー比100
			System.out.println("PWM rate is: " + pwm.getPwm());
			Thread.sleep(sleep);
			//半分ON
			pwm.setPwm(50);//デューティー比50
			System.out.println("PWM rate is: " + pwm.getPwm());
			Thread.sleep(sleep);
			//一瞬ON
			pwm.setPwm(10);//デューティー比10
			System.out.println("PWM rate is: " + pwm.getPwm());
			Thread.sleep(sleep);
			//常にOFF
			pwm.setPwm(0);//デューティー比0
			System.out.println("PWM rate is: " + pwm.getPwm());
			Thread.sleep(sleep);
		}
		gpio.shutdown();		
		
		System.out.println("pwm end");

        
	}
}

 (4)実行結果

 見事に4段階の明るさをPWMで作り出すことに成功しました。
1505438587QQR95GLDchGaVx81505438578.gif

3、おわりに

 本当はハードのPWMを使用したかったのですが、どうしてもうまくいきませんでした。
 javaから制御ができていないように思います。権限の問題でしょうか・・・。
 疑似的ではありますが、PWMを操れたので、次はモータを制御したくなります。

参考
SoftPwmExample.java
 参考にしてプログラム作成しました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Qiita Conference 2025 will be held!: 4/23(wed) - 4/25(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

ymrl、Masanobu Naruse, Takeshi Kano, Junichi Ito, uhyo, Hiroshi Tokumaru, MinoDriven, Minorun, Hiroyuki Sakuraba, tenntenn, drken, konifar

View event details
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?