regarding relay with transistor

from the attachment i provide, did i do any mistake on the circuit if i run with arduino… will the solenoid work if the push button is reading HIGH… if my circuit or the code have mistake, please show me where it is… and here my coding (taken directly from debounce example)

// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin = 2;    // the number of the pushbutton pin
const int ledPin = 13;      // the number of the LED pin

// Variables will change:
int ledState = HIGH;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);

  // set initial LED state
  digitalWrite(ledPin, ledState);
}

void loop() {
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);

  // check to see if you just pressed the button 
  // (i.e. the input went from LOW to HIGH),  and you've waited 
  // long enough since the last press to ignore any noise:  

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  } 
  
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        ledState = !ledState;
      }
    }
  }
  
  // set the LED:
  digitalWrite(ledPin, ledState);

  // save the reading.  Next time through the loop,
  // it'll be the lastButtonState:
  lastButtonState = reading;
}

I think that black wire from the top-most NC pin of the relay to ground is dangerous! With the relay switched as it's shown, trace the wires thru the battery: you'll see that in that pos, the battery + is shorted to ground thru no load and boom!

That wire must come out. Rest looks ok to me, with the usual E&OE, YMMV, Ts & Cs apply, disclaimer.

(And assuming the codes good....)

I think that black wire from the top-most NC pin of the relay to ground is dangerous! With the relay switched as it's shown, trace the wires thru the battery: you'll see that in that pos, the battery + is shorted to ground thru no load and boom!

so i should put a 10k resistor between the NC pin and ground?

crytosis:
so i should put a 10k resistor between the NC pin and ground?

Put nothing, nada, zip, zilch.
As JimboZA wrote, take out the black wire and replace it with nothing.

UnoDueTre:

crytosis:
so i should put a 10k resistor between the NC pin and ground?

Put nothing, nada, zip, zilch.
As JimboZA wrote, take out the black wire and replace it with nothing.

so it will look like this… i will test them later… thx for all the help :slight_smile:

Yep.

Only time you would wire up to the NC side of the relay is if you had something that should be running normally. Another solenoid maybe, needing to be in under normal circumstances, then when you push the button and the relay switches, the first solenoid goes out and your original one comes in.

Yes that is fine.
You can also simplify the circuit slightly by enabling the internal pull_up resistor on the input pin then simply wire your switch to ground.
Don’t forget to also change any logic in your sketch as that input will now go low when the switch is pressed as opposed to high.

I was going to suggest that too, since the prevailing wisdom seems to be to go active low.

JimboZA:
I was going to suggest that too, since the prevailing wisdom seems to be to go active low.

Personally prefer "positive" logic but since the internal resistors exist, why not use them, cheaper than external ones :slight_smile: , especially if one has lots of inputs, plus of course the extra wiring required or waste of pcb real estate to accommodate the extra resistors plus the tracks leading to them.
After reading the switch, one could always invert the status back to positive logic using software.

thanks for the suggestion.. it really simplify the circuit.. no more push button.. i will try learn the internal pull-up resistor.. im not doing it on pcb though.. just trying to learn around

crytosis:
.. no more push button..

You still need the button, just not the extra resistor going down to ground.

pinMode(buttonPin, INPUT_PULLUP);

Then modify "if" statements to check that the switch is low when pressed.

UnoDueTre:

JimboZA:
I was going to suggest that too, since the prevailing wisdom seems to be to go active low.

Personally prefer “positive” logic but since the internal resistors exist, why not use them, cheaper than external ones :slight_smile: , especially if one has lots of inputs, plus of course the extra wiring required or waste of pcb real estate to accommodate the extra resistors plus the tracks leading to them.
After reading the switch, one could always invert the status back to positive logic using software.

Or do it in the same statement:

results = !analogRead(pin#); // convert active low input into a active high input.

retrolefty:
Or do it in the same statement:
results = !analogRead(pin#); // convert active low input into a active high input.

results = !digitalRead(buttonPin);

UnoDueTre:

retrolefty:
Or do it in the same statement:
results = !analogRead(pin#); // convert active low input into a active high input.

results = !digitalRead(buttonPin);

Yea, that's what I meant. :smiley:

i already change the circuit layout.. instead of using motor driver, i just use a relay.. i will upload the pic and code later