2222a transistor as a switch

Hello,

I am trying to use an Attiny bare chip and a 2222a transistor as a switch to open and close a 12vdc 100ma solenoid valve. The attiny is running on 5v regulated power. The 2222a switches on to provide 12vdc to the valve when the at tiny pin 0 goes high.

Sometimes the solenoid valve will turn on as prescribed, sometimes it will chatter off and on, and sometimes it will turn on and draw power but it seems like it is not getting enough power to engage the solenoid.

Should I be using a diode or something to clamp flyback? the transistor seems to work fine as a switch until the valve is hooked up to the circuit but I can not get it to reliably switch off and on the solenoid valve.

Thank you, Jacob

Draw a schematic and photograph it with a cell phone and post it

Always use a fly back diode on a DC coil relay/solonoid.
Show your code.
Schematic, YES!
What is the coil resistance?

in theory: when the momentary button is pressed the attiny85 sends a signal to the 2222a, the transistor grounds the solenoid valve to turn it on.

What is happening: When the momentary button is pressed sometimes the solenoid valve is engaged, sometimes the valve clicks rapidly, and sometimes the valve receives current but it does not seem to be enough current to engage the valve.

the LM7805 is a 5vdc regulator

const int SwitchPin = 2;     // the number of the switch pin
const int RelayPin =  0;      // the number of the Relay pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the Relay pin as an output:
  pinMode(RelayPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(SwitchPin, INPUT);     
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(SwitchPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:    
    digitalWrite(RelayPin, HIGH); 
    delay(5000); 
  } 
  else {
    // turn LED off:
    digitalWrite(RelayPin, LOW); 
  }
}

Thank you again... please let me know if I left out anything pertinent

the resistance on the valve coil is 113 ohms

thanks again!

Your Arduino to 2N2222, base connection needs a resistor, try 220R.
Debounce the switch.
Add 100nF capacitors to the input and output of the 7805. (Near the regulators pins.)

Edit: add 1N4007 across the solenoid (cathode to +12V).

Thank you Thank you Thank you! it works now but just for informing myself.... what does the diode across the valve coil do?

GOOD QUESTION !
It's called a FLYBACK DIODE and it shunts the BACK EMF negative voltage) from the coil when it de-energizes.

Google solenoid back emf