Pages: [1]   Go Down
Author Topic: Problem with relay shield or some mistake with code?  (Read 638 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi to all!

I'm a newbie and i have a problem with my little project... i try to switch on a relay by an if condition!
I think that i have connected my relay shield correctly because i've tested the Blink example to try the switching power on and off.
This little program read temperature with a dallas sensor and if the temperature is high than 40°C the switch must be off otherwise is must be on.
The problem is that my pin 5 never go in HIGH condition...the if condition i think that works well because if the temperature is under 40°C my serial monitor print "a" (so the procedure is well)
I've also noticed a strange thing: if i join the else conditon (switch off the power) the shield work like blink example so i've deduced that my shield works well an the if condition is good too!

Why the state HIGH and LOW doesn't work if they are separated?? any idea?

Thanks to all

i post the code

#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2

OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

void setup(void)
{
  pinMode(5, OUTPUT);
  // start serial port
  Serial.begin(9600);
 
 sensors.begin();
}

void loop(void)
{
  // call sensors.requestTemperatures() to issue a global temperature
  // request to all devices on the bus
   sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.print("Temperature for the device 1 is: ");
  Serial.println(sensors.getTempCByIndex(0));
//my if condition
 if(sensors.getTempCByIndex(0)<=40)
{
digitalWrite(5, HIGH);
Serial.println("a");
}
else
{
digitalWrite(5, LOW);
Serial.println("b ");
}
}
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8108
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The code looks OK.  What values are displayed for "Temperature for the device 1 is: "?
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The code looks OK.  What values are displayed for "Temperature for the device 1 is: "?

Hi John,
The values i think that are correct...initially i test my room temperature, and the values are about 21°C .

The serial monitor show me this:
Temperature for the device 1 is: 21.37
a
Temperature for the device 1 is: 21.44
a
So the code as you say is correct...now i dont understand if i write the if condition like that my switch magically work well.. its strange isn't it?

Working code (like blink example):

 if(sensors.getTempCByIndex(0)<=40)
{
digitalWrite(5, HIGH);
Serial.println("a");
delay(1000);
digitalWrite(5, LOW);
Serial.println("b ");
}

Thanks John
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

mmm this is very strange.....i think that i miss something..... the relay state must be on when the temperature is under 30°C. in this case to activate the relay n°1 (plugged with pin 5) i think that i must to put in HIGH state the pin but doesn't work! if i put the pin state LOW the relay work perfectly!!! i cant understand that...someone could explain me why??
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8108
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Many relay shields are "Active Low" such that a LOW signal activates the relay. That way a light pull-up resistor can be used to de-activate the relay when the processor resets and turn all pins to inputs.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks John!! you've been really helpful!
Logged

Pages: [1]   Go Up
Jump to: