Skip to content

Java code using Pi4J to interface with a KY-004 push button module on Raspberry Pi. Detects button presses and prints to console.

Notifications You must be signed in to change notification settings

x85mkx/KY-004ButtonModule

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

Pi4J KY-004 Button Module Test

This Java application demonstrates how to interface with a KY-004 push button module using the Pi4J library on a Raspberry Pi.

Purpose:

The App.java class provides a basic example of reading input from a KY-004 push button module connected to a Raspberry Pi using Pi4J v2. It showcases how to initialize Pi4J, configure a digital input pin to monitor the button's state, and detect button press events. This serves as a simple starting point for anyone wanting to incorporate button input into their Raspberry Pi projects using Pi4J.

Functionality:

  • Initialization: Sets up the Pi4J context (Pi4J.newAutoContext()), which is the prerequisite for using Pi4J to interact with Raspberry Pi hardware.
  • GPIO Configuration: Configures a single digital input pin (GPIO 24 in BCM numbering) to read the signal from the KY-004 button module. It uses the Pi4J DigitalInput.Builder for a clear and structured configuration, and enables an internal pull-up resistor.
  • Button Press Detection: Sets up an event listener (button.addListener) to detect changes in the digital input state.
  • Console Output on Press: When the button is pressed (the digital input state transitions to LOW due to the pull-up resistor), the program prints "Signal recognized" to the console.
  • Continuous Monitoring: The program runs in an infinite loop (while(true)) to continuously listen for button presses.
  • Background Event Handling: Pi4J's event listener mechanism ensures button presses are detected asynchronously without requiring constant polling in the main loop.

Hardware Requirements:

  • Raspberry Pi: Any Raspberry Pi model supported by Pi4J.
  • KY-004 Button Module: A common and inexpensive push button module.
  • Wiring: Jumper wires to connect the KY-004 module to the Raspberry Pi GPIO pins as follows:
    • Signal (S): GPIO 24 (Pin 18)
    • +V (+): 3.3V (Pin 1)
    • GND (-): Ground (Pin 6 or any other GND pin)

Software Requirements:

  • Java Development Kit (JDK): Required to compile and run the Java code.
  • Pi4J Library: This project is built using Pi4J v2. You'll need to include Pi4J as a dependency in your project (e.g., using Maven or Gradle).
  • Operating System: Raspberry Pi OS (or any OS supported by Pi4J).

How to Use:

  1. Clone this repository.
  2. Ensure you have Pi4J v2 set up in your project. If you are using Maven, include the Pi4J core dependency in your pom.xml.
  3. Compile the App.java class.
  4. Connect the KY-004 Button module to your Raspberry Pi as described above.
  5. Run the compiled App class on your Raspberry Pi.

Important Notes:

  • GPIO Pin Numbering: The code uses BCM (Broadcom) GPIO pin numbering. Ensure your wiring matches this numbering scheme.
  • Pull-up Resistor: The code configures an internal pull-up resistor for the digital input. This is crucial for the KY-004 button module to function correctly. When the button is not pressed, the pin is pulled HIGH. Pressing the button connects the pin to ground, making it LOW.
  • Event-Driven Approach: This example utilizes Pi4J's event listener mechanism, which is more efficient than constantly polling the button state. The program reacts only when the button's state changes.
  • Permissions: Ensure that the user running the Java application has the necessary permissions to access the Raspberry Pi's GPIO pins.

Keywords: Raspberry Pi, Pi4J, KY-004, Button, Push Button, GPIO, Java, IoT, Hardware, Example, Tutorial, Digital Input, Event Listener, Pull-up Resistor.

About

Java code using Pi4J to interface with a KY-004 push button module on Raspberry Pi. Detects button presses and prints to console.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages