Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Name: James Slough
- // Date: 01/04/2022
- // Purpose: Control a fan and 2 LEDs with a thermistor
- // Version: 1
- // Defining pins
- int thermistor_pin = A0; // The pin the thermistor is connected to
- int fan_pin = 3; // The pin the fan is connected to
- int hot_led_pin = 4; // The pin the LED for "device hot" is on
- int cool_led_pin = 5; // The pin the LED for "device cool" is on
- // Thermistor conversion variables
- const float c1 = 1.009249522e-03,
- c2 = 2.378405444e-04,
- c3 = 2.019202697e-07; // Temperature sensor constants
- const float tmp_div_res = 10000; // Resistance of temperature sensor divider
- float tmp_alg; // Variable for the analog of the thermistor
- float tmp_res; // Variable for resistance of temperature sensor
- float log_tmp_res; // Variable for log of tmp_res
- float tmp_deg; // Variable for temperature of sensor in celsius
- // Setup function
- void setup() {
- // Initializing serial
- Serial.begin(9600);
- // Initializing pins
- pinMode(thermistor_pin, INPUT);
- pinMode(fan_pin, OUTPUT);
- pinMode(hot_led_pin, OUTPUT);
- pinMode(cool_led_pin, OUTPUT);
- }
- // Main loop
- void loop() {
- // Read the temperature
- read_temperature();
- // Display the temperature in serial
- Serial.print("Temperature: ");
- Serial.print(tmp_deg);
- Serial.println(" C");
- // Temperature range logic
- if (tmp_deg > 70) { // Highest temperature range
- // Set fan to maximum
- analogWrite(fan_pin, 255);
- // Blinking hot LED
- digitalWrite(cool_led_pin, LOW); // Turn the cool LED off
- digitalWrite(hot_led_pin, HIGH); // Turn the hot LED on
- delay(100); // 0.1s delay
- digitalWrite(hot_led_pin, LOW); // Turn the hot LED off
- delay(100); // 0.1s delay
- } else if (tmp_deg > 40) { // Middle temperature range
- // Set fan to medium
- analogWrite(fan_pin, 180);
- // Hot LED on
- digitalWrite(cool_led_pin, LOW); // Turn the cool LED off
- digitalWrite(hot_led_pin, HIGH); // Turn the hot LED on
- } else if (tmp_deg > 30) { // Lowest temperature range
- // Set fan to low
- analogWrite(fan_pin, 120);
- // Blinking cool LED
- digitalWrite(hot_led_pin, LOW); // Turn the hot LED off
- digitalWrite(cool_led_pin, HIGH); // Turn the cool LED on
- delay(100); // 0.1s delay
- digitalWrite(cool_led_pin, LOW); // Turn the cool LED off
- delay(100); // 0.1s delay
- } else { // Lower than minimum temperature range
- // Set fan to off
- analogWrite(fan_pin, 0);
- // Cool LED on
- digitalWrite(cool_led_pin, HIGH); // Turn the cool LED on
- digitalWrite(hot_led_pin, LOW); // Turn the hot LED off
- }
- }
- // Function to read the temperature from the thermistor
- void read_temperature() {
- tmp_alg = analogRead(thermistor_pin); // Get the analog value of the thermistor
- tmp_res = tmp_div_res * (1023.0 / float(tmp_alg) - 1.0); // Get thermistor resistance
- log_tmp_res = log(tmp_res); // Get the log of the thermistor resistance
- tmp_deg = 1.0/(c1 + c2*log_tmp_res + c3*pow(log_tmp_res, 3)); // Steinhart equation
- tmp_deg -= 273.15; // Convert Kelvin to Celsius
- }
Add Comment
Please, Sign In to add comment