KY-004 Button-module
From SensorKit X40 Wiki
Revision as of 10:24, 19 May 2017 by Sensorkit wiki admin (Talk | contribs)
Contents
Picture
Technical data / Short description
By pressing the button, the signal circuit is switched.
Pinout
Code example Arduino
This example will light up a LED after the button is pressed.
The module KY-011, KY-016 or KY-029 can be used as a LED.
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) ; // Initialization output pin pinMode (Sensor, INPUT) ; // Initialization sensor pin digitalWrite(Sensor, HIGH); // Activating internal pull-up resistor } void loop () { val = digitalRead (Sensor) ; // The current signal at the sensor will be read if (val == HIGH) // If a signal was detected, the LED will light up. { digitalWrite (Led, LOW); } else { digitalWrite (Led, HIGH); } }
Connections Arduino:
LED + | = | [Pin 13] |
LED - | = | [Pin GND] |
Sensor Signal | = | [Pin 10] |
Sensor +V | = | [Pin 5V] |
Sensor - | = | [Pin GND] |
Example program download:
Code example Raspberry Pi
# needed modules will be imported import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) # The input pin of the Sensor will be declared. Additional to that the pull-up resistor 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 end it]" # This output function will be started at signal detection. def ausgabeFunktion(null): print("Signal detected") # At the moment of detecting a Signal ( falling signal edge ) the output function will be activated. GPIO.add_event_detect(GPIO_PIN, GPIO.FALLING, callback=ausgabeFunktion, bouncetime=100) # main program loop try: while True: time.sleep(1) # Scavenging work after the end of the program except KeyboardInterrupt: GPIO.cleanup()
Connections Raspberry Pi:
Signal | = | GPIO24 | [Pin 18] |
+V | = | 3,3V | [Pin 1] |
GND | = | GND | [Pin 6] |
Example program download
To start, enter the following command:
sudo python SensorTest_RPi.py