Olex7iMatix
Olex7iMatix

Reputation: 11

raspberry pi java servo controll

I am java developer and pi4j begginer. I am developing java app that can control servo (raspberry pi 4 model b)

the error:

Exception in thread "main" java.lang.NoClassDefFoundError: com/pi4j/wiringpi/Gpio
        at me.Olex7iMatix.CatBotSoftware.Main.main(Main.java:9)
Caused by: java.lang.ClassNotFoundException: com.pi4j.wiringpi.Gpio
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
        ... 1 more

JRE system library: JavaSE-1.8

the code

package me.Olex7iMatix.CatBotSoftware;

import com.pi4j.wiringpi.Gpio;

public class Main {

    public static void main(String[] args) {
        
        Gpio.pwmWrite(2, 50);
        
    }
    
}

Upvotes: 0

Views: 252

Answers (1)

Ollie Pugh
Ollie Pugh

Reputation: 446

Edit: I just read the erorr message properly, you may need to just install the package, Installation guide

You are missing quite a lot of lines to get a servo to work with an RPi/Java.

You firstly need to tell the Pi which GPIO pin you are using and the output mode of said pin

com.pi4j.wiringpi.Gpio.pinMode({ENTER YOUR GPIO PIN HERE}, com.pi4j.wiringpi.Gpio.PWM_OUTPUT);

You also need to set the PWM Mode

com.pi4j.wiringpi.Gpio.pwmSetMode(com.pi4j.wiringpi.Gpio.PWM_MODE_MS);
com.pi4j.wiringpi.Gpio.pwmSetClock(192);

And then set the PWM Range

com.pi4j.wiringpi.Gpio.pwmSetRange(2000);  // this may differ for your servo

Then you can set a loop do set the servo

while (true) {
    com.pi4j.wiringpi.Gpio.pwmWrite({YOUR GPIO PIN}, 50);
    Thread.sleep(10)  // make this thread wait for 10 milliseconds
}

Upvotes: 0

Related Questions