0

I trying out different java programs for my RPi B+ V1.2 and as far I have only tried to blink with a lamp for a few seconds. It works.

Now I want to try to blink with a lamp by using a button. In this case, I need to use a listener for my button.

I have been using this java code from this page:

import com.pi4j.io.gpio.*;
import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
import com.pi4j.io.gpio.event.GpioPinListenerDigital;

/**
 * This example code demonstrates how to setup a listener
 * for GPIO pin state changes on the Raspberry Pi.
 *
 * @author Robert Savage
 */
public class ListenGpioExample {

    public static void main(String args[]) throws InterruptedException {
        System.out.println("<--Pi4J--> GPIO Listen Example ... started.");

        // create gpio controller
        final GpioController gpio = GpioFactory.getInstance();

        // provision gpio pin #02 as an input pin with its internal pull down resistor enabled
        final GpioPinDigitalInput myButton = gpio.provisionDigitalInputPin(RaspiPin.GPIO_02, PinPullResistance.PULL_DOWN);

        // set shutdown state for this input pin
        myButton.setShutdownOptions(true);

        // create and register gpio pin listener
        myButton.addListener(new GpioPinListenerDigital() {
            @Override
            public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {
                // display pin state on console
                System.out.println(" --> GPIO PIN STATE CHANGE: " + event.getPin() + " = " + event.getState());
            }

        });

        System.out.println(" ... complete the GPIO #02 circuit and see the listener feedback here in the console.");

        // keep program running until user aborts (CTRL-C)
        while(true) {
            Thread.sleep(500);
        }

        // 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();   <--- implement this method call if you wish to terminate the Pi4J GPIO controller
    }
}

This listener won't even print out a single message from the method handleGpioPinDigitalStateChangeEvent

I have WiringPi installed:

enter image description here

I'm trying connect GPIO_02 to 3.3v but nothing happes:

enter image description here

Have I forgot something? It was a long time ago I worked with microcontrollers. Mabey I have missed something?

One weird thing is that if I write this command:

enter image description here

Then GPIO18 will go HIGH/LOW and ligth a LED lamp.

Then I tried this pinmap

enter image description here

And now it wokring.

Question:

Why does my Raspberry Pi B+ has the Raspberry Pi 2 B pinmap?

  • All Pi with the 40 pin header have IDENTICAL header - these are the same – Milliways Oct 15 '18 at 20:33
  • @Milliways But why does it differ for me? – Daniel Mårtensson Oct 15 '18 at 20:45
1

Looks to me you're mixing up pin numbering schemes.

The B+ image above shows BCM (Broadcom) scheme.

The second one is the wiringPi (and implicitly Pi4J) scheme (notice the text at the bottom of the image). The gpio utility, which is part of wiringPi, also uses this scheme by default.

Use https://pinout.xyz/ for reference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.