Control your heating (or cooling) with A-blocks

This example shows you how you can build your own heating or cooling controller.
To build the controller you need an Arduino with A-blocks interface, button and led module (optional), temperature sensor, rotary sensor and a LCD display. Connect the button and led module to port D4, the temperature sensor to port A1, the rotary sensor to port A4 and the LCD display to A2.

Now we can start coding. To read the temperature sensor we read the value from the analog port and calculate the temperature.
 int sensorValue = analogRead(2); // Read the temperature sensor
 float voltage = ((float)sensorValue/1024) * 5 ;
 temperature = (voltage*200)*0.2222-61.111;
To set the switching temperature we read the rotary sensor
 rotary = analogRead(1)/(1023/60); // Temperature set 60 steps, 1 step 0,5 degrees
 setTemp = 30 -(rotary * 0.5);
We display the current temperature and the required temperature.
 NewSoftSerial lcd(10,18);

 lcd.print(0x80, BYTE); // Move cursor to 0,0
 lcd.print(temperature);
 lcd.print("C ");
 lcd.print(setTemp);
 lcd.print("C ");
Now we need some code to switch the heating or cooling on or off. To simulate this we just switch the led from the button and led module on or off. In a real implementation we would switch a relais on or off that turns on heating or cooling.
 if (setTemp < temperature -1) // 1 degree above required temperature. Turn the heater off.
  buttonled.ledOff();
 else if (setTemp > temperature +1) // 1 degree below required temperature. Turn the heater on.
  buttonled.ledOn();
As an extra we use the button and led module to turn the thermostat on or off. The complete source code looks like this.

/*
  Example of a simple heating thermostat
  
  A temperature sensor is used to read the temperature
  A rotary button is used to select the desired temperature.
  A switch button is used to turn the thermostat function on or off
  A led indicates if the heating is on or off
  As display a serial 2x16 LCD is used (Parallax #27977)
  
*/

#include 
#include 

ButtonLed buttonled(4);
NewSoftSerial lcd(10,18);

void setup()
{
  pinMode(18, OUTPUT);
  lcd.begin(19200);
  lcd.print(0x16, BYTE); // Cursor off, no blink
  lcd.print(0x0C, BYTE); // Clear screen
  delay(10);
  lcd.print(0x11, BYTE); // Backlight on
  buttonled.blink(3);
}

float setTemp = 0;
int rotary ;
bool isActive = true;
float temperature;

void loop()
{
  
  int sensorValue = analogRead(2); // Read the temperature sensor
  float voltage = ((float)sensorValue/1024) * 5 ;
  temperature = (voltage*200)*0.2222-61.111;
  
  // Read the rotary sensor
  rotary = analogRead(1)/(1023/60); // Temperature set 60 steps, 1 step 0,5 degrees
  setTemp = 30 -(rotary * 0.5);

  if (buttonled.buttonIsDown()) // Use the button to turn the thermostat function on or off
  {
    isActive = !isActive;
    delay(500);
  }  

  lcd.print(0x80, BYTE); // Move cursor to 0,0
  lcd.print(temperature);
  lcd.print("C    ");
  lcd.print(setTemp);
  lcd.print("C ");

  lcd.print(0x94, BYTE); // Move cursor to 1,0
  lcd.print("Thermostat ");
  
  if (isActive)
    lcd.print("On ");
  else
    lcd.print("Off");

  // Control the heating. In this case we just switch a led on or off we could also 
  // use a servo to turn a knob or a relais to switch the heating on or off
  if (isActive)
  {
    if (setTemp < temperature -1) // 1 degree above required temperature. Turn the heater off.
      buttonled.ledOff();
    else if (setTemp > temperature +1) // 1 degree below required temperature. Turn the heater on.
      buttonled.ledOn();
  }
  
  delay(100);
  
}
Shopping cart