Hello!
I have spent the past few weeks reading a lot about relay control with arduino. I'm not an electrician by study so learning basic coding and circuit building has been a fun adventure.
My first "eureka!" moment was when I was able to take temperature measurements with a thermistor.
My second "eureka!" moment was when I was able to modify the "blink" example code to turn my relay on and off using a transistor, and just today after my diode arrived, turned an LED on when the relay was closed.
My ultimate goal is to replace the LED with a 120V 60W light bulb, which is going to be in close proximity to the thermistor, which I want to use to turn the lightbulb on and off.
This is my VERY SIMPLE code that I borrowed from the Arduino Thermistor page, and I just threw in some "if else" statements hoping it might make sense.
Basically, it's saying "Temp" is not declared in the if/else statements, and I'm not sure wether to replace Temp with AnalogRead0 or what...
I'd like the Analog0 input (after it has been converted to Celsius) to control the relay ON/OFF switch so to speak. I've also put a fritzing schematic at the bottom if that helps, thank you for any help!!
#include <math.h>
int RelayHOT = 4;
double Thermister(int RawADC) {
double Temp;
Temp = log(((10240000/RawADC) - 10000));
Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp );
Temp = Temp - 273.15; // Convert Kelvin to Celcius
return Temp;
}void setup() {
Serial.begin(115200); // begin the serial monitor
pinMode(RelayHOT, OUTPUT); //set pin 4 (known as relayHOT - to an output)
}void loop() {
Serial.println(int(Thermister(analogRead(0)))); // print Celcius temp reading in serial monitor
delay(5000); // wait 5 seconds before sampling temperature againif (Temp < 28) digitalWrite(RelayHOT, HIGH); //if the temperature is less than 28C, turn on the relay which will turn on the light and increase temperature
else if (Temp > 28) digitalWrite(RelayHOT, LOW); //if the temperature is greater than 28C, turn the bulb off because it is hot enough}