Link Search Menu Expand Document

Input Devices

Digital Input Devices



Code taken from ButtonTest:

try (Button button = new Button(inputPin, GpioPullUpDown.PULL_UP)) {
	button.addListener(event -> Logger.debug("valueChanged({})", event));
	Logger.debug("Waiting for 10s - *** Press the button connected to input pin " + inputPin + " ***");

Controlling an LED with a button:

Button controlled LED

Code taken from ButtonControlledLed:

try (Button button = new Button(buttonPin, GpioPullUpDown.PULL_UP); LED led = new LED(ledPin)) {
	button.whenPressed(nanoTime -> led.on());
	button.whenReleased(nanoTime ->;"Waiting for 10s - *** Press the button connected to pin {} ***", Integer.valueOf(buttonPin));
class com.diozero.devices.Button (gpio, pud=NONE) source

Extends [DigitalInputDevice](] to provide push button related utility methods.

  • gpio (int) - GPIO for the button.

  • pud (GpioPullUpDown) - Pull up / down configuration (NONE, PULL_UP, PULL_DOWN).

boolean isPressed ()

: Return true if the button is currently pressed.

boolean isReleased ()

: Return true if the button is currently released.

whenPressed (action)

: Action to perform when the button is pressed.

  • action (Action) - Action function to invoke.

whenReleased (action)

: Action to perform when the button is released.

  • action (Action) - Action function to invoke.

Motion Sensor

Warning “Work in progress”

Still under construction hence in the sandpit package.

class com.diozero.devices.sandpit.MotionSensor (gpio, threshold=10, eventAge=50, eventDetectPeriod=50) source

Extends [SmoothedInputDevice](] and represents a passive infra-red (PIR) motion sensor like the sort found in the CamJam #2 EduKit.

  • gpio (int) - The GPIO pin which the motion sensor is attached.

  • threshold (int) - The value above which the device will be considered “on”.

  • eventAge (int) - The time in milliseconds to keep active events in the queue.

  • eventDetectPeriod (int) - How frequently to check for events.

Analog Input Devices


class com.diozero.devices.TMP36 (gpio, vRef, tempOffset) source

Extends [AnalogInputDevice](] for reading temperature values from a TMP36 Temperature Sensor by Analog Devices.

  • gpio (int) - GPIO on the ADC device.

  • vRef* (float) - Voltage range for the ADC - essential for scaled readings.

  • tempOffset* (float) - Compensate for potential temperature reading variations between different TMP36 devices.

float getTemperature ()

: Get the current temperature in °C.


Generic potentiometer.

TODO Wiring diagram.

class com.diozero.devices.Potentiometer (gpio, vRef) source

Extends [AnalogInputDevice](] for taking readings from a potentiometer.

  • gpio (int) - Pin to which the potentiometer is connected.

  • vRef (float) - Reference voltage.

float getVoltage ()

: Read the potentiometer output voltage value.


class com.diozero.devices.LDR (gpio, vRef, r1) source

Extends AnalogInputDevce. Generic Photoresistor / Light-Dependent-Resistor (LDR).

  • gpio (int) - Pin to which the LDR is connected.

  • vRef (float) - Reference voltage.

  • r1 (float) - Resistor between the LDR and ground.

float getLdrResistance ()

: Read the resistance across the LDR.

float getLuminosity ()

: Read the current luminosity.

Warning “Not yet implemented”

This operation currently just returns the resistance across the LDR.

Sharp GP2Y0A21YK Distance Sensor

Sharp GP2Y0A21YK Distance Sensor.

class com.diozero.devices.GP2Y0A21YK (gpio) source

Extends [AnalogInputDevice](] for taking object proximity readings.

float getDistanceCm ()

: Read distance in centimetres, range 10 to 80cm.