Go Down

Topic: Defined pins are constantly HIGH and do not react (Read 488 times) previous topic - next topic

Enitim91

Sep 05, 2020, 07:55 pm Last Edit: Sep 06, 2020, 06:14 pm by Enitim91
Hello
My current project with an Arduino UNO is about building a kilma chamber with controlled humidity and temperature. For this purpose I am measuring the temperature and humidity with a DHT 22. With a Data Logging Shield I record the measurement. As soon as the set limit values are exceeded or undercut, either a cooling, heating or ultrasonic nebulizer should be switched on. For some reason the relays are constantly open as soon as the Arduino has power. The Arduino constantly applies 5V to all defined pins without regard to the programming.
Can anyone help me with this?
The code is also attached.

Code: [Select]

#include <SPI.h> //for the SD card module
#include <SD.h> // for the SD card
#include <Wire.h>
#include <DHT.h> // for the DHT sensor
#include <RTClib.h> // for the RTC

//define DHT pin
#define DHTPIN 2     // what pin we're connected to

// uncomment whatever type you're using
#define DHTTYPE DHT22   // DHT 22
//#define DHTTYPE DHT22   // DHT 22  (AM2302)

// initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);

// change this to match your SD shield or module;
// Arduino Ethernet shield and modules: pin 4
// Data loggin SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
const int chipSelect = 10;

// Create a file to store the data
File myFile;

// RTC
RTC_DS1307 rtc;

void setup() {
  //initializing the DHT sensor
  dht.begin();

  //initializing Serial monitor
  Serial.begin(9600);
  
  // setup for the RTC
  while(!Serial); // for Leonardo/Micro/Zero
    if(! rtc.begin()) {
      Serial.println("Couldn't find RTC");
      while (1);
    }
    else {
      // following line sets the RTC to the date & time this sketch was compiled
      rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    }
    if(! rtc.isrunning()) {
      Serial.println("RTC is NOT running!");
    }
    
  // setup for the SD card
  Serial.print("Initializing SD card...");

  if(!SD.begin(chipSelect)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
    
  //open file
  myFile=SD.open("DATA.txt", FILE_WRITE);

  // if the file opened ok, write to it:
  if (myFile) {
    Serial.println("File opened ok");
    // print the headings for our data
    myFile.println("Date, Time, Temperature C, Feuchtigkeit %");
  }
  myFile.close();
//Pin Deklarieren
pinMode(3, OUTPUT);
digitalWrite (3, HIGH);
pinMode(4, OUTPUT);
digitalWrite (4, HIGH);
pinMode(5, OUTPUT);
digitalWrite (5, HIGH);
pinMode (6,OUTPUT);
digitalWrite (6,HIGH);
}

void loggingTime() {
  DateTime now = rtc.now();
  myFile = SD.open("DATA.txt", FILE_WRITE);
  if (myFile) {
    myFile.print(now.day(), DEC);
    myFile.print('-');
    myFile.print(now.month(), DEC);
    myFile.print('-');
    myFile.print(now.year(), DEC);
    myFile.print(' ');
    myFile.print(now.hour(), DEC);
    myFile.print(':');
    myFile.print(now.minute(), DEC);
    myFile.print(':');
    myFile.print(now.second(), DEC);
    myFile.print(",");
  }
  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.println(now.day(), DEC);
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.println(now.second(), DEC);
  myFile.close();
  delay(1000);  
}

void loggingTemperature() {
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  // Read temperature as Celsius
  float t = dht.readTemperature();
  float h = dht.readHumidity();
  
  // Check if any reads failed and exit early (to try again).
  if  (isnan(t) /*|| isnan(f)*/) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  
  //debugging purposes
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.println(" °C");
  Serial.print("Feuchtigkeit: ");
  Serial.print(h);
  Serial.println(" %");
  
  myFile = SD.open("DATA.txt", FILE_WRITE);
  if (myFile) {
    Serial.println("Loggen Erfolgreich");
    myFile.print(t);
    myFile.print(",");
    myFile.print(h);
  }
  myFile.close();
}

void Regelung() {
  //Variablen definieren.
  float t = dht.readTemperature();
  float h = dht.readHumidity();
  int humidpin =3;
  int heatpin = 4;
  int coolpin = 5;
  int fanpin = 6;
  //Werte für regelung
  float humidOn = 60;
  float humidOff= 90;
  float coolOn = 25;
  float coolOff = 20;
  float heatOn = 15;
  float heatOff = 18;
  double Statushumid=0;
  double Statuscool=0;
  double Statusheat=0;
    
  if (h < humidOn) {
    digitalWrite(humidpin, LOW);
    Statushumid= 1;
    }
  else if (h > humidOff) {
    digitalWrite(humidpin, HIGH);
    Statushumid= 0;
  }
  
  if (t > coolOn) {
    digitalWrite(coolpin, LOW);
    Statuscool= 1;
    }
  else if (t < coolOff) {
    digitalWrite(coolpin, HIGH);
    Statuscool= 0;  
  }
  
  if (t < heatOn) {
    digitalWrite(heatpin, LOW);
    Statusheat= 1;
   }
  else if (t > heatOff) {
    digitalWrite(heatpin, HIGH);
    Statusheat= 0;  
  }
  
if (Statushumid + Statuscool + Statusheat >= 1.00) {
   digitalWrite(fanpin, LOW);
   Serial.println("Fentilator Ein");  
  }
else if (Statushumid + Statuscool + Statusheat < 1.00){
  digitalWrite(fanpin,HIGH);
  Serial.println("Fentilator Aus");
  }
  
  if (Statushumid >=1){
  Serial.println("Ultraschallvernebler Ein");  
    }
  else if (Statushumid < 1){
  Serial.println("Ultraschallvernebler Aus");
  }
  
    if (Statuscool >=1){
  Serial.println("Kältekompressor Ein");  
    }
  else if (Statuscool < 1){
  Serial.println("Kältekompressor Aus");
  }
  
    if (Statusheat >=1){
  Serial.println("Heizpads Ein");  
    }
  else if (Statusheat < 1){
  Serial.println("Heizpads Aus");
  }
 }
void loop() {
  Regelung();
  loggingTime();
  loggingTemperature();
  delay(5000);
}
}



Thanks a lot in advance

wildbill


Enitim91

#2
Sep 06, 2020, 06:09 pm Last Edit: Sep 06, 2020, 06:16 pm by Enitim91
Often, relay boards are active LOW.
That was probably the problem. I swapped all HIGH and LOW and now it works.

Thank you

btw: I have updated the code here if anyone can use it

Wawa

For relay boards with opto couplers (active LOW), you should write a HIGH to the pin BEFORE setting the pin to output.
That eliminates short relay activation (chatter) during bootup.

So this
pinMode(3, OUTPUT);
digitalWrite (3, HIGH);

should be
pinMode(3, INPUT_PULLUP); // first pull the pin high
pinMode(3, OUTPUT); // then set to output

or, the old way
digitalWrite (3, HIGH);
pinMode(3, OUTPUT);

Leo..


MarkT

For relay boards with opto couplers (active LOW), you should write a HIGH to the pin BEFORE setting the pin to output.
That eliminates short relay activation (chatter) during bootup.

So this
pinMode(3, OUTPUT);
digitalWrite (3, HIGH);

should be
pinMode(3, INPUT_PULLUP); // first pull the pin high
pinMode(3, OUTPUT); // then set to output

or, the old way
digitalWrite (3, HIGH);
pinMode(3, OUTPUT);

Leo..


Since pinMode/digitalWrite take only a few microseconds on even the slowest Arduino boards, the
order shouldn't matter, relays react on timescales of several milliseconds, 3 orders of magnitude slower,
and the opto-couplers are probably taking several microseconds to respond too.

Mechanical systems are sooooooooo much slower than anything electronic they can usually be treated as
stationary.  Even a jet aircraft moves a tiny fraction of a millimeter in a micosecond, and microcontrollers
can perform many instructions in that time.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Go Up