Mini Relay 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.
Locked
User avatar
hydrophyte
 
Posts: 67
Joined: Tue Mar 23, 2021 12:52 am

Mini Relay Code

Post by hydrophyte »

This question is from an older discussion that is now locked....

viewtopic.php?t=195223&start=30

I deleted a couple lines of the rtc code and got it to keep time correctly, but I still can't get the mini relay to work.

Here's the latest version. I'm certain the relay is wired correctly because I tried a simplified sketch with pinMode(12, OUTPUT); and got it to work...

Code: Select all

#include <RTClib.h>

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

RTC_DS1307 rtc;

int hours = 0;
int minutes = 0;
int seconds = 0;

Adafruit_HTU21DF htu = Adafruit_HTU21DF();

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library for ST7735
#include <Adafruit_ST7789.h> // Hardware-specific library for ST7789
#include <SPI.h>

#define TFT_CS        10
#define TFT_RST        9 // Or set to -1 and connect to Arduino RESET pin
#define TFT_DC         8

Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);

void setup() {
  Serial.begin(9600);
  pinMode(12, OUTPUT);
  if (!htu.begin()) {
  Serial.println("Couldn't find sensor!");
  while (1);
  }
  
  tft.init(135, 240);  
  tft.fillScreen(ST77XX_BLACK); 
  tft.setTextColor(ST77XX_CYAN);  // set text color to green and black background
  tft.setTextSize(2);  
  tft.setRotation(3);   

 /* rtc.begin(); */
  
  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() {
  tft.fillScreen(ST77XX_BLACK); 
  float temp = htu.readTemperature();
  float rel_hum = htu.readHumidity();

  tft.setCursor(15, 15);
  tft.print("Temp: "); 
  tft.print(temp); 
  tft.print(" C");
    
  tft.setCursor(15, 45);
  tft.print("RH: "); 
  tft.print(rel_hum); 
  tft.print(" %");
 
  {
    
DateTime now = rtc.now();
    
  tft.setCursor(15, 75);
  tft.print("Time: "); 
  tft.print(now.hour(), DEC);
  tft.print(':');
  tft.print(now.minute(), DEC);
  tft.print(':');
  tft.print(now.second(), DEC);
  }
  
if (rel_hum >=80) {
  digitalWrite(12, HIGH);
  tft.setCursor(15, 105);
  tft.print("Vent:");
  tft.setTextColor(ST77XX_RED);
  tft.setCursor(87, 105);
  tft.print("ON"); 
} 

else if (rel_hum <=60) {
  digitalWrite(12, LOW);
  tft.setCursor(15, 105);
  tft.print("Vent:");
  tft.setCursor(87, 105);
  tft.setTextColor(ST77XX_YELLOW);
  tft.print("OFF");
}

 tft.setTextColor(ST77XX_CYAN); delay(5000);
}

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Mini Relay Code

Post by adafruit_support_bill »

Looks like you are using an Arduino UNO clone. Pin 12 is the MISO pin on the SPI bus. And you are using the SPI bus for your display, so the system will set that pin to INPUT mode.

You should choose a different pin for your relay control.

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

Re: Mini Relay Code

Post by hydrophyte »

Wow thanks a million. I wondered if the problem was the pin choice.

I'm using a Metro Mini. Looks like 12 is the MISO pin...

https://learn.adafruit.com/adafruit-metro-mini/pinouts

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Mini Relay Code

Post by adafruit_support_bill »

Yes. The Metro Mini is another ATMEGA328-based board. So you will need to avoid the SPI pins 11-13 if you are using SPI devices.

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

Re: Mini Relay Code

Post by hydrophyte »

adafruit_support_bill wrote: Thu Mar 09, 2023 8:34 pm Yes. The Metro Mini is another ATMEGA328-based board. So you will need to avoid the SPI pins 11-13 if you are using SPI devices.
Thanks again for helping me with this. I got a video up with the whole project...

https://www.youtube.com/watch?v=FWFufKJcUEk

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Mini Relay Code

Post by adafruit_support_bill »

Nice looking project! Thanks for sharing the video.

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

Re: Mini Relay Code

Post by hydrophyte »

adafruit_support_bill wrote: Wed Mar 29, 2023 7:25 pm Nice looking project! Thanks for sharing the video.
Thanks for watching! There's a second one up too also with microcontroller parts and a stepper driver....

https://www.youtube.com/watch?v=jQN_jVAZ6wo&t=12s

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

Return to “Arduino”