• Arduino
  • Raspberry Pi
  • Raspberry Pi Pico
  • Micro:Bit

The KY-004 is equipped with a push button that shorts two signal outputs together when the button is pressed.

Operating voltage 3,3 V - 5 V
Environment temperature -25 °C - 105°C
Lifetime 100.000 cycles
Dimensions 18 x 15 x 6 mm

Pin assignment

Code example Arduino

Pin assignment Arduino

Arduino Sensor
Pin 10 Signal
5 V +V
GND GND
Arduino Sensor
Pin 13 LED+
GND LED-

This is a sample program that lights up an LED when a signal is detected at the sensor. The modules KY-011, KY-016 or KY-029 can, for example, be used as LEDs, for example.

int Led = 13 ;// declaration of the LED output pin
int Sensor = 10 ;// Declaration of the sensor input pin
int val; // Temporary variable
  
void setup ()
{
  pinMode (Led, OUTPUT) ; // Initialize output pin
  pinMode (Sensor, INPUT) ; // Initialize sensor pin
  digitalWrite(Sensor, HIGH) ; // Activate internal pull-up resistor
}
  
void loop ()
{
  val = digitalRead (Sensor) ; // The current signal at the sensor is read out
  
  if (val == HIGH) // If a signal could be detected, the LED is switched on.
  {
    digitalWrite (Led, LOW);
  }
  else
  {
    digitalWrite (Led, HIGH);
  }
}

Example program download

KY004-Arduino.zip

The KY-004 is equipped with a push button that shorts two signal outputs together when the button is pressed.

Operating voltage 3,3 V - 5 V
Environment temperature -25 °C - 105°C
Lifetime 100.000 cycles
Dimensions 18 x 15 x 6 mm

Pin assignment

Code example Raspberry Pi

Pin assignment Raspberry Pi

Raspberry Pi Sensor
GPIO 24 [Pin 18] Signal
3.3 V [Pin 1] +V
GND [Pin 6] GND

This is a sample program that prints a console output when as signal is detected at the sensor.

# Import the needed modules and set them up
import RPi.GPIO as GPIO
import time
  
GPIO.setmode(GPIO.BCM)
  
# Here the input pin is declared, to which the sensor is connected. Additionally the PullUP resistor at the input will be activated
GPIO_PIN = 24
GPIO.setup(GPIO_PIN, GPIO.IN, pull_up_down = GPIO.PUD_UP)
  
print ("Sensor test [press CTRL+C to exit test]")
  
# This outputFunction is executed on signal detection
def outputFunction(null):
        print("Signal detected")
  
# When a signal is detected (falling signal edge) the output function is executed
GPIO.add_event_detect(GPIO_PIN, GPIO.FALLING, callback=outputFunction, bouncetime=100) 
  
# main program loop
try:
    while True:
        time.sleep(1)
  
# clean up after the program is finished
except KeyboardInterrupt:
    GPIO.cleanup()

Sample program download

KY004-RPi.zip

To start with the command:

sudo python3 KY004-RPi.py

The KY-004 is equipped with a push button that shorts two signal outputs together when the button is pressed.

Operating voltage 3,3 V - 5 V
Environment temperature -25 °C - 105°C
Lifetime 100.000 cycles
Dimensions 18 x 15 x 6 mm

Pin assignment

Code example Micro:Bit

Pinout Micro:Bit:

Micro:Bit Sensor
Pin 1 Signal
3 V +V
GND GND

Sample program download

microbit-KY-004.zip

The KY-004 is equipped with a push button that shorts two signal outputs together when the button is pressed.

Operating voltage 3,3 V - 5 V
Environment temperature -25 °C - 105°C
Lifetime 100.000 cycles
Dimensions 18 x 15 x 6 mm

Pin assignment

Code example Raspberry Pi Pico

Pin assignment Raspberry Pi Pico

Raspberry Pi Pico Sensor
GPIO26 Signal
3.3V +V
GND GND

This is a sample program that counts up and outputs text serially when a signal is detected at the sensor.

# Load libraries
from machine import Pin, Timer

# Initialization of GPIO26 as input
sensor = Pin(26, Pin.IN, Pin.PULL_DOWN)

# Create timer
timer = Timer()

# Variables initialization
counter = 0

# Function: Count steps
def up(timer):
    global counter
    counter = counter + 1
    print(counter)

# Debounce function: Set timer
def btn(pin):
    timer.init(mode=Timer.ONE_SHOT, period=100, callback=up)

# Initialization of the interrupt
sensor.irq(trigger=Pin.IRQ_FALLING, handler=btn)

Example program download

KY004-Pico.zip