I connected the KY -032 obstacle avoidance sensor to an arduino nano using the following pins:
GND --> GND
VCC --> 5V
OUT --> D3
this is the script:
const int sensorPin = 3; // Connect the OUT pin to D3
void setup() {
Serial.begin(9600); // Initialize serial communication for debugging (optional)
pinMode(sensorPin, INPUT); // Set the sensor pin as an input
}
void loop() {
int sensorValue = digitalRead(sensorPin); // Read the digital value from the sensor
if (sensorValue == HIGH) {
Serial.println("Obstacle detected!"); // Print a message when an obstacle is detected (optional)
// Your code to respond to obstacle detection goes here
} Serial.println(sensorValue);
// Delay to avoid rapid multiple readings (adjust as needed)
delay(500);
}
it doesn't detect anything. I tried using a remote controller to blast IR light to the sensor and it worked so I guess the problem is the IR light not the sensor.
You probably have a problem with the sensor.
I put your code in the simulator and used an LDR sensor,
(The simulator does not have the KY-032), and the code works correctly when I adjust the sensor by clicking on it.
I don't think the problem is with the sensor. I think it's the IR light because I tried using a separate IR light and the sensor detected it with no problem.
If it is the same as here there should be two potentiometers on the board.
Quote:
There are two small variable resistors on the board: R5 (closer to the GREEN JUMPER) and R6. R6 is used to tune the 555 oscillator to exactly 38kHz. R5 will limit current to and dim the IR LED as it is turned counter-clockwise. Both adjustments together effect the sensitivity and range of the device.
Did you have the green jumper installed for the test? That should light up the led even without a sketch handling the EN pin (if the above linked information is true).
If you have access to an oscilloscope you might check the signal at the led pins ...
/*
Forum: https://forum.arduino.cc/t/ky-032-obstacle-avoidance-sensor-not-working/1173727/7
Wokwi: https://wokwi.com/projects/377386960545098753
Simple test environment to count pulses using an interrupt
*/
constexpr byte inPin = 3;
unsigned long lastTime =0;
volatile unsigned long count = 0;
void ISRroutine(){
count++;
};
void setup() {
// put your setup code here, to run once:
Serial .begin(115200);
Serial.println("Start");
pinMode(inPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(inPin),ISRroutine,RISING);
lastTime = millis();
}
void loop() {
// put your main code here, to run repeatedly:
if (millis()-lastTime >= 1000){
noInterrupts();
unsigned long lastCount = count;
lastTime = millis();
count =0;
interrupts();
Serial.println(lastCount);
}
}
It uses an IR receiver and an IR Remote Control and prints the number of received pulses per 1000 msec. Each button press of the Remote Control results in 34 pulses in this case. If you press a button several times/second the number of pulses add up.
If you have a spare IR receiver ( e.g. like KY-022 INFRAROT-RECEIVER) you can power the KY-032 and use a setup like here to check incoming pulses. I
If not you may carefully (not to create any shortcut!) connect pin 3 to the anode of the IR diode, power the KY-032 and run the sketch. If the diode gets any pulses Serial should display values far from zero. I tested it with the following results: