This sensor uses infrared light to detect obstacles. If the emitted infrared light hits an obstacle, it is reflected and detected by the photodiode. The distance to be reached for detection can be adjusted with the two knobs.

This behavior can be used in controllers, such as those used in robots, to be able to stop autonomously if they were going to hit an obstacle.

Condition 1 : There is no obstacle in front of the detector [LED on the module: Off] [Sensor Signal= Digital A]

Condition 2 : Detector has detected obstacle [LED on the module: One] [Sensor Signal= Digital Off]

With the additional pin “Enable”, this sensor has the option to activate or deactivate the obstacle detection by means of a controller. This sensor is activated by default, so the detection is performed permanently – if you don’t want to, e.g. because the desired programming doesn’t provide for it, you have to remove the plug-in (see green in the picture) with the label “EN” and activate a control signal on the “Enable-Pin.”

  • Please note that the right controller is responsible for the measurable distance of the sensor. If it is rotated to the right, the measurable distance increases and if it is rotated to the left, the measurable distance decreases.

  • Please note that the left controller is responsible for the sensitivity of the sensor. When the sensor is rotated to the right, the sensitivity increases and when the sensor is rotated to the left, the sensitivity of the sensor decreases.

Pin assignment

Code example Arduino

Connection assignment Arduino

Arduino Sensor
- Enable
5V +V
Mass GND
Pin 10 Signal

The program reads the current state of the sensor pins and displays in the serial console whether the obstacle detector is currently in front of an obstacle or not.

int Led  =  13;// Declaration of the LED output spin
int Sensor  =  10;  // Declaration of the sensor input spin
int  val;  //  Temporary  Variable
   
void setup  ( )
{
  pinMode (Led, OUTPUT);  // Initialization output spin
  pinMode (Sensor, INPUT);  //  Initialization sensor pin
}
   
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);
  }
}

Sample program download

KY032-Arduino.zip

Code example Raspberry Pi

Pin assignment Raspberry Pi

Raspberry Pi Sensor
- Enable
3,3V +V
Mass GND
GPIO 24 [Pin 18] Signal

The program reads the current status of the sensor pin and outputs in the serial console whether the obstacle detector is currently in front of an obstacle or not.

#Modules required are imported and set up
import RPi. GPIO  as  GPIO
import time
 
GPIO.setmode (GPIO.BCM) 
 
# Here the input pin to which the sensor is connected is declared.
GPIO_PIN = 24
GPIO.setup (GPIO_PIN, GPIO.IN , pull_up_down = GPIO.PUD_UP) 
 
# Pause between output is defined (in seconds) 
delayTime  =  0.5
 
print (“Sensor test [push CTRL+C to stop the test]”) 
 
# Main program loop
 try:
        while True:
            if GPIO.input  (GPIO_PIN)   = =  True:
                print (“No obstacle”) 
            else:
                print (“obstacle detected”) 
            print (“ ---------------------------------------")
 
            # Reset + Delay
            time.sleep (delayTime) 
 
# Tidying up after the program has been terminated
except KeyboardInterrupt:
        GPIO.cleanup ()

Sample program Download

[KY032-RPi.zip](/files/ files/ sensors/KY-032 /KY032-RPi.zip)

To start with the command:

sudo python3  KY032.py

Code example Micro:Bit

Connection assignment Micro : Bit:

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

This is a MakeCode example for Micro: Bit which essence does the same as the examples of the other two variants. However, this example is closer to the example of the Raspberry Pi than to the example of the Arduino.

Sample program download

microbit-KY-032.zip