Explorer hat Pro Coding for non analogue input

Hi everyone,

Newbie. Quite simply how do I code for input sensors.

For example I am trying to get a IR obstacle sensor to work in input 1 but don’t know how to write it into code for the explorer hat. There doesn’t appear to be any instructions (can only find info for analogue sensors).

I’m a newbie so a little lost. All I want to do is for a program to print ‘object detected’ when it is within say 10cm or print whenever a change occurs (perhaps the distance value?). Any advice would be great.

Thanks,

Dee

A link to info on the IR sensor would help.

Hi,

It’s the IR Sensor for Obstacle Avoidance KY-032.

http://irsensor.wizecode.com/

Thanks :)

The function reference is here,

Input
Explorer HAT/pHAT includes 4 buffered, 5v tolerant inputs. These act just like the GPIO pins on your Pi and don't require any special treatment. When you send a HIGH signal into them, you'll read a HIGH pin state ( 1 ) in Python.
read() - Read the state of the input
has_changed() - Returns true if the input has changed since the last read
on_changed( handler_function[, bounce_time ] ) - Calls "handler_function" when the input changes, debounce time ( in ms ) is optional
on_low( handler_function[, bounce_time ] ) - Calls "handler_function" when the input goes low ( off )
on_high( handler_function[, bounce_time ] ) - Calls "handler_function" when the input goes on ( high )
clear_events() - Remove all handlers
Unlike analog events, you'll get an instance of the input passed to your handler function, so you can do something like this:
def changed(input):
  state = input.read()
  name  = input.name
  print("Input {} changed to {}".format(name,state))
  
explorerhat.input.one.changed(changed)
Then, when you change the input you'll see something like:
Input one changed to 1
Input one changed to 0

And from the link you posted

microDelay( 210);                   // Wait 210µs (8 pulses of 38kHz).
if( digitalRead( outputPin))        // If detector Output is HIGH,
{
    objectDetect = false;           // then no object was detected;
}
else                                // but if the Output is LOW,
{
    microDelay( 395);               // wait for another 15 pulses.
    if( digitalRead( outputPin))    // If the Output is now HIGH,
    {                               // then first Read was noise
        objectDetect = false;       // and no object was detected;
    }
    else                            // but if the Output is still LOW,
    {
        objectDetect = true;        // then an object was truly detected.
    }
}
digitalWrite( enablePin, LOW);      // Disable the internal 38kHz signal.

You’ll want to wire the VCC to 3.3V, and GND to Ground.
Then wire the EN (Enable) to an Output pin on the Explorer Hat, and the OUT pin to an Input on the Hat. Then do what is listed in that sample C code. The comment part gives you the time to turn on off etc. The Output function reference is just below the Input function reference.

Just what I was looking for. Really useful thanks :)

If it doesn’t work try using 5V instead of 3.3v for VCC. The Explorer Hat inputs and outputs are 5V safe. Its just habit for me to use 3.3v as thats all the Pi itself can handle. The Explorer Hat buffers the signals and only sends 3.3V logic to the Pi so 5V with that sensor shouldn’t be an issue.The product page says the IR sensor can work with both.