1

So I made this circuit + a momentary push button on D2 and GND: enter image description here

Because I don't use a real arduino, I use the RAW port to act like the +5V port. It all worked with a single LED without the npn transistor. Now, with the transistor, the strip of LED's doesn't turn off anymore when I hit the button. The code:

 #include <SoftwareSerial.h>

  #include <Keyboard.h>

  boolean toggle = false;
  int inPin = 2;
  int outPin = 3;

  int state = HIGH;
  int ledOn;
  int reading;
  int previous = LOW;

  long time = 0;
  long debounce = 200;

  void setup() {
    // make pin 2 an input and turn on the
    // pullup resistor so it goes high unless
    // connected to ground:
    pinMode(inPin, INPUT_PULLUP);
    pinMode(outPin, OUTPUT);
    Keyboard.begin();
  }

  void loop()
  {
    reading = digitalRead(inPin);
    ledOn = digitalRead(outPin);

    // if the input just went from LOW and HIGH and we've waited long enough
    // to ignore any noise on the circuit, toggle the output pin and remember
    // the time
    if ((reading == LOW) and (ledOn == LOW)) {
      state = HIGH;
      Keyboard.press((char) 128);
      Keyboard.press('r');
      delay(50);
      Keyboard.releaseAll();
      delay(200);
    }

    if ((reading == LOW) and (ledOn == HIGH)) {
      state = LOW;
      Keyboard.press((char) 32);
      delay(10);
      Keyboard.releaseAll();
      delay(100);
      Keyboard.press((char) 177);
      delay(10);
      Keyboard.releaseAll();
      delay(200);

      time = millis();
    }

    digitalWrite(outPin, state);

    previous = reading;
  }
  • 1
    Put a resistor in between the D3 output and the base of the transistor, say 500ohms, and try it again. If it works, I'll post a proper answer explaining why. – Mark Smith Feb 18 '17 at 20:35
  • Could you just explain it in an answer so I can mark it as solved? – Hendriks3D Feb 18 '17 at 21:00
0

Looks like Eric has beaten me to it with his answer, which is more thorough than mine, but I'll explain why the base resistor works anyway.

Your code relies on reading the level of a pin set to output. It will only switch off the output if that output is already reading "high".

If you don't have a resistor on the base, then a pretty high current will flow from base to emitter. This will pull the level of the output down below the threshold for "high", even if it's being driven. (It also might blow the output, but I guess you have been lucky.)

It's unusual, even if perhaps not "wrong", to read the value of an output. I'd use a global or static variable to hold the on/off state, rather than reading it.

1

You need to adjust the value of Rc, the collector current limiting resistor. When replacing the single LED with a strip of N LEDs of the same kind, you have increased N times the current required (that's why you need to use a BJT, because the Arduino I/O pins won't supply that much current). So, you need a new value for that resistor, let's call it Rc', much lower: Rc' = Rc/N. Otherwise, the old value may limit the current far below of what the LEDs need to light up.

But that's not all. You also need to add 2 resistors:

  1. Rb: a current limiting resistor between D3 and the BJT base. Its value should be in the range 330 - 2200 Ohm. The higher the current draw of the LED strip, the lower the value of Rb should be.
  2. Rpd: a pull-down resistor between the BJT base and ground to avoid it floating while D3 is not initialised as OUTPUT. Make it at least 20x the value of Rb.

All this should be enough to make it work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.