Adding Relay when Alarm is active.

Hi Forum, David (newbie) here from Saskatchewan, Canada. Doing my first project, but hitting a wall.

I seen a simple project on You Tube and wanted to do it, but got problems when taking it further. I have the Uno and wav shield. Temp sensor and CO2 sensor. I’ve got the temp sensor and CO2 sensor fine to play audible alarm through wav shield using SD card. I’m now trying to add replay. I have searched the forum as didn’t want to waste peoples time and still got problems. So rather than give up and throw away everything away, I’m asking for help and hopefully it will encourage me to do further project I like the look of.

All I want is to do is switch my relay from a NO state to a NC state when my alarm plays. It sound simple, but driving me up the wall. A friend looked at it and he programmed pin 12. Now I have a 4 pack relay and to switch the relay state, I need a pin to ground. Now pin 12 and 13 are to ground at all times on my Uno. Pin 12 is ground, but when alarm actually plays the speech, it’s actually chattering the relay, and once speech stops, even though alarm is active it’s back to a solid ground, then when speech is playing again (delay 2000), the relay is chattering again. Once speech stops, it’s back to solid ground and alarm can stop and it’s status remains the same. I have experimented tapping other pins etc, but experienced a loud squeal after tapping VCC and I lost my SD card. The Uno, 2 laptops, camera & camcorder can’t see the SD card. Plus I used an AC adaptor ( by mistake) on CO2 sensor and it didn’t last long, split second, project was over, so bought another and would like to see this through.

So how would I go about changing a pin status to ground, to switch relay state only when alarm plays. I’ve copy and pasted a clean code as I was getting in a mess assigning pins for a high state and then a low state, reading and then writing. So the code I’ve uploaded, hasn’t got the relay info in at all. Could somebody add the code for me please as it’s driving me bonkers.

Thanks forum members and looking forward to getting to know you.

Code.txt (6.91 KB)

Hello and welcome,

Use digitalWrite to set the state of an output. Your relays are Active Low, so to turn it on:

digitalWrite( relayPin, LOW );

and off:

digitalWrite( relayPin, HIGH );

Hi Guix, thanks for this, but where in the code do I add it?

Also I've tried the LOW for ground and HIGH for iirc 3.3v, but all I'm getting is ground. I'm unsure if I got the correct code in assigning the pins for reading.

Start by getting the relay to work, with a simple sketch like:

const uint8_t relayPin = 12;

void setup()
{
}

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

Connect your relay to pin 12, and it should turn on and off every seconds. If it doesn't work, then you wired your relay wrong.

Then, you said you want to turn on your relay when the alarm sound is playing, so, something like this:

...
    if (CO >= COLevel){
      if (Temp > TempLevel){
        digitalWrite( relayPin, LOW ); // turn on relay
        playcomplete("Alarm.wav");
        Serial.println("CO2 is High");
        Serial.println("Temp is High");
        }
...

Thank you Guix, I'll give this go when I get some time after. Something that seemed simple is giving me so many problems. As for wiring on Relay unit I have the GND to GND on Uno, VCC to 5V on Uno and then the relays are numbered and each one just needs a ground to switch over, and of course my COM on relay is carrying my voltage and just acting as a switch. I appreciate your reply and will give it a go.