New to using Arduino, I've had this board in my cupboard for a few years so thought i finally use it.
I'm planing to make a temp control unit using 2 relays and a temp sensor but unsure if my schematic is correct, could someone have a look and see if i have made a mistake please.
I'm stilll waiting for parts to arrive so i cannot test it.
Heres the code i wrote aswell
#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
int RELAY1 = A1; // Heater
int RELAY2 = A2; // Fan
float Range = 3 // Unused
//Key connections with arduino
const int up_key=3;
const int down_key=2;
int SetPoint=30;
//=================================================================
// SETUP
//=================================================================
void setup(){
pinMode(LED_RED,OUTPUT);
pinMode(LED_GREEN,OUTPUT);
pinMode(RELAY1,OUTPUT);
pinMode(RELAY2, OUTPUT);
pinMode(up_key,INPUT_PULLUP);
pinMode(down_key,INPUT_PULLUP);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("Temp Controller v1");
lcd.setCursor(0,1); //Move coursor to second Line
lcd.print("Temp. Controller");
digitalWrite(LED_GREEN,HIGH); //Green LED Off
digitalWrite(LED_RED,LOW); //Red LED On
digitalWrite(RELAY1,LOW); //Turn off Heater Relay
digitalWrite(RELAY2,LOW); //Turn off Fan Relay
delay(2000);
}
//=================================================================
// LOOP
//=================================================================
void loop(){
double Temperature = ((5.0/1024.0) * analogRead(A0)) * 100; //10mV per degree 0.01V/C. Scalling
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>0)
{
SetPoint--;
}
}
if(digitalRead(up_key)==LOW)
{
if(SetPoint<150)
{
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)
{
digitalWrite(RELAY1,LOW); //Turn off heater
digitalWrite(LED_RED,LOW); //Turn off Red LED
digitalWrite(LED_GREEN,HIGH); //Turn on Green LED
}
// If temp is over limit turn on fan
if(Temperature > SetPoint)
{
digitalWrite(RELAY1,LOW); //Turn off heater
digitalWrite(RELAY2,HIGH); //Turn on fan
digitalWrite(LED_RED,LOW); //Turn off Red LED
digitalWrite(LED_GREEN,HIGH); //Turn on Green LED
}
//if temp is below limt turn on heater / turn off fan
if(Temperature < SetPoint )
{
digitalWrite(RELAY1,HIGH); //Turn on heater
digitalWrite(RELAY2,LOW); //Turn off fan
digitalWrite(LED_GREEN,LOW); //Turn off Green LED
digitalWrite(LED_RED,HIGH); //Turn on RED LED
}
delay(100); //Update at every 100mSeconds
}
Welcome!
Thanks for posting your code in code tags, very nice for a new resident.
One thing, before grabbing the code to post, ctrl-T is your friend in the IDE. It formats your code, and the indentation it provides gives lots of hints when you're looking for bugs...
The way that your switches are wired, you should use pinMode(pin, INPUT_PULLUP); // enable the internal pullup resistor
Unless the wires are long and you have trouble with noise. If that is the case you may need a lower value (start with 10K) external resistor for a stronger pullup.
A 0.1uF cap across each switch can also help with noise and provide hardware debounce.
Not sure, the order might matter, might have to write the ones before declaring input, not sure. I never paid much attention, INPUT_PULLUP was available when I jumped into this pool.
One would write HIGH to the pin after setting the pinMode.
But since the pins are inputs (with the internal pullups disabled) by default you don't actually have to set the input to INPUT, just do the digitalWrite(pin, HIGH);
The pinMode(pin, INPUT_PULLUP); is preferred for readability nowadays, though.
You may find that the setpoint changes more rapidly than you want when you hold the buttons to change the setpoint down. You can use the state change detection method to only increment or decrement once per button press.
Would this be the way to implement state change detection?
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(9, 8, 7, 6, 5, 4);
// this constant won't change:
//Key connections with arduino
const byte down_key = 2; // the pin that the pushbutton is attached to
const byte up_key = 3;
//Led connections with arduino
const int LED_RED=10; //Red LED
const int LED_GREEN=11; //Green LED
// Variables will change:
int RELAY1 = A1; // Heater
int RELAY2 = A2; // Fan
int Range = 3; // Temp +/- Range
int SetPoint=30; // Default temp
int buttonPushCounter_down_key = 0; // counter for the number of button presses
boolean ButtonState_down_key = 0; // current state of the button
boolean lastButtonState_down_key = 0; // previous state of the button
int buttonPushCounter_up_key = 0; // counter for the number of button presses
boolean ButtonState_up_key = 0; // current state of the button
boolean lastButtonState_up_key = 0; // previous state of the button
//=================================================================
// SETUP
//=================================================================
void setup(){
pinMode(LED_RED,OUTPUT);
pinMode(LED_GREEN,OUTPUT);
pinMode(RELAY1,OUTPUT);
pinMode(RELAY2, OUTPUT);
// initialize the button pin as a input:
pinMode(down_key, INPUT_PULLUP);
pinMode(up_key, INPUT_PULLUP);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("Temp Controller v1");
//lcd.setCursor(0,1); //Move coursor to second Line
//lcd.print("Temp. Controller");
digitalWrite(LED_GREEN,HIGH); //Green LED Off
digitalWrite(LED_RED,LOW); //Red LED On
digitalWrite(RELAY1,LOW); //Turn off Heater Relay
digitalWrite(RELAY2,LOW); //Turn off Fan Relay
delay(2000);
}
//=================================================================
// LOOP
//=================================================================
void loop(){
double Temperature = ((5.0/1024.0) * analogRead(A0)) * 100; //10mV per degree 0.01V/C. Scalling
lcd.setCursor(0,0);
lcd.print("Temperature:"); //Do not display entered keys
lcd.print(Temperature);
static unsigned long timer = 0;
unsigned long interval = 20;
if (millis() - timer >= interval)
{
timer = millis();
// read the pushbuttons input pin:
ButtonState_down_key = digitalRead(down_key);
ButtonState_up_key = digitalRead(up_key);
// compare the ButtonState_down_key to its previous state
if (ButtonState_down_key != lastButtonState_down_key)
{
// if the state has changed, increment the counter
if (ButtonState_down_key == LOW)
{
// if the current state is LOW then the button went from off to on:
buttonPushCounter_down_key++;
if(SetPoint>0)
{
SetPoint--;
}
}
lastButtonState_down_key = ButtonState_down_key;
}
// compare the ButtonState_up_key to its previous state
if (ButtonState_up_key != lastButtonState_up_key)
{
// if the state has changed, increment the counter
if (ButtonState_up_key == LOW)
{
// if the current state is LOW then the button went from off to on:
buttonPushCounter_up_key++;
if(SetPoint<150)
{
SetPoint++;
}
}
lastButtonState_up_key = ButtonState_up_key;
}
//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)
{
digitalWrite(RELAY1,LOW); //Turn off heater
digitalWrite(LED_RED,LOW); //Turn off Red LED
digitalWrite(LED_GREEN,HIGH); //Turn on Green LED
}
// If temp is over limit turn on fan /Turn off heater
if(Temperature > (SetPoint+Range))
{
digitalWrite(RELAY1,LOW); //Turn off heater
digitalWrite(RELAY2,HIGH); //Turn on fan
digitalWrite(LED_RED,LOW); //Turn off Red LED
digitalWrite(LED_GREEN,HIGH); //Turn on Green LED
}
//if temp is below limt turn on heater / turn off fan
if(Temperature < (SetPoint-Range))
{
digitalWrite(RELAY1,HIGH); //Turn on heater
digitalWrite(RELAY2,LOW); //Turn off fan
digitalWrite(LED_GREEN,LOW); //Turn off Green LED
digitalWrite(LED_RED,HIGH); //Turn on RED LED
}
delay(100); //Update at every 100mSeconds
}
}
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) to format your code for readability. All of the loop code is inside the millis() timer, including the 100 ms delay. Is that the structure that you want?
Thats a nice feature Pretty sure i've sorted it now using wokwi.com
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(9, 8, 7, 6, 5, 4);
// this constant won't change:
//Key connections with arduino
const byte down_key = 2; // the pin that the pushbutton is attached to
const byte up_key = 3;
//Led connections with arduino
const int LED_RED = 10; //Red LED
const int LED_GREEN = 11; //Green LED
const float BETA = 3950; // should match the Beta Coefficient of the thermistor
// Variables will change:
int RELAY1 = A1; // Heater
int RELAY2 = A2; // Fan
float Range = 3.00; // Temp +/- Range
int SetPoint = 30; // Default temp
int buttonPushCounter_down_key = 0; // counter for the number of button presses
boolean ButtonState_down_key = 0; // current state of the button
boolean lastButtonState_down_key = 0; // previous state of the button
int buttonPushCounter_up_key = 0; // counter for the number of button presses
boolean ButtonState_up_key = 0; // current state of the button
boolean lastButtonState_up_key = 0; // previous state of the button
//=================================================================
// SETUP
//=================================================================
void setup() {
pinMode(LED_RED, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
// initialize the button pin as a input:
pinMode(down_key, INPUT_PULLUP);
pinMode(up_key, INPUT_PULLUP);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("Temp Controller");
lcd.setCursor(0, 1); //Move coursor to second Line
lcd.print("Version 1");
digitalWrite(LED_GREEN, HIGH); //Green LED Off
digitalWrite(LED_RED, LOW); //Red LED On
digitalWrite(RELAY1, LOW); //Turn off Heater Relay
digitalWrite(RELAY2, LOW); //Turn off Fan Relay
delay(2000);
}
//=================================================================
// LOOP
//=================================================================
void loop() {
int analogValue = analogRead(A0);
float Temperature = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
lcd.setCursor(0, 0);
lcd.print("Temperature:"); //Do not display entered keys
lcd.print(Temperature);
static unsigned long timer = 0;
unsigned long interval = 20;
if (millis() - timer >= interval)
{
timer = millis();
// read the pushbuttons input pin:
ButtonState_down_key = digitalRead(down_key);
ButtonState_up_key = digitalRead(up_key);
// compare the ButtonState_down_key to its previous state
if (ButtonState_down_key != lastButtonState_down_key)
{
// if the state has changed, increment the counter
if (ButtonState_down_key == LOW)
{
// if the current state is LOW then the button went from off to on:
buttonPushCounter_down_key++;
if (SetPoint > 0)
{
SetPoint--;
}
}
lastButtonState_down_key = ButtonState_down_key;
}
// compare the ButtonState_up_key to its previous state
if (ButtonState_up_key != lastButtonState_up_key)
{
// if the state has changed, increment the counter
if (ButtonState_up_key == LOW)
{
// if the current state is LOW then the button went from off to on:
buttonPushCounter_up_key++;
if (SetPoint < 100)
{
SetPoint++;
}
}
lastButtonState_up_key = ButtonState_up_key;
}
}
//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)
{
digitalWrite(RELAY1, LOW); //Turn off heater
digitalWrite(LED_RED, LOW); //Turn off Red LED
digitalWrite(LED_GREEN, HIGH); //Turn on Green LED
}
// If temp is over limit turn on fan /Turn off heater
if (Temperature > (SetPoint + Range))
{
digitalWrite(RELAY1, LOW); //Turn off heater
digitalWrite(RELAY2, HIGH); //Turn on fan
digitalWrite(LED_RED, LOW); //Turn off Red LED
digitalWrite(LED_GREEN, HIGH); //Turn on Green LED
}
//if temp is below limt turn on heater / turn off fan
if (Temperature < (SetPoint - Range) || Temperature < SetPoint)
{
digitalWrite(RELAY1, HIGH); //Turn on heater
digitalWrite(RELAY2, LOW); //Turn off fan
digitalWrite(LED_GREEN, LOW); //Turn off Green LED
digitalWrite(LED_RED, HIGH); //Turn on RED LED
}
delay(100); //Update at every 100mSeconds
}