We are designing a boat that can be controlled by a DC
motor, and a servo. We are also including a red LED to indicate that temperature
is below some certain temperature (a temperature that we can set). We are also including
a yellow LED to show that the temperature is now above the set temperature. Both
action ( red or yellow) are going to be shown on a LCD with the current temperature
at that moment.
hardware:
1. temperature sensor.
2. LCD (16x2)
3. a red LED.
4. a yellow LED
5. potentiometer
6. 2 resistor.
7. wires.
code:
/*
Basic Analogue Input Control - Temperature Sensor
This example reads an analogue value from analogue in pin 1 and then compares this to a set point.
If the set point is threshold is crossed, the LED on digital pins 8,9 is turned on/off. A deadband is
provided to prevent high speed toggling at the set point transition.
The logic used below with act on Rising temperature.
The analogue input will come from a TMP36 temperature sensor.
-40'C to 125'C = 100mV to 1750mv (linear 10mV per 'C)
*/
int tempIn = 0; // temperature sensor on Analogue Pin 3
int ledOut = 8; // LED on Digital Pin 8
int LedOut1 = 9; // LED on Digital Pin 9
int aiValue = 0; // input value (0-1023 = 0 to 5v)
float setPoint =30; // Trigger value in 'C
float deadband = 0.5; // Differential for reset value in 'C
#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
/*
NOTE: the deadband action can be disabled by setting the deadband to 0
*/
float degC; // The temperature in Degrees Centigrade
void setup()
{
pinMode(ledOut, OUTPUT); // Configure the Digital Pin Direction for the LED
pinMode(LedOut1, OUTPUT);
lcd.begin(16, 2);
lcd.clear();
Serial.begin(9600);
}
void loop()
{
degC = getTemperature(getVolts()); //Get the Volts and the Temperature using Helper functions.
if (degC > setPoint)
{
digitalWrite(ledOut, HIGH); // Temperature Limit Reached, turn the LED on.
Serial.println(" | Output: ON.");
lcd.setCursor(0,1);
lcd.print(" Yellow ");
lcd.print(degC);
}
else
{
if (degC > (setPoint - deadband))
{
digitalWrite(ledOut, LOW); // Temperature dropped below SP and deadband, turn the LED off.
Serial.println(" | Output: OFF.");
lcd.setCursor(0,1);
lcd.print(" ");
}
}
if (degC < setPoint)
{
digitalWrite(LedOut1, HIGH); // Temperature Limit Reached, turn the LED on.
Serial.println(" | Output: ON.");
lcd.setCursor(0,1);
lcd.print(" RED ");
lcd.print(degC);
}
else
{
if (degC < (setPoint - deadband))
{
digitalWrite(LedOut1, LOW); // Temperature dropped below SP and deadband, turn the LED off.
Serial.println(" | Output: OFF.");
lcd.setCursor(0,1);
lcd.print(" ");
}
}
delay(1000);
}
/*
Return the voltage for the input value; 0-1023 = 0 to 5V
*/
float getVolts()
{
int inputValue;
inputValue = analogRead(tempIn);
float volts;
volts = (((float)inputValue / 1024) * 5);
Serial.print("Input Value: ") ; Serial.print(inputValue);
Serial.print(" | Voltage: ") ; Serial.print(volts);
return volts;
}
/*
Return the temperature in DegreesC for given voltage
*/
float getTemperature(float volts)
{
float temp = (volts - 0.5) / 0.01 ;
Serial.print(" | Temperature: "); Serial.print(temp); Serial.print(" 'C");
return temp;
}
some images: