Sensor + Relay + RTC + Display - Arduino Code

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
hydrophyte
 
Posts: 67
Joined: Tue Mar 23, 2021 12:52 am

Sensor + Relay + RTC + Display - Arduino Code

Post by hydrophyte »

I'm an Arduino novice and working on a new project. I haven't gotten far beyond just copy-pasting code and this new setup requires slightly more advanced code. There are lots of similar projects out there, but I get confused looking at example sketches. I hope that I can figure out just one aspect of this at a time to finally integrate everything.

The project needs a temp & humidity sensor to collect environmental data, then display along with current time on a mini LCD screen while also switching an additional device on-off with a relay to keep humidity values (but not temperature) within a certain range. Here are the components I've assembled along with the wiring configuration, which seems OK:

Adafruit 240x135 Color TFT Breakout LCD Display

Vin to 5V (red)
GND to GND (black)
SCK to Digital 13 (orange)
MOSI to Digital 11 (green)
TFTCS to Digital 10 (yellow)
RST to Digital 9 (blue)
DC to Digital 8 but (orange)

Adafruit HTU21D-F Temperature & Humidity Sensor - 12C

RED to 5V
BLACK to GND
YELLOW to A5
BLUE to A4

Adafruit STEMMA Non-Latching Mini Relay

RED to 5V
BLACK to GND
WHITE to 6

SparkFun Real Time Clock Module - RV-8803 - I2C

RED to 5V
BLACK to GND
YELLOW to A5
BLUE to A4

I would like to first get the sensor to communicate with the serial monitor while also switching the relay to keep humidity within a range of 60-70%. Like I mentioned I've looked as several similar projects, but just haven't gotten very far figuring out the code. Any tips would be really appreciated.
electronics-I.jpg
electronics-I.jpg (725.8 KiB) Viewed 417 times

User avatar
dastels
 
Posts: 15656
Joined: Tue Oct 20, 2015 3:22 pm

Re: Sensor + Relay + RTC + Display - Arduino Code

Post by dastels »

For your first goal, in the loop() function you'll read from the sensor, Serial.print the values (which then end up in the serial monitor), and control the relay's digital pin accordingly. If you want the relay to turn on when the humidity is above a threshold value you could use something like:

Code: Select all

digitalWrite(6, humidity > threshold);
Getting and displaying the time uses a similar approach: get the time from the RTC and Serial.print it as well.

A separate issue/step is moving from output to Serial to display on the TFT.

I advise following that order:
1) get sensor data printing to Serial
2) control the relay
3) add time to the Serial output
4) convert the Serial output to TFT display

I strongly suggest using Git (and maybe GitHub, but that's not required) to manage your code, committing each time you get something new working. That way you have a full history and can step back if things get broken.

Dave

User avatar
hydrophyte
 
Posts: 67
Joined: Tue Mar 23, 2021 12:52 am

Re: Sensor + Relay + RTC + Display - Arduino Code

Post by hydrophyte »

dastels wrote: Sat Oct 15, 2022 8:52 am For your first goal, in the loop() function you'll read from the sensor, Serial.print the values (which then end up in the serial monitor), and control the relay's digital pin accordingly. If you want the relay to turn on when the humidity is above a threshold value you could use something like:

Code: Select all

digitalWrite(6, humidity > threshold);
.....That way you have a full history and can step back if things get broken.

Dave


Wow. Thanks a million. It's difficult to get a clear explanation like this. I will try it.

User avatar
dastels
 
Posts: 15656
Joined: Tue Oct 20, 2015 3:22 pm

Re: Sensor + Relay + RTC + Display - Arduino Code

Post by dastels »

Come back with any questions.

Dave

User avatar
hydrophyte
 
Posts: 67
Joined: Tue Mar 23, 2021 12:52 am

Re: Sensor + Relay + RTC + Display - Arduino Code

Post by hydrophyte »

dastels wrote: Sat Oct 15, 2022 4:37 pm Come back with any questions.

Dave
OK I'm back to this thing.

Now I'm just trying to get the relay to work. Here is a simple sketch that prints temp & humidity to the serial monitor. I've also added a line similar to what you suggested...

Code: Select all

#include <Wire.h>
#include "Adafruit_HTU21DF.h"


Adafruit_HTU21DF htu = Adafruit_HTU21DF();

void setup() {
  Serial.begin(9600);
  Serial.println("HTU21D-F test");

  if (!htu.begin()) {
    Serial.println("Couldn't find sensor!");
    while (1);
  }
}

void loop() {
    float temp = htu.readTemperature();
    float rel_hum = htu.readHumidity();
    Serial.print("Temp: "); Serial.print(temp); Serial.print(" C");
    Serial.print("\t\t");
    Serial.print("Humidity: "); Serial.print(rel_hum); Serial.println(" \%");
    delay(5000);

    digitalWrite(6, rel_hum >= 80);

    
}
This verifies OK, but doesn't do anything to the relay. Do I need to add something additional to setup? (I think) it would be nice if I could keep it this simple.

I've seen some similar projects with lines like this...

Code: Select all

if(humidity >= maxHumidity){
    powerOnRelay();
  } else if (humidity <= minHumidity) {
    powerOffRelay();
  }
 
}
void powerOnRelay() {
    digitalWrite(relayPin, HIGH);

void powerOffRelay() {
    digitalWrite(relayPin, LOW);
Last edited by dastels on Thu Nov 03, 2022 10:53 pm, edited 1 time in total.
Reason: changed quote tags to code tags

User avatar
dastels
 
Posts: 15656
Joined: Tue Oct 20, 2015 3:22 pm

Re: Sensor + Relay + RTC + Display - Arduino Code

Post by dastels »

The snippet you mention implements hystereous: if it gets too high the fan turns on and stays on until it gets too low. The way your code works, the fan is on whenever the RH is too high.

You should set the pin to OUTPUT in setup() with

Code: Select all

pinMode(6, OUTPUT); 
If that doesn't do it, then it's likely a hardware problem.

Dave

User avatar
hydrophyte
 
Posts: 67
Joined: Tue Mar 23, 2021 12:52 am

Re: Sensor + Relay + RTC + Display - Arduino Code

Post by hydrophyte »

dastels wrote: Thu Nov 03, 2022 10:57 pm The snippet you mention implements hystereous: if it gets too high the fan turns on and stays on until it gets too low. The way your code works, the fan is on whenever the RH is too high.

You should set the pin to OUTPUT in setup() with

Code: Select all

pinMode(6, OUTPUT); 
If that doesn't do it, then it's likely a hardware problem.

Dave
Yes I intended to write for a lower threshold also so that humidity could stay within a wider range. For now just trying to get the relay to work.

I tried with that update, but still with no result. Some time earlier I did get the relay to switch because the red indicator LED was turning on, but I don't remember what I was doing exactly. It is easy to raise the humidity by just holding the sensor in my hand.

Here is the updated code. I tried a different digital pin (12) to see if that might have been the issue..

Code: Select all

#include <Wire.h>
#include "Adafruit_HTU21DF.h"


Adafruit_HTU21DF htu = Adafruit_HTU21DF();

void setup() {
  Serial.begin(9600);
  Serial.println("HTU21D-F test");

  if (!htu.begin()) {
    Serial.println("Couldn't find sensor!");
    while (1);
    pinMode(12, OUTPUT); 
  }
}

void loop() {
    float temp = htu.readTemperature();
    float rel_hum = htu.readHumidity();
    Serial.print("Temp: "); Serial.print(temp); Serial.print(" C");
    Serial.print("\t\t");
    Serial.print("Humidity: "); Serial.print(rel_hum); Serial.println(" \%");
    delay(5000);

    digitalWrite(12, rel_hum >= 80);

    
}

User avatar
hydrophyte
 
Posts: 67
Joined: Tue Mar 23, 2021 12:52 am

Re: Sensor + Relay + RTC + Display - Arduino Code

Post by hydrophyte »

Update: I got it! It works. I moved...

Code: Select all

pinMode(12, OUTPUT); 
...up under the first curly brace in setup and now the relay switches.

Now I need to get current time to write the serial monitor. Any tips? The RTC is on the i2c bus along with the sensor. I have tried this already a few times, but with no luck yet.

User avatar
dastels
 
Posts: 15656
Joined: Tue Oct 20, 2015 3:22 pm

Re: Sensor + Relay + RTC + Display - Arduino Code

Post by dastels »

Have you read the sparkfun guide for that RTC? https://learn.sparkfun.com/tutorials/re ... okup-guide

Dave

User avatar
hydrophyte
 
Posts: 67
Joined: Tue Mar 23, 2021 12:52 am

Re: Sensor + Relay + RTC + Display - Arduino Code

Post by hydrophyte »

Well I got this to control the relay and also print RH and temp to the serial monitor with a timestamp.

time-stamp-rtc.jpg
time-stamp-rtc.jpg (65.23 KiB) Viewed 318 times

I got myself confused trying to set up the SparkFun rtc and actually the Stemma wire connection was kind of awkward, so I instead used a ds1307 that I had extra. I just pasted that rtc example code into my existing sketch, then cut out extra stuff.

Now I need to clean this up and figure out how to get it to get it to the TFT display. It's currently printing seconds, but I think I just want hours and minutes. How can I achieve that? Actually, for a more dynamic display (and since screen is so little) I think it might look the best with current time alternating with temp + humidity and relay status, so it would go in a sequence like this with a few seconds for each:
  • 04:19
  • Temp: 23C RH: 44%
  • Vent OFF


Code: Select all

#include <RTClib.h>

#include <Wire.h>
#include "Adafruit_HTU21DF.h"

RTC_DS1307 rtc;

Adafruit_HTU21DF htu = Adafruit_HTU21DF();

void setup() {
  Serial.begin(9600);

      pinMode(12, OUTPUT); 

  if (!htu.begin()) {
    Serial.println("Couldn't find sensor!");
    while (1);

  }


#ifndef ESP8266
  while (!Serial); // wait for serial port to connect. Needed for native USB
#endif

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    while (1) delay(10);
  }

  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running, let's set the time!");
 
  }

}


void loop() {
    float temp = htu.readTemperature();
    float rel_hum = htu.readHumidity();
    Serial.print("Temp: "); Serial.print(temp); Serial.print(" C");
    Serial.print("\t\t");
    Serial.print("Humidity: "); Serial.print(rel_hum); Serial.println(" \%");
    delay(5000);

    digitalWrite(12, rel_hum >= 80);

    


{
    DateTime now = rtc.now();



    

    Serial.println();
    delay(5000);
}
}

User avatar
dastels
 
Posts: 15656
Joined: Tue Oct 20, 2015 3:22 pm

Re: Sensor + Relay + RTC + Display - Arduino Code

Post by dastels »

You can extract the hours and minutes from the DateTime object.
From RTCLib.h:

Code: Select all

 /*!
      @brief  Return the hour
      @return Hour (0--23).
  */
  uint8_t hour() const { return hh; }
  
  /*!
      @brief  Return the minute.
      @return Minute (0--59).
  */
  uint8_t minute() const { return mm; }
  

User avatar
hydrophyte
 
Posts: 67
Joined: Tue Mar 23, 2021 12:52 am

Re: Sensor + Relay + RTC + Display - Arduino Code

Post by hydrophyte »

dastels wrote: Mon Nov 21, 2022 11:03 am You can extract the hours and minutes from the DateTime object.
From RTCLib.h:
Thanks so much. I will try that. Actually, I'll probably leave the seconds, but remove milliseconds.

Any tips for getting those three variables to serial print in sequence, and without the time stamp for RH + temp and relay?

User avatar
dastels
 
Posts: 15656
Joined: Tue Oct 20, 2015 3:22 pm

Re: Sensor + Relay + RTC + Display - Arduino Code

Post by dastels »

Uncheck "Show timestamp" in the Serial Monitor.

Dave

User avatar
hydrophyte
 
Posts: 67
Joined: Tue Mar 23, 2021 12:52 am

Re: Sensor + Relay + RTC + Display - Arduino Code

Post by hydrophyte »

dastels wrote: Mon Nov 21, 2022 5:34 pm Uncheck "Show timestamp" in the Serial Monitor.

Dave
Of course. Thanks.

I've made a little more headway. I think I want the tft to display time + RH + temp alternating w
with the relay status every few seconds just like it is printing to the serial monitor...

Untitled 2.jpg
Untitled 2.jpg (36.19 KiB) Viewed 289 times

However, I'm stuck again. I commented out the digitalWrite line and tried to replace with a if/else statement to get the relay switching between high and low RH thresholds. It worked OK with the if statement (with relay pin just staying HIGH after top threshold reached) but broke when I tried to add the else statement. Any idea what is wrong here?

Code: Select all

    //digitalWrite(12, rel_hum >= 80);

if (rel_hum >=80){
  digitalWrite(12, HIGH);
  Serial.println("Vent: ON");
}

else (rel_hum <=60);{
  digitalWrite(12, LOW);
  Serial.println("Vent: OFF");

}

User avatar
dastels
 
Posts: 15656
Joined: Tue Oct 20, 2015 3:22 pm

Re: Sensor + Relay + RTC + Display - Arduino Code

Post by dastels »

Yes. Your syntax is incorrect.

Maybe:

Code: Select all

    //digitalWrite(12, rel_hum >= 80);

if (rel_hum >=80) {
  digitalWrite(12, HIGH);
  Serial.println("Vent: ON");
} else if (rel_hum <=60) {
  digitalWrite(12, LOW);
  Serial.println("Vent: OFF");
}
Dave

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”