Thursday, August 7, 2014

Brewduino

PURPOSE

I just finished testing my new experiment: a device that keeps the temperature of a fermenter between two set values and display and record the data over time. It takes about 6-7 days to ferment a home brew beer, and the temperature must be kept between 18 and 24 degrees (it can change slightly depending on the beer). If it's too cold the fermentation stops, if it's too warm the beer goes bad. I bought a 30W heating belt to wrap the fermenter, but I wanted it to go on and off automatically when needed, so I set up my Arduino Uno, a temperature sensor (DHT11) and a relay to control the AC current using a DC signal of 5 Volts. Additionally I attached an LCD to see the temperature read by the sensor and compare it with the analog thermometer, just to double check. I also like to analyse data so I attached an SD card module to record the temperature and state of the heating belt (on and off) over time.

SCHEMATICS



As you can see from the schematics, I'm using pin 5 to send the signal to the relay that controls the AC power to the heating belt. I decided to switch the active wire (brown): switching the neutral (blue) would work exactly the same but it wouldn't be safe in case of a fault. You can set the relay "normally open" (it will activate the circuit when the signal is present), or "normally closed" (it will stop the circuit when the signal is present) using different pins.
The SD card module uses 4 pins that are set in the relative library, so I didn't change them: they are 13,12,11,10 plus power and ground. These modules are supposed to work with both 3,3V and 5V but I can't get it to work with 5V so I had to use 3,3V. 
The sensor DHT11 has its own library and it only uses one pin (in this case pin 3) plus power and ground.
The connection of the LCD is always the same, with the potentiometer to control the brightness.

SKETCH

#include <LiquidCrystal.h>
#include <DHT11.h>
#include <SD.h>

LiquidCrystal lcd(9, 8, 7, 6, 4, 2);

int pin=3;
DHT11 dht11(pin); 

const int chipSelect = 10;
int t,h;

long previousMillis = 0;
long interval = 30000;

void setup(){
  Serial.begin(9600);
  pinMode(10, OUTPUT);
  pinMode(5, OUTPUT);
  lcd.begin(16, 2);
  if (!SD.begin(chipSelect)) {
    return;
  }
}

void loop(){
  int err;
  float temp, humi;
  if((err=dht11.read(humi, temp))==0)
  {
    t = (int) temp;
    h = (int) humi;
    
  lcd.print(t);

  if(t < 19){digitalWrite(5,HIGH);}
  if(t > 21){digitalWrite(5,LOW);}
    
  int state = digitalRead(5);
  lcd.setCursor(8, 0);
  lcd.print(state);

//  Serial.println(dataString);

  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis > interval) {
    
    String dataString = "";
    dataString += "temperature:";
    dataString += String(t);
    dataString += ",";
    dataString += "humidity:";
    dataString += String(h);
    dataString += ",";
    dataString += String(state);
        
    File dataFile = SD.open("datalog.txt", FILE_WRITE);
    if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    }
    previousMillis = currentMillis;   
   }
  }
    delay(1000);
    lcd.clear();
}


To write the sketch I had to include the 3 libraries to control the LCD, DHT11 and SD module. I had to use pin 9,8,7,6,4,2 to control the LCD because I'm using 13,12,11,10 to control the SD module (and I can't change that unless I change the library). The "chipSelect" is pin 10 in the Arduino Uno (it's different in the Mega). Pin 3 is used by the sensor and pin 5 is the pin that controls the relay.
The sketch itself is pretty simple, I took parts from the datalogger sketch. I had to introduce a different kind of delay: I want the program to run every second, but to record data on the SD only every 30 seconds. To do that I can't use the function delay(30000), because it would stop the whole program for 30 seconds. I'm using the function millis() that counts the milliseconds passed since the start of the program. This value is compared to the set interval and when it's larger than that, it execute the part of the program that writes the data into "datalog.txt" on the SD card.
The function digitalRead() can also read an output pin. For example here I'm reading the state of pin 5, even though I'm using it as an output.

TEST

Because I don't want to waste beer, I filled up the fermenter with water for the purpose of this test. 


When you deal with higher voltages it's a good idea to enclose your devices in a box. 


After about 4 days I stopped the device and analized the data recorded on the SD card: I had 10545 values that correspond at about 88 hours of recording. As you can see from the chart, the temperature was kept between 19 and 21 degrees (the analog thermometer was reading the same). The last number in the log is the state of the heater (0=off, 1=on). The same value is displayed on the LCD, next to the temperature.

I imported this data into Excel and plotted a chart of temperature over time: as you can see the first day I had it set to keep the temperature between 18 and 24 degrees. The blue line indicates the temperature. The red line shows when the heater was on.


The heat belt was on for 11 hours to increase the temperature to 24 degrees, and the temperature decreased quickly after the heat belt went off. Then I changed the settings to make it work more efficiently: I just want to keep the temperatures over 18 degrees, so I set it to start the heat belt when the temp was under 19 and to stop when it was over 21. The second night it went on for a shorter time, enough to increase the temperature by 2 degrees (about 2 hours), the third night it didn't even go on because it wasn't cold enough and the fourth night it went on about 2.5 hours.

Analyzing data is useful to understand if the device is working efficiently or it can be improved. For example, I can observe that it takes about 1.5 hours to increase 23 liters by one degree, using about 30W of power.
I also plugged in a power meter at the heat belt to check the consumption: it uses maximum 40W power and at the end of the experiment the total energy consumption was about 680Wh.

2 comments:

  1. Hi, it's working for me... The sd card library and the lcd library use the same pins, so you have to change the libraries to use different pins

    ReplyDelete