LED Project Low Power

Hey there,

I’m trying to controll multiple LEDs with an arduino by using a light dependent resistor (LDR).
The general setup contains (obviously) an arduino pro mini and a Ds3231 RTC.
Why the Real Time Clock?
The Arduino should consume as few power as possible, that’s why I chose the pro mini version of the arduino.

When it is dark enough the LDR becomes more resistant. In that case the Arduino emittes a current to activate a MOSFET which enlights the LEDs.

I’d like to power that setup with batteries, so I would let the arduino fall asleep during daylight.
My problem is to write the code properly to reach that goal.
If you have better ideas to save as much power as possible I would really appreciate that.

Thanks for your help, below the code I wrote (it is not complete cause I don’t know how)

/* Libraries to download

#include <avr/sleep.h> //Including the sleep library
#include <DS3232RTC.h> //Including the RTC Library
#define interruptPin 2 //Pin to wake the Arduino up

int SensorSpannung = A4;
int sensorWert = 0;
int TransistorPin = 6;

void setup() {

Serial.begin(9600);
pinMode(interruptPin, INPUT_PULLUP); //set pin d2 to wakeup-input using the buildin pullup resistor

// initialize the alarms to known values, clear the alarm flags, clear the alarm interrupt flags
RTC.setAlarm(ALM1_MATCH_DATE, 0, 0, 0, 1);
RTC.setAlarm(ALM2_MATCH_DATE, 0, 0, 0, 1);
RTC.alarm(ALARM_1);
RTC.alarm(ALARM_2);
RTC.alarmInterrupt(ALARM_1, false);
RTC.alarmInterrupt(ALARM_2, false);
RTC.squareWave(SQWAVE_NONE);

tmElements_t tm;
tm.Hour = 00; // set the RTC to an arbitrary time
tm.Minute = 00;
tm.Second = 00;
tm.Day = 4;
tm.Month = 2;
tm.Year = 2019 - 1970; // tmElements_t.Year is the offset from 1970
RTC.write™; // set the RTC from the tm structure

RTC.setAlarm(ALM1_MATCH_HOURS , 0, 19, 16); //setting the wakeup-time
RTC.alarm(ALARM_1);
// configure the INT/SQW pin for “interrupt” operation (disable square wave output)
RTC.squareWave(SQWAVE_NONE);
// enable interrupt output for Alarm 1
RTC.alarmInterrupt(ALARM_1, true);

}

void loop() {

time_t t;// creates temp time variable
t=RTC.get(); //gets current time from rtc

if (t(00, 04, 08)){ //set arduino to sleep mode if it is 8:04 am

Going_to_sleep;

}

}

sensorWert = analogRead(SensorSpannung);

if(sensorWert < 200){
digitalWrite(TransistorPin, HIGH);
}

else {
digitalWrite(TransistorPin, LOW);

delay(120000);

}

}

void Going_to_Sleep (){

sleep_enable();//Enabling sleep mode
attachInterrupt(0, wakeUp, LOW);//attaching a interrupt to pin d2
set_sleep_mode(SLEEP_MODE_PWR_DOWN);//Setting the sleep mode, in our case full sleep
sleep_cpu();//activating sleep mode
RTC.setAlarm(ALM1_MATCH_HOURS , 0, 19, 16); //setting the wakeup-time
RTC.alarm(ALARM_1);

}

void wakeUp(){
sleep_disable();//Disable sleep mode
detachInterrupt(0); //Removes the interrupt from pin 2;

}

time_t t;// creates temp time variable
    t=RTC.get(); //gets current time from rtc

or

   time_t t = RTC.get(); // No useless comments needed
      Going_to_sleep;

This makes a much sense as:

      42;

It's not a syntax error, but is does nothing.

    sleep_cpu();//activating sleep mode
      RTC.setAlarm(ALM1_MATCH_HOURS , 0, 19, 16);   //setting the wakeup-time
     RTC.alarm(ALARM_1);

Put the CPU to sleep, and then expect it to execute some code. Hmmm.

Well,
thanks at first for your answer.
If I understand you correctly, my code is absolute nonesense.

As mentioned in the first post, the important part of the code is this one:

int SensorSpannung = A4;
int sensorWert = 0;
int TransistorPin = 6;

void setup() {

  Serial.begin(9600);

}


void loop() {

sensorWert = analogRead(SensorSpannung);

  if(sensorWert < 200){
    digitalWrite(TransistorPin, HIGH);
  }
  
    else {
    digitalWrite(TransistorPin, LOW);

    delay(120000);

}

The intention of the rest is to save as much power as possible by putting the Arduino in sleep mode.
I would really appreciate any proposal to reach that goal.

I would really appreciate any proposal to reach that goal.

Your code was apparently going in that direction. But, apparently, you wanted to call the Going_to_sleep function, not just reference its address. Calling a function requires ().

You must do EVERYTHING you want to do BEFORE going to sleep, not after. The last statement in Going_to_sleep() should be the call to sleep_cpu().

If I understood it correctly, the Arduino continues the code after waking up. The intention was to set an alarm for the next time. the first alarm was set in the setup function.

Does that solution work?

Scaty

You would do it like that, wouldn’t you?

/*
 Libraries to download
 - DS3231 RTC Library (https://github.com/JChristensen/DS3232RTC/archive/master.zip)
  */

#include <avr/sleep.h>    //Including the sleep library
#include <DS3232RTC.h>    //Including the RTC Library
#define interruptPin 2    //Pin to wake the Arduino up

int SensorSpannung = A4;
int sensorWert = 0;
int TransistorPin = 6;

void setup() {

  Serial.begin(9600);
  pinMode(interruptPin, INPUT_PULLUP);    //set pin d2 to wakeup-input using the buildin pullup resistor
  
// initialize the alarms to known values, clear the alarm flags, clear the alarm interrupt flags
    RTC.setAlarm(ALM1_MATCH_DATE, 0, 0, 0, 1);
    RTC.setAlarm(ALM2_MATCH_DATE, 0, 0, 0, 1);
    RTC.alarm(ALARM_1);
    RTC.alarm(ALARM_2);
    RTC.alarmInterrupt(ALARM_1, false);
    RTC.alarmInterrupt(ALARM_2, false);
    RTC.squareWave(SQWAVE_NONE);


    tmElements_t tm;
    tm.Hour = 00;               // set the RTC to an arbitrary time
    tm.Minute = 00;
    tm.Second = 00;
    tm.Day = 4;
    tm.Month = 2;
    tm.Year = 2019 - 1970;      // tmElements_t.Year is the offset from 1970
    RTC.write(tm);              // set the RTC from the tm structure

    // configure the INT/SQW pin for "interrupt" operation (disable square wave output)
    RTC.squareWave(SQWAVE_NONE);
    // enable interrupt output for Alarm 1
    RTC.alarmInterrupt(ALARM_1, true);

}

void loop() {

time_t t;// creates temp time variable
    t=RTC.get(); //gets current time from rtc

    if (t(00, 04, 08)){      //set arduino to sleep mode if it is 8:04 am
        
      Going_to_sleep();
      
      
      }

  
  
  
  }

  sensorWert = analogRead(SensorSpannung);

  if(sensorWert < 200){
    digitalWrite(TransistorPin, HIGH);
  }
  
    else {
    digitalWrite(TransistorPin, LOW);

    delay(120000);
    
  }


  

}


void Going_to_Sleep (){

  sleep_enable();//Enabling sleep mode
    attachInterrupt(0, wakeUp, LOW);//attaching a interrupt to pin d2
    set_sleep_mode(SLEEP_MODE_PWR_DOWN);//Setting the sleep mode, in our case full sleep
    sleep_cpu();//activating sleep mode
      RTC.setAlarm(ALM1_MATCH_HOURS , 0, 19, 16);   //setting the wakeup-time
     RTC.alarm(ALARM_1);
     sleep_cpu();//activating sleep mode
    
  }

  void wakeUp(){
   sleep_disable();//Disable sleep mode
  detachInterrupt(0); //Removes the interrupt from pin 2;
 
}

I’ve completely re-written my code, could someone check it? :slight_smile:

#include <DS3231.h> // https://github.com/NorthernWidget/DS3231
#include <avr/sleep.h>
#include <wire.h>

DS3231  rtc(SDA, SCL);
Time t;

const int OnHour = 16;
const int OnMin = 19;
const int OffHour = 8;
const int OffMin = 4;

int SensorSpannung = A4;
int sensorWert = 0;
int TransistorPin = 6;
const int wakeUpPin = 2;

#define ALRM2_MATCH_HR_MIN 0b100


void setup() {


  CLKPR = 0x80;   //Bei Problemen diese zwei Zeilen rausnehmen, sie verringern die CPU Clock
  CLKPR = 0x04;

  set_sleep_mode (SLEEP_MODE_PWR_DOWN);


  Serial.begin(9600);
  rtc.begin();

  // Configure Interrupt Pin
  pinMode(wakeUpPin, INPUT_PULLUP);
  digitalWrite(wakeUpPin, HIGH);

  delay(100);

  // Start the I2C interface
  Wire.begin();

  // Set time
  Clock.setClockMode(false);
  Clock.setYear(Year);
  Clock.setMonth(Month);
  Clock.setDate(Date);
  Clock.setHour(Hour);
  Clock.setMinute(Minute);
  Clock.setSecond(Second);

  
  byte ALRM2_SET = ALRM2_MATCH_HR_MIN;

  int ALARM_BITS =ALRM2_SET;
  ALARM_BITS <<= 4;

   Clock.setA2Time(0, 16, 19, ALARM_BITS, false, false, false);
   Clock.turnOnAlarm(2);

   attachInterrupt(digitalPinToInterrupt(wakeUpPin), wakeUp, FALLING);
   delay(500);



}

void loop() {

    Clock.setA2Time(0, 16, 19, ALARM_BITS, false, false, false);
    Clock.turnOnAlarm(2);

  t = rtc.getTime();

  if (t.hour == OffHour && t.min == OffMin) {
    sleep_enable();
    delay(500);
    sleep_cpu;

  }

    
  sensorWert = analogRead(SensorSpannung);

  while(sensorWert < 200){
    digitalWrite(TransistorPin, HIGH);
    delay(55000);
  }
  
    digitalWrite(TransistorPin, LOW);
    delay(500);
    
  }



}

void wakeUp() {
  }