Dark sensor circuit using LM393 question.... unstable OFF/ON... flickering

Thread Starter

johnaustinkaty

Joined Jul 16, 2021
284
No! :eek:Check pin8 and C2 on the LM393.
I gave the correct values for R11 and R12.
I'm so glad you have patience.... I didn't notice the resistor sizes and they make perfect sense setting the VREF to the same as what VCC will be at 11v. I moved the cap to the neg side, Was that the problem? I'll add the hysteresis too...

1768783306669.png
 

Thread Starter

johnaustinkaty

Joined Jul 16, 2021
284
UPDATE....
I got my PCBs from JLCPCB and they did a great job as always. I have assembled several boards and all appear to be working as designed in my device. However, I am now realizing that I need to have the low battery warning set to trigger when the battery goes below 12v instead of 11v as it is currently. Over several months of testing with a few different feeder type batteries (7 Ah), it appears that I have discharged them too low too many times and now their capacity is diminished. One of my batteries is showing 12.99v, but my light restarts as soon as it draws >130 mA approx. I tested the same unit with another good battery that is at 11.7v and it worked just fine. Should I revise my resistors to attain a warning at <12v or should I leave it as is? If I do need to change it to <12v, can you provide resistor sizes? I also want to confirm that the low battery warning should trigger when the "battPin" on the attiny goes LOW? Thx again for everyone's help and I consider the effort encompassed in this thread to be a success. I just need to dial in my low battery warning (rapid flashing red leds) at the end of my sketch. I am attaching my board, schematic and code for reference.
Screenshot 2026-02-09 201953.png
PCB.png
Code:
//8 mghz

//                                  ***MAIN LED AT 91.7% BRIGHTNESS WITH LOW BATT ALERT***
//                          THIS SKETCH IS THE LATEST FOR THE NEW 2026 PCB WITH THE FORUM TEAM

//                            THIS SKETCH IS CONFIGURED FOR EASY TESTING (QUICK FADE UP AND DOWN)
//                                            REVISE FADE TIMING WHEN TESTING IS COMPLETE

const int ledPin = 0;
const int sensorPin = 2;
const int alertPin = 1;
const int battPin = 5;
const int darkPin = 3;


int delayTime = 30000;
int sensorValue = LOW;
unsigned long lastmotion;

// ATMEL ATTINY85
//
//                            +-\/-+
//  BATT (D 5) PB5  1|      |8  Vcc
// DARK (D 3) PB3  2|      |7  PB2 (D 2) SENSOR
//           (D 4) PB4  3|      |6  PB1 (D 1) ALERT
//                  GND  4|      |5  PB0 (D 0) LED
//                            +----+



void setup()
{
  pinMode(ledPin, OUTPUT);
  pinMode(sensorPin, INPUT);
  pinMode(alertPin, OUTPUT);
  pinMode(battPin, INPUT_PULLUP);
  pinMode(darkPin, INPUT_PULLUP);

  for (int i = 0; i < 10; i++) {
        digitalWrite(alertPin, HIGH);  // turn the LED on
        delay(50);                    // wait for 500 milliseconds (half a second)
        digitalWrite(alertPin, LOW);   // turn the LED off
        delay(2000);                    // wait for 500 milliseconds
      }

  //digitalWrite(sensorPin, LOW);//CHANGE TO LOW AFTER TESTING
  //digitalWrite(battPin, HIGH); //CHANGE TO LOW AFTER TESTING
  //digitalWrite(darkPin, HIGH);//CHANGE TO LOW AFTER TESTING
  delay(60000);  //UNCOMMENT AFTER TESTING
}



void loop() {
  while (digitalRead(darkPin) == HIGH) {
    digitalWrite(ledPin, LOW);
    digitalWrite(alertPin, LOW);
  }

  while (digitalRead(darkPin) == LOW) {  //CHANGE TO LOW AFTER TEST
    //digitalWrite(sensorPin, LOW);
    analogWrite(ledPin, 1);
    analogWrite(alertPin, 25);
    //delay(3000);


    if (digitalRead(sensorPin) == HIGH) {
      for (int i = 0; i < 5; i++) {
        digitalWrite(alertPin, HIGH);  // turn the LED on
        delay(500);                    // wait for 500 milliseconds (half a second)
        digitalWrite(alertPin, LOW);   // turn the LED off
        delay(500);                    // wait for 500 milliseconds
      }
      analogWrite(alertPin, 25);
      // fade in from min to max 550 mA:
      for (int fadeValue = 1 ; fadeValue <= 234; fadeValue += 1) {
        // sets the value (range from 2 to 234):
        analogWrite(ledPin, fadeValue);
        // wait for 30 milliseconds to see the dimming effect
        delay(384); //CHANGE TO 384 AFTER TEST
      }
      delay(120000);

//      lastmotion = millis();
//
//
//      while (millis() < lastmotion + delayTime) {
//        if (digitalRead(sensorPin) == HIGH) {  // should this be LOW??
//          lastmotion = millis();
//        }
//      }
    

      // fade out from max to min 40 mA:
      for (int fadeValue = 234 ; fadeValue >= 1; fadeValue -= 1) {
        // sets the value (range from 0 to 255):
        analogWrite(ledPin, fadeValue);
        // wait for 30 milliseconds to see the dimming effect
        delay(193); //CHANGE TO 193 AFTER TEST
      }


      delay(1000);

      if (digitalRead(battPin) == HIGH) {  //CHANGE TO LOW AFTER TEST??

        for (int i = 0; i < 10; i++) {
          digitalWrite(alertPin, HIGH);  // turn the LED on
          delay(50);                    // wait for 50 milliseconds (20th of a second)
          digitalWrite(alertPin, LOW);   // turn the LED off
          delay(300);
        } // wait for 100 milliseconds (10th of a second)

        analogWrite(ledPin, 1);
        analogWrite(alertPin, 25);
        //delay(5000);

      }
    }
  }
}
 

eetech00

Joined Jun 8, 2013
4,709
To isolate the 5 volt micro circuit.
The comparator is applying 12v to the ATtiny pin 2...thats a bad thing. The ATtiny is not 12v tolerant.
The comparators should be run from 5v supply and each of their references set to 2.5v (or less). The trigger of each should be calculated using 2.5v (or less) as a reference, while dropping the input voltage to a safe level.
 

Thread Starter

johnaustinkaty

Joined Jul 16, 2021
284
Are you guys on the same page? I'm glad you're discussing it. Please let me know if my board is good and if I can just change R11. I thank you both.
 

sghioto

Joined Dec 31, 2017
8,634
There are always several ways to do things.
This project and circuit have been evolving as features have been added.
Using what has already been working rather then a complete redesign.
I'll admit running the LM393 off the 5 volt regulator is more practical.
 

Thread Starter

johnaustinkaty

Joined Jul 16, 2021
284
There are always several ways to do things.
This project and circuit have been evolving as features have been added.
Using what has already been working rather then a complete redesign.
I'll admit running the LM393 off the 5 volt regulator is more practical.
Yes, I agree. It is what it is.
So, my questions remain....
1. Should I keep R11 as is? Or, should I change it to signal low battery at <12v instead?
2. Will battPin (pin 2 on attiny) be LOW when signal is sent from pin 7 on the LM393?
 
Top