Thursday, July 7, 2022

0000 0000 1001 1101

Solar Light Temperature Indicator

The appearance of the Stable Joule Thief (SJT) means that I can now supply a steady reliable voltage for a microcontroller from a single solar charged NiMH battery! It still seems like a wild idea to me, even though I've been using the SJT for in projects for more than a year now.

Basically, if you can think of a simple low power project running on, for instance, an ATTiny μC, then you can use a cheap solar powered garden light to run the project.

Here is the blog link for this SJT and here is the video link that matches that post.

For this post, I've had it in mind that I'd like to look out the window on a cold night and get a rough idea of the temperature. The initial idea comes from a project of Julian Ilett where he uses a flashing LED to indicate the charge in a solar charge battery rig.

Also recently I've been looking at the 'cold monitoring' flashing lights we sometimes see on our roads here in Tassie.

For this project, I just wanted to know if the temperature was in the following ranges:

  • greater than 10 degrees celsius (no LED flashing)
  • between 7 and 10 degrees (1 flash)
  • between 4 and 7 degrees (2 flashes)
  • below 4 degrees (3 flashes)

The chosen garden light is a very cheap local hardware store version - bought in bulk for just under $2 per light.

We've seen on this blog and as a video a very similar project, but in that occasion I just wanted simple flashing LEDs.

In this version, there is an added LM35 probe which is used for temperature monitoring. The code for the ATTiny13 (at 128kHz) works as follows:

1. Sleep
2. Wake up every 3 minutes and turn on the LM35
3. Measure the temperature using the internal reference voltage
4. If it's above 10 degrees go back to sleep!
5. If it's lower than 10 degrees then flash accordingly, sleeping between flashes to conserve energy.

/*
   LM35 solar light temperature monitor
   OneCircuit:
   https://www.youtube.com/c/onecircuit-as/videos
   https://onecircuit.blogspot.com/

   pin PB1 used as VCC for LM35
   pin A1 used as analog input
   pin PB0 used as LED output

   Device: ATtiny13A
            ____________
         1 /            | 8
       o-- | RESET  VCC | --o
       o-- | PB3    PB2 | --o--A1 measure
       o-- | PB4    PB1 | --o--LM35 on/off
       o-- | GND    PB0 | --o--LED
         4 |____________| 5

   Sun 19 Jun 2022 11:45:21 AEST
*/

#include <avr/sleep.h>
#define sensorPin A1

// temp limits in celsius
#define upper_temp 10
#define mid_temp 7
#define low_temp 3

void setup() {
  analogReference(INTERNAL); // greater accuracy
  DDRB = 0b00000011;         // LEDPin output
  PORTB = 0b00111000;        // pullups enabled
}

ISR(WDT_vect) {              // watchdog ISR
}

void snooze(uint8_t cycles, uint8_t howlong) {
  // sleep preparation
  WDTCR = howlong;
  ADCSRA &= ~(1 << ADEN);
  ACSR |= (1 << ACD);
  cli();
  BODCR = (1 << BODSE) | (1 << BODS);
  BODCR = (1 << BODS);
  sei();
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_enable();

  // sleep now
  for (cycles; cycles > 0; cycles--) {
    sleep_mode();
  }

  sleep_disable();        // waking up
  ADCSRA |= (1 << ADEN);  // turn on ADC
  ACSR = (0 << ACD);      // Turn on Analog comparator.
  delay(10);              // take a breath
}

// blinking routine (including sleeping)
void blinkme(uint8_t blinks) {
  for (blinks; blinks > 0; blinks--) {
    PORTB = 0b00000001;    // turn on LED
    snooze(1, 0b01000001); // 1x32 ms sleep
    PORTB = 0b00000000;    // turn on LM35
    snooze(1, 0b01000100); // 1x250 ms sleep
  }
}

void loop() {

  DDRB = 0b00000011;    // LM35 pin to output
  PORTB = 0b00000010;   // turn on LM35
  delay(1000);          // settle LM35

  uint16_t reading = analogRead(sensorPin);
  uint16_t voltage = reading * (1100 / 1024.0);
  uint8_t temperature = voltage / 10;

  PORTB = 0b00000000;   // turn off LM35
  DDRB = 0b00000001;    // LM35 pin to input

  // analyse result
  if (temperature > upper_temp) {
    DDRB = 0b00000000;      // all pins to input
    snooze(20, 0b01100001); // 20x8 seconds
    DDRB = 0b00000001;      // LEDPin to output
  }
  else if (temperature > mid_temp) {
    blinkme(1);
  }
  else if (temperature > low_temp) {
    blinkme(2);
  }
  else {
    blinkme(3);
  }
  DDRB = 0b00000000;      // all pins to input
  snooze(1, 0b01000111);  // 1x2 seconds
  DDRB = 0b00000001;      // LEDPin to output
}

There are probably some improvements to be found - mainly by converting to assembler - but at the moment it works and works pretty well!

The early version of this project had the LM35 "on" the whole time, thus consuming more power (60 μA current drain according to the datasheet) than necessary, but the latest version runs very very sparingly!

It has been up and running happily in the garden for a few weeks now, and is might useful as well.




No comments:

Post a Comment