Two of my students are trying to use a 2N2222 transistor to toggle a simple DC Motor on and off. When I connect a LED to the output that goes to the transistor base pin it lights, so I know it's sending signal. And when I connect the motor directly to the power it runs. But when I introduce the transistor it doesn't toggle the motor.
Logic Flow:
Button or limit switch pressed ---> Light an LED and turn on the motor
Neither the button or limit switch passed ---> Turn off the light and motor.
Light works, but motor dos not. Wiring and Code follows. Resistors are representative of the correct values in the circuit. The transistor is a 2N2222. I used TinkerCAD to illustrate the wiring, so it shows a 3-way switch, but in reality it's a limit switch commonly used on a 3D printer.
int LimitInputPin = 7;
int LEDOutputPin = 2;
int ButtonInputPin = 4;
int MotorOutputPin = 13;
void setup() {
pinMode(LimitInputPin, INPUT);
pinMode(LEDOutputPin, OUTPUT);
pinMode(ButtonInputPin, INPUT);
pinMode(MotorOutputPin, OUTPUT);
}
void loop() {
if ((digitalRead(LimitInputPin)==HIGH)||(digitalRead(ButtonInputPin)==HIGH)){
digitalWrite(LEDOutputPin,HIGH);
digitalWrite(MotorOutputPin,HIGH);
}
if ((digitalRead(LimitInputPin)==LOW)&&(digitalRead(ButtonInputPin)==LOW)){
digitalWrite(LEDOutputPin,LOW);
digitalWrite(MotorOutputPin, LOW);
}
if ((digitalRead(LimitInputPin)==HIGH)&&(digitalRead(ButtonInputPin)==HIGH)){
digitalWrite(MotorOutputPin,HIGH);
}
}