Pages: 1 [2] 3   Go Down
Author Topic: Need help programming a photocell/relay shield  (Read 6909 times)
0 Members and 1 Guest are viewing this topic.
Norfolk UK
Offline Offline
Faraday Member
**
Karma: 70
Posts: 2587
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I kind of know what it is but I dont know what its used for on this shield.
The DIP switch is S1 in the schematic you supplied and connects to pins 4 & 5 on the UNO. They do not turn on/off any of the circuits they are just for you to use as configuration/control signals as you need. As an example you could alter your code so if a switch is closed it disables the relay or they could be used to adjust the delay time to some preset values like...
00 = 5 seconds delay
01 = 10 seconds
10 = 20 seconds
11 = 40 seconds
Logged


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

Sorry I havent been back on here in a little bit. I started a new job and has been busy.
Can someone tell me what exactly COM stands for on the diagram for the relay?
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 118
Posts: 4339
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

COM = Common. 
It is the connection that is switched between the Normally Open and Normally Closed connections
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

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

Do i absolutely have to have power going to the COM for the relay to trip?
I ask this cause id like to test it but dpnt have another power source to use right now
Logged

Lacey, Washington, USA
Offline Offline
Faraday Member
**
Karma: 162
Posts: 2502
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

No, you don't need power to the relay contacts in order to test this. Although you'll want something to check that the relay contacts are moving. A meter set to Continuity or Ohms on COM and NO will show low resistance when the relay is activated. Just don't have anything else connected to COM, NC, or NO.
Logged

Steve Greenfield AE7HD
CET Consumer Electronics and Computer
Please don't read your attitudes into my messages

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

Alright ill do that tonight. Tried to test yestersay with no meter. Figured uou could hear the relay switch but maybe its just a quite one.
Logged

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

So i tried this and the relay isnt working or something.
Im getting power to my shield but the relay isnt switching when I cover up the photocell or let the light hit it.
Logged

Lacey, Washington, USA
Offline Offline
Faraday Member
**
Karma: 162
Posts: 2502
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Can you put your meter on the output pin on the Arduino?
Logged

Steve Greenfield AE7HD
CET Consumer Electronics and Computer
Please don't read your attitudes into my messages

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 118
Posts: 4339
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Or change the output to pin 13 so that you can see if the output is working.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Norfolk UK
Offline Offline
Faraday Member
**
Karma: 70
Posts: 2587
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This simple sketch should toggle the relay on/off every 2 seconds assuming it is relay 1 and the shield matches the schematic you posted. It will also toggle pin 13 (inbuilt LED) at the same time.
Code:
#define relayPin 2
#define ledPin 13

void setup(){
  pinMode(relayPin,OUTPUT);
  pinMode(ledPin,OUTPUT);
}

void loop(){
  digitalWrite(relayPin,HIGH);
  digitalWrite(ledPin,HIGH);
  delay(2000);
  digitalWrite(relayPin,LOW);
  digitalWrite(ledPin,LOW);
  delay(2000);
}

EDIT:
To find out if the LDR is working as expected and get an idea what the threshold value should be try the below sketch that prints out the LDR reading about twice per second.
Code:
#define ldrPin A0

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

void loop(){
  Serial.print("A0 = ");
  Serial.println(analogRead(ldrPin));
  delay(500);
}
« Last Edit: March 28, 2014, 03:15:13 am by Riva » Logged


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

Ya the first sketch isnt working. Everything has power to it. The LED on the arduino comes on but no lights on the shield come on.
When I cover up the photocell, nothing happens. Used the ohm meter on the relay and nothing happens. But the board has the 12v going to it and that's all I can read.

This is whats on arduino now:

Code:
/*Program for Photocell/ Relay configuration*/

#define ldrPin A0                 // Pin the light dependent resistor is connected to
#define relayPin 2                // Pin relay is connected to
#define ledPin 13                 // Pin LED is connected to

#define triggerLevel 512          // Levels below this threshold will trigger relay
#define relayDelay 20UL           // Relay delay time in seconds

unsigned long relayOffTime = 0;   // Time to turn off relay

void setup(){
  //Serial.begin(115200);
  pinMode(relayPin,OUTPUT);
  digitalWrite(ledPin,LOW);       // Turn relay off
  pinMode(ledPin,OUTPUT);
  digitalWrite(ledPin,LOW);       // Turn LED off
}

void loop(){
  if (analogRead(ldrPin) < triggerLevel){
    if (relayOffTime == 0) {      // Relay off time zero so relay is off and we need to turn it on
      digitalWrite(ledPin,HIGH);  // Turn relay on
      digitalWrite(ledPin,HIGH);  // Turn LED on
      relayOffTime = (relayDelay * 1000UL) + millis();  // Set off time
      //Serial.println("Relay On");
    }
  }
  
  if (relayOffTime != 0) {        // Relay off time not zero so relay is on and we need to check if it's time to turn it off
    if (millis() > relayOffTime){ // Time expired?
      digitalWrite(ledPin,LOW);   // Turn relay off
      digitalWrite(ledPin,LOW);   // Turn LED off
      relayOffTime = 0;           // Clear off time
      //Serial.println("Relay Off");
    }
  }
}


Also I uploaded that LED code that you mentioned in ya last post. That one did work. the LED on the shield is blinking every 2 seconds. But the main code has no LED's come on.

And I tried uploading the second part of that code you mentioned that will print something? Yea it uploaded but dont know what to do after that.
« Last Edit: March 29, 2014, 01:30:50 pm by irishluck » Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 118
Posts: 4339
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try
Code:
Serial.println(analogRead(ldrPin));
at the start of loop()

What value do you get and does it vary when you cover the ldr ?
You will need to uncomment the Serial.begin() in setup and make sure that the baud rate matches that of the serial monitor.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Norfolk UK
Offline Offline
Faraday Member
**
Karma: 70
Posts: 2587
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Also I uploaded that LED code that you mentioned in ya last post. That one did work. the LED on the shield is blinking every 2 seconds. But the main code has no LED's come on.
But was the relay also clicking on and off?

And I tried uploading the second part of that code you mentioned that will print something? Yea it uploaded but dont know what to do after that.
You need to open the serial monitor window (see below image) and you should get loads of numbers rolling by that should change by a lot when you cover the LDR. If they are not changing by much then try changing A0 to A1 in the sketch and upload again.

Can you upload a picture of the shield your using and we can see what components and where they are.


* Clipboard-1.jpg (15.41 KB, 500x132 - viewed 19 times.)
« Last Edit: March 30, 2014, 05:14:29 am by Riva » Logged


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

okay i got that window opened.
its showing A0 = and then numbers.
WIth the light, its showing from 880 to 910 give or take.
when I cover the sensor its 1017-1018

and im not sure what baud is but in the lower right hang corner it shows 9600 baud
« Last Edit: March 30, 2014, 11:39:11 pm by irishluck » Logged

Norfolk UK
Offline Offline
Faraday Member
**
Karma: 70
Posts: 2587
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

okay i got that window opened.
its showing A0 = and then numbers.
WIth the light, its showing from 880 to 910 give or take.
when I cover the sensor its 1017-1018
and im not sure what baud is but in the lower right hang corner it shows 9600 baud
Okay, it looks like the LDR is working but the range between light & covered is not great and it's logic is opposite to what the code was expecting.
I have altered the last program you posted to suit the info you supplied and after uploading it you should open the Serial monitor again. When you cover the LDR a 'Relay On' message should print and about 20 seconds later a 'Relay Off' will be printed. All we need to do now is figure out why the relay does not appear to be working.

Code:
/*Program for Photocell/ Relay configuration*/

#define ldrPin A0                 // Pin the light dependent resistor is connected to
#define relayPin 2                // Pin relay is connected to
#define ledPin 13                 // Pin LED is connected to

#define triggerLevel 950          // Levels above this threshold will trigger relay
#define relayDelay 20UL           // Relay delay time in seconds

unsigned long relayOffTime = 0;   // Time to turn off relay

void setup(){
  Serial.begin(9600);
  pinMode(relayPin,OUTPUT);
  digitalWrite(relayPin,LOW);       // Turn relay off
  pinMode(ledPin,OUTPUT);
  digitalWrite(ledPin,LOW);       // Turn LED off
}

void loop(){
  if (analogRead(ldrPin) > triggerLevel){
    if (relayOffTime == 0) {      // Relay off time zero so relay is off and we need to turn it on
      digitalWrite(relayPin,HIGH);  // Turn relay on
      digitalWrite(ledPin,HIGH);  // Turn LED on
      relayOffTime = (relayDelay * 1000UL) + millis();  // Set off time
      Serial.println("Relay On");
    }
  }
 
  if (relayOffTime != 0) {        // Relay off time not zero so relay is on and we need to check if it's time to turn it off
    if (millis() > relayOffTime){ // Time expired?
      digitalWrite(relayPin,LOW);   // Turn relay off
      digitalWrite(ledPin,LOW);   // Turn LED off
      relayOffTime = 0;           // Clear off time
      Serial.println("Relay Off");
    }
  }
}
Logged


Pages: 1 [2] 3   Go Up
Jump to: