• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

Nano temp control

Q&A Forum › Category: Arduino › Nano temp control
0 Vote Up Vote Down
Larry asked 2 months ago
Hate to bother you with this simple sketch but it seemed to work initially
and now for some reason it don't. Has to be something simple here I am missing buy can't
seem to see it. What this circuit is supposed to do is when a given temperature is reached
it will either turn on a led and a relay or off. The temps really don't matter here ,just
using values for test purposes. The display reads out 22.5C so it should respond to the code.
Also can you tell me what I would need to do to get it to read in This is going to be a
proof of concept circuit. It will turn on a fan or off at a given temp. Currently the green
led always goes on and the red never .I have tested the led and they do work.
Thanks




Start your code here

// DHT11 Sensor:

// VCC to 5V
// GND to GND
// Data to digital pin 2

// OLED Display:

// VCC to 5V
// GND to GND
// SCL to A5 (SCL)
// SDA to A4 (SDA)
// Arduino Code:
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>
int redLed =4;
int greenLed=5;
int RLY1=7;

// OLED display settings
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET    -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

// DHT11 settings
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  // Initialize serial communication
  Serial.begin(9600);
  pinMode(redLed,OUTPUT);
  pinMode(greenLed,OUTPUT);
  pinMode(RLY1,OUTPUT);

  // Initialize the DHT sensor
  dht.begin();

  // Initialize the OLED display
  if(!display.begin(SSD1306_SWITCHCAPVCC,0x3C, OLED_RESET)) {
    Serial.println(F(“SSD1306 allocation failed”));
    for(;;);
  }
  display.display();
  delay(2000);
  display.clearDisplay();
}

void loop() {
  //Relay operation and led’s
  if
    (“C” < 20 ){
    digitalWrite(redLed,HIGH);
    digitalWrite(greenLed,LOW);
    digitalWrite(RLY1,LOW);
  }
 if   (“C” > 25)
  {
        digitalWrite(redLed,LOW);
        digitalWrite(greenLed,HIGH);
        digitalWrite(RLY1,HIGH);
        }
  // Read temperature as Celsius
  float temperature = dht.readTemperature();

  // Check if any reads failed and exit early (to try again)
  if (isnan(temperature)) {
    Serial.println(F(“Failed to read from DHT sensor!”));
    return;
  }

  // Display temperature on OLED
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, 0);
  display.print(F(“Temp: “));
  display.print(temperature);
  display.print(F(” C”));
  display.display();

//  Print temperature to Serial Monitor
  Serial.print(F(“Temperature: “));
  Serial.print(temperature);
  Serial.println(F(” C”));

  // Wait a few seconds between measurements
  delay(2000);
}

Question Tags: Fan Control
4 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 2 months ago

Hi.
The first thing in the loop should be reading the temperature.
After having the temperature compare it with your values.
Don’t use “C”, use your actual temperature variable.
Compare with float values like 25.0 and not just 25.
 
I hope this help.s
Regards,
Sar

0 Vote Up Vote Down
Larry answered 2 months ago

Thanks. I believe I killed the post because I resolved my issue, thanks for the great advice though 

0 Vote Up Vote Down
Larry answered 2 months ago

Thanks. I believe I killed the post because I resolved my issue, thanks for the great advice though 

0 Vote Up Vote Down
Sara Santos Staff answered 2 months ago

Ok.
Great.
I’ll mark this issue as resolved.
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.