Control SSR based on voltage present at digital pin

Complete noob...I know this is ridiculously simple, but I have searched for similar projects and can't seem to figure out what I am doing wrong.

I am trying to control an SSR with an arduino. I want the SSR to come on when there is no voltage present at a particular pin on the arduino and go off when 5v is present at that pin.

This is the sketch I wrote...where am I going wrong?

int SSR = 13; // SSR connected to pin 13 int TST = 12; // 5V input connected to pin 12

void setup() {

pinMode(SSR, OUTPUT);

digitalWrite(SSR, LOW); //Verify SSR is turned off

}

void loop() {

if (TST=HIGH ) { digitalWrite(SSR, LOW);}

else { digitalWrite(SSR, HIGH); }

}

nesseiht: Complete noob...I know this is ridiculously simple, but I have searched for similar projects and can't seem to figure out what I am doing wrong.

I am trying to control an SSR with an arduino. I want the SSR to come on when there is no voltage present at a particular pin on the arduino and go off when 5v is present at that pin.

This is the sketch I wrote...where am I going wrong?

int SSR = 13; // SSR connected to pin 13 int TST = 12; // 5V input connected to pin 12

void setup() {

pinMode(SSR, OUTPUT);

digitalWrite(SSR, LOW); //Verify SSR is turned off

}

void loop() {

if (TST=HIGH ) { digitalWrite(SSR, LOW);}

else { digitalWrite(SSR, HIGH); }

}

You defined TST as an integer with the value of 12, and then you set TST to HIGH (0x01).

try this:

void loop() {
  
  if (digitalRead(TST)==HIGH)  { // read the value on pin (TST) compare it to HIGH
    digitalWrite(SSR, LOW);
    }
  else {
      digitalWrite(SSR, HIGH);
      }
  
  }

chuck

Thanks so much Chuck. That works...as soon as "TST" gets the 5v input, "SSR" goes LOW. Then it takes about 10-15 seconds once the 5v input at TST is removed for SSR to go back to HIGH, and it "ramps" up over a period of 5-8 seconds. With a multimeter, you can see SSR climb back up till it reaches 5v.

Is there a way to get it to work without the delay?

I think we need to see a circuit. Hand drawn and photographed will do.

Weedpharma

Circuit

nesseiht:
Circuit

Here is a circuit with a transistor switch, Your SSR might need more power to energize it than the Arduino can provide.

pin 13 is driven low to energize the SSR

pin 12 is grounded to activate the switch.

here is code to use this circuit:

#define SSRPin 13
#define SSRON LOW    // my circuit turns the SSR on when pin 13 is low,
#define SSROFF HIGH
#define SWITCHPin 12

void setup(){
// when the Arduino exits Reset, all I/O pins are in INPUT mode
// While an I/O pin is in INPUT mode, if you digitalWrite(pin,HIGH), you turn on a ~50k pullup resistor on that pin.

digitalWrite(SSRPin, SSROFF);  // default the SSR to off 
digitalWrite(SWITCHPin,HIGH); // enable the pullup resistor

pinMode(SSRPin,OUTPUT); 

}

void loop(){
if(digitalRead(SWITCHPin)==LOW) { // switch is activate, so turn on SSR
   digitalWrite(SSRPin,SSRON);
  }
else digitalWrite(SSRPin,SSROFF);
}

Now this code is not very good, when a mechanical switch closes, it has a habit of bouncing.

This means that a single closure of a switch could be seen as a series of ON/OFF translations.

A mechanical switch could oscillate for up to 50ms, hundreds of times.

A better switch read code is:

bool switchState=HIGH;
byte debounceCount=0;

void loop(){

if(digitalRead(SWITCHPin)==LOW){
  if(switchState==HIGH)){ // switch is closed but not yet valid(close for timeout period)
    debounceCount++;
    if(debounceCount>50){ // switch has been held close for 50 loops 
      switchState = LOW;  // change switch State to active
      digitalWrite(SSRPin,SSRON); // turn on SSR
      }
    }
  }
else { // SWITCHPin is HIGH, switch is open, shutdown SSR, Reset debounce Count
  if(switchState==LOW) { // SSR was on, turn it off
    digitalWrite(SSRPin,SSROFF);
    }
  debounceCount = 0;
  switchState=HIGH; //  switch was released, reset to off state.
  }
}

Another way using the Millisecond counter instead of counting iterations of a loop.

bool switchState=HIGH;
unsigned long debounceTimeOut=millis();

void loop(){

if(digitalRead(SWITCHPin)==LOW){
  if(switchState==HIGH)){ // switch is closed but not yet valid(close for timeout period)
    
    if(debounceTimeOut+250<millis()){ // switch has been held close for 1/4 second 
      switchState = LOW;  // change switch State to active
      digitalWrite(SSRPin,SSRON); // turn on SSR
      }
    }
  }
else { // SWITCHPin is HIGH, switch is open, shutdown SSR, Reset debounce Count
  if(switchState==LOW) { // SSR was on, turn it off
    digitalWrite(SSRPin,SSROFF);
    }
  debounceTimeOut=millis(); // restart debounce timeout
  switchState=HIGH; //  switch was released, reset to off state.
  }
}

Chuck.

1) Avoid pin 13. It's the onboard LED pin. It could change state during bootup, and your SSR could fire randomly.

2) Don't connect a voltage (power) source directly to an input pin. It could phantom-power the Arduino when you forget to turn the Arduino on, and damage the pin. Use a 10k resistor between switch and pin. Add a 100n cap from pin to ground for hardware debouncing.

3) Don't leave an input pin "floating". It could have "any" digital state (your off delay). In your diagram the pin is "floating" when the switch is off. Use a high value (>=100k) resistor from pin to ground.

AFAIK SSRs can be directly driven by an Arduino pin. Post the link to the SSR. Leo..

Thank you very much Chuck and Leo. The SSR I am using is here: SSR

I realize I probably should have bought one with a better data sheet...this one came with nothing except what is printed on the top of the SSR. I can't seem to find the current requirement for the input.

I will report the results when I get the capacitor and resistors to follow your advice.

nesseiht: Thank you very much Chuck and Leo. The SSR I am using is here: SSR

I realize I probably should have bought one with a better data sheet...this one came with nothing except what is printed on the top of the SSR. I can't seem to find the current requirement for the input.

I will report the results when I get the capacitor and resistors to follow your advice.

Based on this DataSheet it only needs 7.5mA to drive it, so my Transistor circuit is unnecessary.

Now the $1E6 question, does the actual SSR match it's Markings? or is it counterfeit?

If this datasheet is the correct one, that SSR can switch at 120hz. It is either On or Off, your device or meter may not be fast enough to see each ON cycle. The Arduino has +-25mA of drive capability on each pin, you should be able to pulse it at each zero crossing of the AC wave. You will have to build a SYNC circuit if you need individual pulse control. But just ON/OFF should be no problem, IF the SSR matches the datasheet.

Chuck.