Articole din categoria 'Arduino'

Chronothermostat

UNDER CONSTRUCTION

This was my first actual project with Arduino, and after about 6 months it was done (this included learning how to work with Arduinos, waiting for various components from Asia and waiting for the box to be 3D printed) 🙂

Plant watering system

A simple circuit and software that checks every 12 hours if there is water is the pot tray and if it’s not it starts a pump for 6 seconds.

The water sensor is a 1 MΩ resistor.

What’s left to do:

  • Power it from a battery (that’s why I’ve included the LowPower.h library)
  • Build a box

 

 

The code:

/*
  Copyright © 2018 Razvan Murariu <razwww@gmail.com>
  This program is free software. It comes without any warranty, to
  the extent permitted by applicable law. You can redistribute it
  and/or modify it under the terms of the Do What The Fuck You Want
  To Public License, Version 2, as published by Sam Hocevar. See
  the COPYING file for more details.

  The circuit:
  Water pump: D7
  Sensor (1 MO resistor): A0
*/

#include 

#define sensorPin A0

byte pumpPin=4;
int tempo=6000;  //watering time
int i=0;  //counter

int readSensor() {
  byte readCount = 0;
  int sensorValueTotal = 0;
  int sensorValueAveraged = 0;
  digitalWrite(sensorPin,HIGH); // to avoid electrocorrosion we set it to high only during measuring
  
  while (readCount < 5) { int sensorValue = analogRead(sensorPin); // Serial.println(sensorValue); sensorValueTotal += sensorValue; readCount++; delay(10); } sensorValueAveraged = sensorValueTotal / 5; digitalWrite(sensorPin,LOW); // Serial.println(sensorValueAveraged); return (sensorValueAveraged); } void setup() { Serial.begin(9600); pinMode(pumpPin,OUTPUT); pinMode(sensorPin,INPUT); pinMode(13,OUTPUT); digitalWrite(13,LOW); //turn off led digitalWrite(pumpPin,LOW); //turn off pump } void loop() { if( i==0 && readSensor()>900 ) { // dry=982
    digitalWrite(pumpPin,HIGH);
    delay(tempo);
    digitalWrite(pumpPin,LOW);
//    Serial.println("pompa");
  }
  
  i++;
  // Enter power down state for X s with ADC and BOD module disabled
  LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);

  if( i==5400 ) {  // 10800 = once per day
    i=0; // reset i
  }          
}

 

The most recent version of the code is found on GitHub

Cat counter

This counts and displays on an LCD the number of times someone/something (of course it can be used to count anything, not only cats 🙂 ) approaches within 12 cm from the sensor. Also the back light is turned on for 5 seconds after the initial approach.

After the event all approaches are ignored for the next 2 minutes.

If the button is pressed, the LCD backlight is turned on for 5 seconds.

Schematic

 

Real circuit

 

Used

BTW, my cat eats between 10 and 22 times per day and drinks water between 8 and 15 times per day 🙂

The code:

/*
  Copyright © 2018 Razvan Murariu <razwww@gmail.com>
  This program is free software. It comes without any warranty, to
  the extent permitted by applicable law. You can redistribute it
  and/or modify it under the terms of the Do What The Fuck You Want
  To Public License, Version 2, as published by Sam Hocevar. See
  the COPYING file for more details.

  The circuit:
  Arduino Nano
  trigPin: A1
  echoPin: A0
  display SCL: A4
  display SDA: A5
  button (NC): between 5V and D2
  D2 pulled-down to ground with a 10 KΩ resistor
*/

#include 

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

const long intervalEat = 120000;
const long intervalLight = 5000;
int distance = 12;
long duration;
int distanceCm;
int counter = 0;
int buttonState = 0;
unsigned long previousMillisEat = 0;
unsigned long previousMillisButton = 0;
const int trigPin = A1;
const int echoPin = A0;

void setup() {
  lcd.begin(16,2);
  
  Serial.begin(9600);
  
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  
  pinMode(13, OUTPUT);
  pinMode(2, INPUT); // button for backlight
  
  lcd.setCursor(0,0);
  lcd.print("0   pisici");
  lcd.backlight();
  delay(intervalLight);
  lcd.noBacklight();
}

void loop() {
  unsigned long currentMillis = millis();

  delay(10);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distanceCm= duration*0.034/2;
  
  // distance smaller than 12 cm && (just powered on || intervalEat passed)
  if (distanceCm < distance && ((currentMillis < intervalEat && counter == 0) || (currentMillis - previousMillisEat >= intervalEat))) {
    previousMillisEat = currentMillis;
    counter++;
    Serial.print(distanceCm);
    Serial.println(" cm");
    Serial.print(counter);
    Serial.println(" pisici");
    Serial.println(currentMillis);
    lcd.backlight();
    lcd.setCursor(0,0);
    lcd.print(counter);
    delay(intervalLight);
    lcd.noBacklight();
  }
  
  buttonState = digitalRead(2);
  if (buttonState == LOW) {
    Serial.println("Buton");    
    lcd.backlight();
    previousMillisButton = currentMillis;
    }
    
  if ( currentMillis - previousMillisButton >= intervalLight ) {
    lcd.noBacklight();
    }

}

The most recent version of the code is found on GitHub