lunes, 16 de enero de 2012

Detector de nivel de agua con Atmega

Detector sin Arduino
En casa tenemos un riego automático que funciona aceptablemente bien. El problema es que la bomba está sumergida en un depósito de unos 5 litros y hay que rellenarlo con cierta frecuencia.

En septiembre del 2010 busqué la forma de hacer un detector que avisara cuando el nivel estuviera demasiado bajo. Me basé en "Detector de nivel para un depósito de agua". Pero yo quería justo lo contrario. Es decir que se encendiese un led al bajar el nivel, no que se fuesen apagando. Por dos razones, para que llamase la atención... y para que "gastase menos". El caso es que al 4066 (switch analógico digital) le añadí un 7404 (NOT) y el circuito me quedó así:


Funcionar funcionaba, pero las tres pilas AAA que tenía de alimentación se las comía en tres días...
Ya conocía Arduino, peor me parecía una pasada gastarme 35€ en el asunto.

 De Arduino a microcontrolador con consumo reducido.
En varios sitios....:

Hay que ponerla también en el duemilanove!!!:

El producto final.
En varios sitios....:
3 pilas AAA nuevas han durado al más de 7 días, desde el El 6/1/2012 a las 17:10 al 14/01/2012.


Tengo que calcular cuánto chupa un led encendido siempre. Una modificación plausible sería poner un pulsador para comrpobar que sigue con pilas en vez de tener el led encendido siempre...

Vuelvo a ponerle pilas el 15/01/2012 a las 20:30

Circutio en eagle o kicad: ofú...
Fotos:

código:

//****************************************************************

/*

 * Water level test (based in Watchdog Sleep)

 * Probes  on analog0 and  pin 10
 
 * Indicator LED in pin 13
 

 */

//****************************************************************



#include <avr/sleep.h>

#include <avr/wdt.h>



#ifndef cbi

#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))

#endif

#ifndef sbi

#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))

#endif



int nint;        // interaction number

int ntimes=255;     // the longest time whatchdog can manage is 9= 8sec but it is enough to read the level each 30 minutes. So here we put 225 (=30x60/8)

int pinLed=13;  //

int pinFeeder=10;



volatile boolean f_wdt=1;



void setup(){

  Serial.begin(9600);

  pinMode(pinLed,OUTPUT);

  pinMode(pinFeeder,OUTPUT);

  Serial.println("Water level test");



  // CPU Sleep Modes 

  // SM2 SM1 SM0 Sleep Mode

  // 0    0  0 Idle

  // 0    0  1 ADC Noise Reduction

  // 0    1  0 Power-down

  // 0    1  1 Power-save

  // 1    0  0 Reserved

  // 1    0  1 Reserved

  // 1    1  0 Standby(1)



  cbi( SMCR,SE );      // sleep enable, power down mode

  cbi( SMCR,SM0 );     // power down mode

  sbi( SMCR,SM1 );     // power down mode

  cbi( SMCR,SM2 );     // power down mode



  setup_watchdog(9); // 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec

}



byte del;

int cnt;

byte state=0;

int water=0; 





//****************************************************************

//****************************************************************

//****************************************************************

void loop(){



  int modulo=0;

  if (f_wdt==1) {  // wait for timed out watchdog / flag is set when a watchdog timeout occurs

    f_wdt=0;       // reset flag

    // Read only each ntimes times
    modulo=nint%ntimes;
    
    if (modulo==0){
      digitalWrite(pinFeeder,1);  // send voltage in the water
      
      delay(10);
    
      water=analogRead(0);  // reading probes
      
      digitalWrite(pinFeeder,0);  // stop voltage in the water
     
  
      Serial.print("water: " );
  
      Serial.println(water );
  
  
  
      switch (state){
  
      case 0:     // probes are in the water
  
        if (water < 900) {  // if there is no currente between the probes
  
          state=1;
          digitalWrite(pinLed,1);  // switch on the warning light
        }
  
        break;
  
      case 1:  // probes out of the water
  
        if (water > 1000)  { // if some current go through the probes
  
          state=0;
          digitalWrite(pinLed,0);  // switch off the warning light
  
  
        }
  
        break;
  
      }

    }

    nint++;

    



    Serial.print("Sleep " );

    Serial.println(nint );

    delay(2);               // wait until the last serial character is send

    



  //  pinMode(pinLed,INPUT); // set all used port to intput to save power

    pinMode(pinFeeder,INPUT); // set feeder port to intput to save power



    system_sleep();



  // pinMode(pinLed,OUTPUT); // set all ports into state before sleep

    pinMode(pinFeeder,OUTPUT); // set feeder port into state before sleep





  }



}




//****************************************************************  

// set system into the sleep state 

// system wakes up when wtchdog is timed out

void system_sleep() {



  cbi(ADCSRA,ADEN);                    // switch Analog to Digitalconverter OFF



  set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here

  sleep_enable();



  sleep_mode();                        // System sleeps here



    sleep_disable();                     // System continues execution here when watchdog timed out 

    sbi(ADCSRA,ADEN);                    // switch Analog to Digitalconverter ON



}



//****************************************************************

// 0=16ms, 1=32ms,2=64ms,3=128ms,4=250ms,5=500ms

// 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec

void setup_watchdog(int ii) {



  byte bb;

  int ww;

  if (ii > 9 ) ii=9;

  bb=ii & 7;

  if (ii > 7) bb|= (1<<5);

  bb|= (1<<WDCE);

  ww=bb;

  Serial.println(ww);





  MCUSR &= ~(1<<WDRF);

  // start timed sequence

  WDTCSR |= (1<<WDCE) | (1<<WDE);

  // set new watchdog timeout value

  WDTCSR = bb;

  WDTCSR |= _BV(WDIE);





}

//****************************************************************  

// Watchdog Interrupt Service / is executed when  watchdog timed out

ISR(WDT_vect) {

  f_wdt=1;  // set global flag

}

Fin código

No hay comentarios:

Publicar un comentario