Need help programming a photocell/relay shield

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?

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

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

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.

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.

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.

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

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

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.

#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.

#define ldrPin A0

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

void loop(){
  Serial.print("A0 = ");
  Serial.println(analogRead(ldrPin));
  delay(500);
}

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:

/*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.

Try

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.

irishluck:
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

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

irishluck:
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.

/*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");
    }
  }
}

You Riva are just awesome!

I uploaded it and its works. I covered the photocell and as soon as it loses light the LED turns on and the relay switches on. Stays for roughly 20 seconds and then turns off.

Glad it works.

I really appreciate the help.

Ive got a little more work to do to get this project done.
Ill post pictures of the final project when im done with it.

What part of the code do I change again that would adjust the time if I want to?

irishluck:
What part of the code do I change again that would adjust the time if I want to?

#define relayDelay 20UL           // Relay delay time in seconds

To ensure the maths work properly keep the UL on the end of the number or you could change the line to

const unsigned long relayDelay = 20          // Relay delay time in seconds

to make it a bit simpler.

Awesome, thank you again.

Would anyone know how many amps the arduino board actually draws?
And would adding a shield on top draw more amps?