LM393 Photodiode not working?

I'm using a LM393 Photo diode if i am not wrong.


I want the photo diode to test the surroundings for light and make an ultrasonic sensor test for the distance between an object and the Arduino if there is no light, turning on a led strip should the distance between the object and the Arduino be smaller than the set distance which can be adjusted by a rotary encoder.

Below is my code for before the LM393 photo diode sensor was added in:

#include <Adafruit_NeoPixel.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);  // set the LCD address to 0x27 for a 16 chars and 2 line display


#define echoPin 12 // attach pin D12 Arduino to pin Echo of HC-SR04
#define trigPin 13 //attach pin D13 Arduino to pin Trig of HC-SR04
#define PIN 9
// Rotary Encoder Inputs
#define CLK 2
#define DT 3
#define SW 4

int counter = 0;
int currentStateCLK;
int lastStateCLK;
String currentDir ="";
unsigned long lastButtonPress = 0;

// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);

// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel.  Avoid connecting
// on a live circuit...if you must, connect GND first.

// defines variables
long duration; // variable for the duration of sound wave travel
int distance_cm; // variable for centimeters measurement

void setup() { 
  // Set encoder pins as inputs
  pinMode(CLK,INPUT);
  pinMode(DT,INPUT);
  pinMode(SW, INPUT_PULLUP);

  // Read the initial state of CLK
  lastStateCLK = digitalRead(CLK);
  
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
  pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
  Serial.begin(9600); // // Serial Communication is starting with 9600 of baudrate speed
  Serial.println("Ultrasonic Sensor HC-SR04 Test"); // print some text in Serial Monitor
  Serial.println("with Arduino UNO R3");
  lcd.init();                      // initialize the lcd 
  lcd.init();
  
}

void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}

void loop() {
  
  // Read the current state of CLK
  currentStateCLK = digitalRead(CLK);

  // If last and current state of CLK are different, then pulse occurred
  // React to only 1 state change to avoid double count
  if (currentStateCLK != lastStateCLK  && currentStateCLK == 1){

    // If the DT state is different than the CLK state then
    // the encoder is rotating CCW so decrement
    if (digitalRead(DT) != currentStateCLK) {
      counter --;
      currentDir ="CCW";
    } else {
      // Encoder is rotating CW so increment
      counter ++;
      currentDir ="CW";
    }
    lcd.backlight();
    lcd.setCursor(0, 0);
    lcd.print("Set: ");
    lcd.println(counter);
    lcd.print(" cm");
  }

  // Remember last CLK state
  lastStateCLK = currentStateCLK;

  // Read the button state
  int btnState = digitalRead(SW);

  //If we detect LOW signal, button is pressed
  if (btnState == LOW) {
    //if 50ms have passed since last LOW pulse, it means that the
    //button has been pressed, released and pressed again
    if (millis() - lastButtonPress > 50) {
      Serial.println("Button pressed!");
    }

    // Remember last button press event
    lastButtonPress = millis();
  }

  // Put in a slight delay to help debounce the reading
  delay(1);
  
  // Clears the trigPin condition
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin HIGH (ACTIVE) for 10 microseconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  // Calculating the distance
  distance_cm = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
  // Displays the distance on the Serial Monitor
  lcd.backlight();
  lcd.setCursor(0, 1);
  lcd.print("Distance: ");
  lcd.print(distance_cm);
  lcd.println(" cm");

  static unsigned long LastTimeDistanceWasLE10 = 0;

  if (distance_cm <= counter)
  {
    LastTimeDistanceWasLE10 = millis();
  }

  if (millis() - LastTimeDistanceWasLE10 >= 3000)
  {
    colorWipe(strip.Color( 0, 0, 0), 0);
  }

  else
  {
    colorWipe(strip.Color( 255, 125, 125), 0);
  }
}

and below here is the new code that tries to incorporate the LM393 photo diode sensor:

#include <Adafruit_NeoPixel.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);  // set the LCD address to 0x27 for a 16 chars and 2 line display


#define echoPin 12 // attach pin D12 Arduino to pin Echo of HC-SR04
#define trigPin 13 //attach pin D13 Arduino to pin Trig of HC-SR04
#define PIN 9
// Rotary Encoder Inputs
#define CLK 2
#define DT 3
#define SW 4

int counter = 0;
int currentStateCLK;
int lastStateCLK;
String currentDir ="";
unsigned long lastButtonPress = 0;

// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);

// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel.  Avoid connecting
// on a live circuit...if you must, connect GND first.

// defines variables
long duration; // variable for the duration of sound wave travel
int distance_cm; // variable for centimeters measurement

void setup() { 
  // Set encoder pins as inputs
  pinMode(CLK,INPUT);
  pinMode(DT,INPUT);
  pinMode(SW, INPUT_PULLUP);

  // Read the initial state of CLK
  lastStateCLK = digitalRead(CLK);
  
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
  pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
  Serial.begin(9600); // // Serial Communication is starting with 9600 of baudrate speed
  Serial.println("Ultrasonic Sensor HC-SR04 Test"); // print some text in Serial Monitor
  Serial.println("with Arduino UNO R3");
  lcd.init();                      // initialize the lcd 
  lcd.init();
  pinMode(8,INPUT);
  
}

void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}

void loop() {
  
  // Read the current state of CLK
  currentStateCLK = digitalRead(CLK);

  // If last and current state of CLK are different, then pulse occurred
  // React to only 1 state change to avoid double count
  if (currentStateCLK != lastStateCLK  && currentStateCLK == 1){

    // If the DT state is different than the CLK state then
    // the encoder is rotating CCW so decrement
    if (digitalRead(DT) != currentStateCLK) {
      counter --;
      currentDir ="CCW";
    } else {
      // Encoder is rotating CW so increment
      counter ++;
      currentDir ="CW";
    }
    lcd.backlight();
    lcd.setCursor(0, 0);
    lcd.print("Set: ");
    lcd.println(counter);
    lcd.print(" cm");
  }

  // Remember last CLK state
  lastStateCLK = currentStateCLK;

  // Read the button state
  int btnState = digitalRead(SW);

  //If we detect LOW signal, button is pressed
  if (btnState == LOW) {
    //if 50ms have passed since last LOW pulse, it means that the
    //button has been pressed, released and pressed again
    if (millis() - lastButtonPress > 50) {
      Serial.println("Button pressed!");
    }

    // Remember last button press event
    lastButtonPress = millis();
  }

  // Put in a slight delay to help debounce the reading
  delay(1);
  
  // Clears the trigPin condition
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin HIGH (ACTIVE) for 10 microseconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  // Calculating the distance
  distance_cm = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
  // Displays the distance on the Serial Monitor
  lcd.backlight();
  lcd.setCursor(0, 1);
  lcd.print("Distance: ");
  lcd.print(distance_cm);
  lcd.println(" cm");

  int temp=digitalRead(8);      //assign value of LDR sensor to a temporary variable
  delay(300);
  
  static unsigned long LastTimeDistanceWasLE10 = 0;
  
  if (temp==HIGH)
  {
    if (distance_cm <= counter)
    {
      LastTimeDistanceWasLE10 = millis();
    }

    if (millis() - LastTimeDistanceWasLE10 >= 3000)
    {
      colorWipe(strip.Color( 0, 0, 0), 0);
    }

    else
    {
      colorWipe(strip.Color( 255, 125, 125), 0);
    }
  }
}

I have no idea why but after I changed the code to incorporate the LM393 photo diode, the code no longer works and the led strip doesn't turn on even after the distance between the object and the Arduino is far less than the set distance. Is my LM393 sensor just faulty, am I using the correct sensor or is my code wrong?

Please post a link to the sensor product page, and a complete wiring diagram. A photo of a hand drawn diagram is preferred.

If the manufacturer provides example code, use that to test the sensor.

For help with with code, describe what you expect to happen, and what happens instead.

Don’t write code all together. Do you have code for just the photodiode? You should get it working in its own, aptly named, sketch before combining

An LM393 is the comparator chip which "happens" to be connected to a photodiode. We need a link to the specs/datasheet of the board that you bought.

Hopefully you bought it from a reliable supplier so that documentation is available.

You should be able to test it with the code from the Digital Read Serial Example or the Analog Read Serial Example, but of course you'll connect the light sensor instead of a switch or pot.

NOTE - A comparator puts-out a digital signal (high or low) which would indicate light or dark. But I see an "AO" connection which is usually an analog output.

LM393 is the voltage comparator on the little board.

Is the photodiode for visible or IR light? If it an IR photodiode, ambient room light will have little effect on it.

Hi,
This is possibly the schematic of your sensor.

There is a module sensitivity adjustment trimpot . (this blue cube with a screw).

The Photodiode Sensor module is made to detect light, brightness and especially brightness. It consists of a digital output that can be connected directly to a microcontroller such as Arduino. If the ambient light intensity or brightness is below the set value, the sensor output is in the high state, and when the ambient light intensity exceeds the range, the digital output is in the low state. This value can be adjusted through the trimpot located on the sensor that will regulate the digital output D0 through the adjusted brightness intensity. It is possible to use the analog output to measure the brightness or brightness intensity level.

Does the led indicator on the sensor PCB light up and go out based on the light level as you would expect?

Let's hope not. If that schematic accurately represents the PCB, the whole thing would be useless!

Its working now, thank you, i realised that i haven't adjusted the range with the trimpot, tysm!

Hi,
if your problem was solved, do a kindness to everyone on the forum, especially to those who helped you.

Write [Solved] before your topic title, so if someone searches and finds your topic, they'll know what the solution looks like.

And if it was solved by some help, please tick the one that best describes your solution.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.