New to Arduino - Relay and 74HC595

Hello everybody,

Just a couple of weeks ago I bought my first arduino with the intention of making a temperature controller for my house. Reading through the various forums and the internet, I was able to piece together some programs that work very well in isolation. Like I've the DS18B20 reading out temperature on the screen, the ds1307 telling me the time and the 74hc595 turning on leds and relays at each of its 8 output.

However when I combine the temperature program with the 74HC595 I run into a problem. I'm trying to turn on some of the leds/relay connected to the 8 pins of the 74hc595 depending on my "temp" variable value. So let say if the variable "Temp" was more than a certain value "x" turn on pins Q5-Q6-Q7 on the 74hc595. IF not, then turn on Q0-Q-4. Now the problem: Once I have arduino do the math, it turns on the q5-q6-q7 when temp>x, however it get a pulse and not a continuous signal at the pins (leds light up and die out). Is there someway to hold the pins HIGH so its easy to attach a relay that would drive a fan.

I'm using a modified verison of Tom Igoes code on shiftout.

----------------------------------------------------------------
//Pin connected to ST_CP of 74HC595
int latchPin = 9;
//Pin connected to SH_CP of 74HC595
int clockPin = 13;
////Pin connected to DS of 74HC595
int dataPin = 8;
int temp=30; //<== I plan to read this value from the DS18B20.

void setup() {
  //set pins to output because they are addressed in the main loop
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);  
  pinMode(clockPin, OUTPUT);
   
}

void loop() {
  if (temp>21)
  
  {lightLED1();
    
}
else{
  lightLED2();  
}
 
void lightLED1(){
    digitalWrite(latchPin, LOW);  
  shiftOut(dataPin, clockPin, LSBFIRST, B01100000);
  digitalWrite(latchPin, HIGH);   

}
void lightLED2(){
    digitalWrite(latchPin, LOW);  
  shiftOut(dataPin, clockPin, LSBFIRST, B00000011);    
  digitalWrite(latchPin, HIGH);   

}
-----------------

Any help/suggestions would be welcome.

Thanks
Ritzy

Welcome in arduino land,

please modify your post, select the code and press the # button => it will look better :slight_smile:

Refactored your code a bit, so you can use + and - to set the temperature..

//Pin connected to ST_CP of 74HC595
int latchPin = 9;
//Pin connected to SH_CP of 74HC595
int clockPin = 13;
//Pin connected to DS of 74HC595
int dataPin = 8;
int temp=30;           //<== I plan to read this value from the DS18B20.

void setup() 
{
  Serial.begin(115200);
  Serial.println("Start temp monitor 0.1");

  //set pins to output because they are addressed in the main loop
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT); 
  pinMode(clockPin, OUTPUT);
}

void loop() 
{
  if (Serial.available() > 0)
  {
    char c = Serial.read();
    if (c == '+') temp++;
    if (c == '-') temp--;
    Serial.println(temp, DEC);
  }
  if (temp > 21)
  {
    lightLED(B01100000);
  }
  else if (temp < 20)
  {
    lightLED(B00000011); 
  }
}

void lightLED(uint8_t val)
{
  digitalWrite(latchPin, LOW); 
  shiftOut(dataPin, clockPin, LSBFIRST, val);
  digitalWrite(latchPin, HIGH);   
}

thanks Rob,

I tried the code. It does work, however i still get flicker at the output. The LEDs light up and then go off. I'm trying to hold the pins on the 74HC595 high till the temp goes down a certain value. For example, if the temperature is 33 i would want the fan on till I get it down to say 30.

Do i have to use a capacitor to hold the pin HIGH ?

thanks
Ritzy

At the moment, you are writing out to the 74HC595 each time through loop. Would it not be better to only write when you need the output to change?

yes, but how do i do that ? :slight_smile:

I just want the pin to keep the values they have and change only when required.

i'm sorry im not too good at programming, but any help would be useful.

I just want the pin to keep the values they have and change only when required.

Isn't that what happens? I thought the shift registers held their setting unless a new setting was shifted in.

Try this. I've added a variable to compare the temperature this time through loop() to the last time, and only write to the 74HC595 if they are different.

//Pin connected to ST_CP of 74HC595
int latchPin = 9;
//Pin connected to SH_CP of 74HC595
int clockPin = 13;
//Pin connected to DS of 74HC595
int dataPin = 8;
int temp=30;           //<== I plan to read this value from the DS18B20.
int previousTemp = 0;  // Use to check if temperature has changed.

void setup() 
{
  Serial.begin(115200);
  Serial.println("Start temp monitor 0.1");

  //set pins to output because they are addressed in the main loop
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT); 
  pinMode(clockPin, OUTPUT);
}

void loop() 
{
  if (Serial.available() > 0)
  {
    char c = Serial.read();
    if (c == '+') temp++;
    if (c == '-') temp--;
    Serial.println(temp, DEC);
  }
  if ((temp > 21) && (temp != previousTemp))
  {
    lightLED(B01100000);
  }
  else if ((temp < 20) && (temp != previousTemp))
  {
    lightLED(B00000011); 
  }
  previousTemp = temp;
}

void lightLED(uint8_t val)
{
  digitalWrite(latchPin, LOW); 
  shiftOut(dataPin, clockPin, LSBFIRST, val);
  digitalWrite(latchPin, HIGH);   
}

Seeing that you are a beginner, can I point out that the tutorial on the shift register has a capacitor placed on the latch pin.
On no account do this, connecting a capacitor like this can damage your arduino. Instead put the capacitor from power to ground.
This is an error in the tutorial that they will not fix for some reason.