Automatic Arduino Kitchen Lights code problem

Hello

I love this code! However it needs to be developed. If you could give a hand it will be appreciated very much!

  • if below conditions could be injected in to the code that would be great.

if motion detected (led,HIGH)
if motion detected while (led,HIGH) reset counter. (even if leds are fading out)

  • another thing that needs to be improved is;

arduino pin 11 is not triggering mosfet enough ==> this couses heating problem.
which part should be used to trigger mosfet without sacrificing fade in and fade out speed?

  • one more thing

i want to use this code everywhere in the house. This is expensive so i have bought 5pcs Attiny85 chips
however i get below error.
serial was not declared in this scope.
do you have any ideas to improve this?

thank you in advance
Sami

  • finally if you have similar codes in your archive for this project please share.
/************************************************************
 * Led dimmer using a logic lever MOSFET transitor
 * Supply voltage is 12V / LED build for 12V, so no resistor is needed
 * PIR-sensor power is fed from Arduino 5V / GND-pins. 
 * PIR output to digital pin (D2) - polled
 * PIR can be retriggable or non retriggable
 * LED switches on when PIR detects movement
 * LED output will stay 'ON' until 5 minutes of 'PIR silence'
 * LED (Mosfet gate) MUST be connected to PWM pin. (D11)
 * this code can be drastically redused on volume - made for readability
 ****************************************************************/
// declare global constants 
const byte led = 11;         // connect to gate
const byte pir =  3;         // PIR signal out 
const byte turnOnTime = 5;   // turn on/off time in seconds !(will be forced:  50 > time > 0)
const byte minutesToOff = 1; // minutes of 'silence' before "lights out" !! ALTER THIS VALUE
const boolean debug=false;
// declare global variables
boolean lightIsOn = false;   // remember LED-status 'now'
unsigned long timeToTurnOff; // time when led  will turn off
unsigned long timeNow;       // current time


// one time setup
void setup() 
{
  // set status og used I/O-pins
  pinMode(led,OUTPUT);
  pinMode(pir,INPUT);  
}
//********************************
// small functions and procedures - to make reading of main prog easier
boolean heWantsLightsOn()
{
  return (digitalRead(pir)==HIGH);  
}
//********************************
void turnLightsOn(byte period)
{
  // now the LED shall increase light intensity over "turnOnTime" seconds
  long delayTime = period * 5 ;
  for (byte i=40; i<255; i++) 
  {
    analogWrite(led,i);
    delay(delayTime);
  }
  digitalWrite(led,HIGH); // no PWM needed - turn on 100%
  lightIsOn=true;         // remember status
}
//**********************************
void turnLightsOff(byte period)
{
  // now the LED shall decrease light intensity - slowly to zero 
  long delayTime = period*5 ;
  for (byte i=254; i>40; i--) 
  {
    analogWrite(led,i);
    delay(delayTime);
  }
  digitalWrite(led,LOW); // no PWM needed - turn off
  lightIsOn=false;         // remember status
}
//**********************************

void loop() 
{
  timeNow=millis();  // note time now
  if (heWantsLightsOn()) // if movement -> be light! 
  {  
    if (!lightIsOn) turnLightsOn(turnOnTime);   // if still dark - be light! (else light were on already)
    // set 'new' time for off -since you just told 'stay on for some mor minutes..)
    timeToTurnOff=timeNow + minutesToOff * 60000;  // set 'off-time' some minutes into the future (milliseconds used)
    if (debug) Serial.println("renew for one minute");
  }
  // now test for "Elvis  has left the building"
  if (timeNow > timeToTurnOff) // is it time to turn off ??
  {
    if (lightIsOn) turnLightsOff(turnOnTime);  // if light still are on: turn them off
  }
}
//***************END***********************

Hi Sami, are you using a logic-level MOSFET?

Paul

Hello Sami

i have bought 5pcs Attiny85 chips
however i get below error.
serial was not declared in this scope.

The ATtiny does not have hardware serial. But in this program, serial is only used for debugging, I think. Easiest solution is to remove the serial statements.

Regards

Ray

Hi Pau?,

total of 10amp leds are driven through IRF540 Hexfet Power mosfet.

i dont know if it is logic level mosfet :roll_eyes:

Sami

The data sheet says gate-source threshold is 4.0V (max) so the signal provided by the atmega328 or attiny85 is at most 1V above that. I guess this is the problem with heating. The mosfet is not completely "saturated" so its resistance has not dropped as low as it can go, hence the heat buildup.

A good replacement will have a g-s threshold of around 2V and an "on" resistance (Rds on) as low as possible.

My friend Bob (Crossroads) is one of the forum moderators and will have some good suggestions for a replacement.

Paul

PHOTOGUY:
Hi Pau?,

total of 10amp leds are driven through IRF540 Hexfet Power mosfet.

i dont know if it is logic level mosfet :roll_eyes:

If it was logic-level it would be “IRL”, not “IRF” (‘L’ for logic-level).

So…it won’t work. Start by replacing it.