Input Devices

Digital Input Devices

Button

Button

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 + " ***");
    SleepUtil.sleepSeconds(10);
}

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(led::on);
    button.whenReleased(led::off);
    Logger.info("Waiting for 10s - *** Press the button connected to pin {} ***", Integer.valueOf(buttonPin));
    SleepUtil.sleepSeconds(10);
}
class com.diozero.Button (gpio, pud=NONE) source

Extends [DigitalInputDevice](API.md#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

Work in progress

Still under construction hence in the sandpit package.

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

Extends [SmoothedInputDevice](API.md#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

TMP36

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

Extends [AnalogInputDevice](API.md#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.

Potentiometer

Generic potentiometer.

TODO Wiring diagram.

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

Extends [AnalogInputDevice](API.md#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.

LDR

class com.diozero.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.

Not yet implemented

This operation currently just returns the resistance across the LDR.

Sharp GP2Y0A21YK Distance Sensor

Sharp GP2Y0A21YK Distance Sensor.

class com.diozero.GP2Y0A21YK (gpio) source

Extends [AnalogInputDevice](API.md#analoginputdevice] for taking object proximity readings.

float getDistanceCm ()

Read distance in centimetres, range 10 to 80cm.