Momentary micro switch press to start code

In my project I am going to use a momentary press of a micro switch to run my code.

I am using an 2N2222 transistor as a switch to achieve this. I have the wiring and the code configured so that when pin 7 is high pin 6 outputs to the base of the transistor, closing the switch. For some reason this isn't working.

I am using a 12v power supply

Any help is appreciated,
Thanks!

void setup()

{
pinMode(6, OUTPUT); //set enable pin as output
pinMode(7, INPUT); //set pin as input
}

void loop() //check if pin 7 is HIGH, if so turn on pin 6

 {   
 if(digitalRead(7)==HIGH) // if HIGH 
 
{
digitalWrite(6, HIGH); // close transistor switch
delay(5000); //count for 5 seconds
digitalWrite(6, LOW); //open transistor switch
}

 }

Sorry my mistake. Pin 7 is the input and 6 is the output. When is press the switch nothing happens. 0V output from pin 6. Does pin 7 need to be constantly HIGH to exectue the code after the if statement?

I hope your real circuit is different from that Fritzing picture.

No base current resistor, no kickback diode across the motor, and a motor?? on the 5volt supply.
That will eventually kill the transistor and/or the Arduino.
Leo..

I have a 1k resistor on the base pin. Im just running a 6v motor right now to test the code. I am just using the 5v to send a HIGH to pin 7 when the button is pressed, it is not running the motor.

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Forget the fritzy picture please.
Can you post a picture of your project so we can see your component layout.

On your protoboard have you checked if the side rails you ar using for power supply, conduct for the full lengh of the board,some have a brrak halfway down the board, so it is not a continuous conductor.

Do you have a DMM to measure some voltages.

Can you try this code please.

byte buttonInPin = 7;
byte motorOutPin = 6;

void setup()
{
  pinMode(motorOutPin, OUTPUT);             //set enable pin as output
  pinMode(buttonInPin, INPUT);              //set pin as input
  digitalWrite(motorOutPin, LOW);           //set motor to off
}

void loop()
{
  if (digitalRead(buttonInPin) == HIGH)     // if button pressed
  {
    digitalWrite(motorOutPin, HIGH);        // close transistor switch
    delay(5000);                            //count for 5 seconds
    digitalWrite(motorOutPin, LOW);         //open transistor switch
  }
  else
  {
    digitalWrite(motorOutPin, LOW);
  }
}

Thanks.. Tom.. :slight_smile:

Attached is an autocad drawing of the circuit. I will try that code when I get home.

Thanks

The motor has no kickback diode across.
The transistor is used as emitter follower.
The base resistor is too high.
The switch should be between pin and ground, with internal pull up enabled.
Basically most of that picture is wrong.
Leo..

Hi,

Wawa:
The switch should be between pin and ground, with internal pull up enabled.

Leave switch and resistor where they are, that's how the code is written.
Sorry but just cos you have access to internal pullup, does not mean you HAVE to use it.
Tom.... :slight_smile:

Tom,

I tried your code and it still didn't work. As suggested by Wawa I used a 220 ohm base resistor and wired the motor to the collector and the emitter to ground. I also added a flyback diode, still no luck. I pulled out my dmm and am reading 3v from pin 7 to gnd when the switch is pressed, yet no output from pin 6 to the transistor base? Is 5v needed at the pin for it to read HIGH?

TomGeorge:
Hi,Leave switch and resistor where they are, that's how the code is written.
Sorry but just cos you have access to internal pullup, does not mean you HAVE to use it.

Sure. I said "should". The sooner you discover the convenience of the internal pull up resistors the better.
Could also be safer to not have power wires running to a switch.

Tom's sketch should work.
Must be a wiring error, or the transistor is already blown from previous motor kickback.
Post a real picture of your setup.
Leo..

Hi,
Put 2 x 22k resisitors in parallel to pullup the input, or 1 x 10K resistor.

What is your button?
If you use a piece of wire to switch instead of your button what happens?

Tom.. :slight_smile: