Arduino code help for temperature controller

The forum for discussing all kinds of brewing paraphernalia.
Post Reply
Stoat on a rope

Arduino code help for temperature controller

Post by Stoat on a rope » Thu May 18, 2017 7:23 pm

Does anyone here have some skill working with Arduino?

I'm building a heated container for growing yeast in that will be controlled by an arduino board and I've got it working to an extent but i want to improve the code for a few aspects.

Basically its a small ceramic heater, tmp36 to read the temperature in the chamber, an LCD display showing set temperature and current temperature, two momentary buttons (to adjust temperature up and down) and two LED's to show if output is going to the heater.

This is the code Im using, Im very new to arduino, I took this code from a similar project I found and tweaked it a bit to make it work. As a result there may also be some redundant bits of code floating around in there, sorry about that. Some of the annotation might be off too as I've been editing it quite a bit.

#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(9, 8, 7, 6, 5, 4);

const int LED_RED=10; //Red LED
const int LED_GREEN=11; //Green LED
const int RELAY=12; //Lock Relay or motor

//Key connections with arduino
const int up_key=3;
const int down_key=2;

int SetPoint=20;
//=================================================================
// SETUP
//=================================================================
void setup(){
pinMode(LED_RED,OUTPUT);
pinMode(LED_GREEN,OUTPUT);
pinMode(RELAY,OUTPUT);
pinMode(up_key,INPUT);
pinMode(down_key,INPUT);

//Pull up for setpoint keys
digitalWrite(up_key,HIGH);
digitalWrite(down_key,HIGH);

Serial.begin(9600);

// set up the LCD's number of columns and rows:
lcd.begin(0, 2);
// Print a message to the LCD.
lcd.setCursor(3,0); //Move coursor to first Line
lcd.print("StoatCraft");
lcd.setCursor(1,1); //Move coursor to second Line
lcd.print("Yeast-O-Matic!");
digitalWrite(LED_GREEN,HIGH); //Green LED Off
digitalWrite(LED_RED,LOW); //Red LED On
digitalWrite(RELAY,LOW); //Turn off Relay
delay(10000);
}
//=================================================================
// LOOP
//=================================================================
void loop()

{
//getting the voltage reading from the temperature sensor
int reading = analogRead(A0);

// converting that reading to voltage
float voltage = reading * 5;
voltage /= 1024.0;

// print out the voltage
Serial.print(voltage); Serial.println(" volts");

// now print out the temperature
float Temperature = (voltage - 0.5) * 100 ; //converting from 10 mv per degree wit 500 mV offset
//to degrees ((voltage - 500mV) times 100)
Serial.print(Temperature); Serial.println(" degrees C");


lcd.setCursor(0,0);
lcd.print("Temperature:"); //Do not display entered keys
lcd.print(Temperature);

//Get user input for setpoints
if(digitalRead(down_key)==LOW)
{
if(SetPoint>12)
{
SetPoint--;
}
}
if(digitalRead(up_key)==LOW)
{
if(SetPoint<30)
{
SetPoint++;
}
}

//Display Set point on LCD
lcd.setCursor(0,1);
lcd.print("Set Point:");
lcd.print(SetPoint);
lcd.print("C ");

//Check Temperature is in limit
if(Temperature > SetPoint + 0.5)
{
digitalWrite(RELAY,LOW); //Turn off heater
digitalWrite(LED_RED,LOW);
digitalWrite(LED_GREEN,HIGH); //Turn on Green LED
}
if(Temperature < SetPoint - 0.5)
{
digitalWrite(RELAY,HIGH); //Turn on heater
digitalWrite(LED_GREEN,LOW);
digitalWrite(LED_RED,HIGH); //Turn on RED LED
}
delay(200); //Update at every 200 miliSeconds
}
//=================================================================


So there are a few things I want to change that I cant get my head around...

1. I want to get rid of the delay in the loop and use millis instead, I only want the temperature to be checked (my A0) once a minute, without affecting the read from my momentary switches (up_key and down_key)

2. I want to incorporate a debounce on the up_key and down_key

3. im struggling to get an accurate reading from my tmp36, its reading 20 or so degrees higher than it should... am I ok to just alter the 500mV offset at 10mV per degree?



Ive spent about two days trying to get this to work now but I just don't know enough about the coding yet to get it to work. Does anyone have any ideas?

Thanks to anyone that can make sense of this!

Cheers

Kieran

bobsbeer

Re: Arduino code help for temperature controller

Post by bobsbeer » Thu May 18, 2017 7:38 pm

What temp probe are you using? I don't know anything about code, but I can't see what temp sensor you are using.

ayepee
Tippler
Posts: 12
Joined: Sat Aug 17, 2013 4:49 pm

Re: Arduino code help for temperature controller

Post by ayepee » Thu May 18, 2017 7:45 pm

The temperature probe is the TMP36 - https://www.google.co.uk/url?sa=t&rct=j ... 4P_DMrpYSg

bobsbeer

Re: Arduino code help for temperature controller

Post by bobsbeer » Thu May 18, 2017 7:49 pm

Not used that sensor.

Stoat on a rope

Re: Arduino code help for temperature controller

Post by Stoat on a rope » Thu May 18, 2017 7:57 pm

I was using an lm35 and had the same problem... switched it out and changed to the appropriate coding


Sent from my iPhone using Tapatalk

bobsbeer

Re: Arduino code help for temperature controller

Post by bobsbeer » Thu May 18, 2017 8:09 pm

I have always used the DS18B20 sensors. They would need yet more of a code change, to use the i2c but they are accurate.

Stoat on a rope

Re: Arduino code help for temperature controller

Post by Stoat on a rope » Thu May 18, 2017 8:26 pm

Thanks I'll look in to it, in therory I shouldn't have any problem with the tmp36, just wondered if I was missing something.


Sent from my iPhone using Tapatalk

BigMouth
Steady Drinker
Posts: 76
Joined: Wed Sep 24, 2014 7:18 pm

Re: Arduino code help for temperature controller

Post by BigMouth » Fri May 19, 2017 12:25 am

Hi Stoat,

Not got an ardunio myself (but looked at it or a pi) but this looks like good old c to me.

You have a void loop constantly going with a slight delay at the end.

I would leave that slight delay and set up some variables before the loop for debounce state and last time and then check in the constant looping as to whether true using a function called millis ().
I would do similar for a 60 second temp check checking if 60 seconds have passed from last temp check performed and set in this inner condition.

Stoat on a rope

Re: Arduino code help for temperature controller

Post by Stoat on a rope » Fri May 19, 2017 12:10 pm

I'll have another go at it today cheers, I just can't seem to stop the millis either over writing the delay or being ignored all together. If I try and bracket the millis in a specific area, the rest of the code then can't see the parameters within that piece of code, like the voltage reading I'm taking from the tmp36.

I then move stuff around trying to solve it until my brain melts and I have to stop, go eat a biscuit, then come back and start again

I've come a long way though, a week ago I'd never written any code or even seen an arduino!


Sent from my iPhone using Tapatalk

Stoat on a rope

Re: Arduino code help for temperature controller

Post by Stoat on a rope » Fri May 19, 2017 12:26 pm

Ok I think I've solved my temp reading problem, seems just to be fluctuations on voltage in my circuit (bad connections in my breadboard etc) but I've got a nice circuit board that I've designed coming so I'll hopefully be able to run stable voltage across it all.


Sent from my iPhone using Tapatalk

Fil
Telling imaginary friend stories
Posts: 5232
Joined: Sun Oct 16, 2011 1:49 pm
Location: Cowley, Oxford

Re: Arduino code help for temperature controller

Post by Fil » Fri May 19, 2017 7:11 pm

https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay


Code: Select all


int interval =60000;

void loop() {
  // here is where you'd put code that needs to be running all the time.
      
    ie your button handling code



  // check to see if it's time to check temp
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

   // read and process your temp reading here

 
  }
}

check out the arduino playground and its forum for all the tips n tricks needed..
Last edited by Fil on Fri May 19, 2017 7:14 pm, edited 1 time in total.
ist update for months n months..
Fermnting: not a lot..
Conditioning: nowt
Maturing: Challenger smash, and a kit lager
Drinking: dry one minikeg left in the store
Coming Soon Lots planned for the near future nowt for the immediate :(

User avatar
Andy
Virtually comatose but still standing
Posts: 8716
Joined: Fri Nov 18, 2005 1:00 pm
Location: Ash, Surrey
Contact:

Re: Arduino code help for temperature controller

Post by Andy » Fri May 19, 2017 7:12 pm

Stoat on a rope wrote:
I've come a long way though, a week ago I'd never written any code or even seen an arduino!
That's great, well done! If you need any further assistance, PM me or just post on here. We'll get you on to using ESP8266 devices rather than Arduino next, built in Wifi :)
Dan!

Stoat on a rope

Re: Arduino code help for temperature controller

Post by Stoat on a rope » Sat May 20, 2017 10:30 am

Cheers! I'll have another crack at it tomorrow.

Once I've built this I'll document it and stick it up on Jims. I'm already designing my next project, a CIP machine with pre set rinse, clean and sanitise cycles...


Sent from my iPhone using Tapatalk

JabbA

Re: Arduino code help for temperature controller

Post by JabbA » Wed May 24, 2017 1:48 pm

I've built an Arduino / esp8266 based HERMs controller and fermenting fridge/ kegorator controller, like yourself I'd not heard of Arduino or been interested in microcontollers / electronics before seeing a project on here a few years ago!

I've since gone from being a CAD monkey to a design engineer responsible for mechanical design as well as PCB layouts, mostly down to home-brewing :-)

I took the easy route with debouncing and use the bounce2 library. If you need any more help, just shout!

Cheers,
Jamie

Post Reply