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.
Sensor + Relay + RTC + Display - Arduino Code
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- hydrophyte
- Posts: 67
- Joined: Tue Mar 23, 2021 12:52 am
- dastels
- Posts: 15674
- Joined: Tue Oct 20, 2015 3:22 pm
Re: Sensor + Relay + RTC + Display - Arduino Code
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:
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
Code: Select all
digitalWrite(6, humidity > threshold);
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
- hydrophyte
- Posts: 67
- Joined: Tue Mar 23, 2021 12:52 am
Re: Sensor + Relay + RTC + Display - Arduino Code
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:.....That way you have a full history and can step back if things get broken.Code: Select all
digitalWrite(6, humidity > threshold);
Dave
Wow. Thanks a million. It's difficult to get a clear explanation like this. I will try it.
- dastels
- Posts: 15674
- Joined: Tue Oct 20, 2015 3:22 pm
Re: Sensor + Relay + RTC + Display - Arduino Code
Come back with any questions.
Dave
Dave
- hydrophyte
- Posts: 67
- Joined: Tue Mar 23, 2021 12:52 am
Re: Sensor + Relay + RTC + Display - Arduino Code
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);
}
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
Reason: changed quote tags to code tags
- dastels
- Posts: 15674
- Joined: Tue Oct 20, 2015 3:22 pm
Re: Sensor + Relay + RTC + Display - Arduino Code
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
If that doesn't do it, then it's likely a hardware problem.
Dave
You should set the pin to OUTPUT in setup() with
Code: Select all
pinMode(6, OUTPUT);
Dave
- hydrophyte
- Posts: 67
- Joined: Tue Mar 23, 2021 12:52 am
Re: Sensor + Relay + RTC + Display - Arduino Code
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.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() withIf that doesn't do it, then it's likely a hardware problem.Code: Select all
pinMode(6, OUTPUT);
Dave
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);
}
- hydrophyte
- Posts: 67
- Joined: Tue Mar 23, 2021 12:52 am
Re: Sensor + Relay + RTC + Display - Arduino Code
Update: I got it! It works. I moved... ...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.
Code: Select all
pinMode(12, OUTPUT);
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.
- dastels
- Posts: 15674
- Joined: Tue Oct 20, 2015 3:22 pm
Re: Sensor + Relay + RTC + Display - Arduino Code
Have you read the sparkfun guide for that RTC? https://learn.sparkfun.com/tutorials/re ... okup-guide
Dave
Dave
- hydrophyte
- Posts: 67
- Joined: Tue Mar 23, 2021 12:52 am
Re: Sensor + Relay + RTC + Display - Arduino Code
Well I got this to control the relay and also print RH and temp to the serial monitor with a timestamp.
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:
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);
}
}
- dastels
- Posts: 15674
- Joined: Tue Oct 20, 2015 3:22 pm
Re: Sensor + Relay + RTC + Display - Arduino Code
You can extract the hours and minutes from the DateTime object.
From RTCLib.h:
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; }
- hydrophyte
- Posts: 67
- Joined: Tue Mar 23, 2021 12:52 am
Re: Sensor + Relay + RTC + Display - Arduino Code
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?
- dastels
- Posts: 15674
- Joined: Tue Oct 20, 2015 3:22 pm
Re: Sensor + Relay + RTC + Display - Arduino Code
Uncheck "Show timestamp" in the Serial Monitor.
Dave
Dave
- hydrophyte
- Posts: 67
- Joined: Tue Mar 23, 2021 12:52 am
Re: Sensor + Relay + RTC + Display - Arduino Code
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...
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");
}
- dastels
- Posts: 15674
- Joined: Tue Oct 20, 2015 3:22 pm
Re: Sensor + Relay + RTC + Display - Arduino Code
Yes. Your syntax is incorrect.
Maybe:
Dave
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");
}
Please be positive and constructive with your questions and comments.