I have an Arduino MKR 1010 wired up with a transistor to act as a switch to control an externally powered LED. Everything works just fine. However, when the pin (7) has been set to LOW and the LED is off as expected, I can touch the resistor and the LED will turn on slightly. The light is not very bright, but I'm still baffled as to why the LED would turn on when I touch the resistor. Specifically, it happens when I touch the resistor at the point where the wire goes into the resistor. If I touch it lightly, it barely turns on, but if I press harder it glows a little bit brighter.
Why is this happening, and how do I prevent this? Is it normal for electricity to still travel through the transistor to the LED even when it's "off"?
So in the code, it waits for a message to be received via MQTT. If the message is "HIGH", it sets digitalWrite(7, HIGH);. Otherwise, if the message is "LOW", it sets digitalWrite(7, LOW);. The pin stays at that state until it receives a message telling it to switch.
Oh yeah, should have mentioned those. The transistor is a 2N2222. The base resistor is 330 Ohm. I didn't know I also needed a resistor to be in series with the LED. Would it also need to be the same value as the base resistor?
Besides the things mentioned above, LED's can light up on their own pretty easy sometimes. I've had times where just holding one of the legs in the air gives a faint glow. You might even be giving some of your static charge to the transistor, activating it.
No it's a calculated value. Typical red LRD so let's guess for want of a data sheet the LED Vforward is 1.2 volts and you want to limit the current to 15 mA. Your LED Vsupply is 9.0 volts. So we take Vsupply - LED Forward Voltage / LED Forward current15 mA 9.0 - 1.2 / .015 = 520 Ohms or as an off the shelf 560 Ohms or even 680 Ohms.
Yes, you need a series resistor to limit LED current. Very much needed in this case.
This is just a proof-of-concept so it won't be powering anything for long periods of time. I was basically just learning how to use a transistor to work as a switch for something fairly low power like the LED.
That shouldn't be happening with the output pin driven high or low. But it MIGHT happen with the I/O pin set as an input (high impedance). It might also happen if the resistor temporarily loses contact and is not connected to the Arduino.
Your body will pick-up voltage from the AC power lines all around you. If you've ever touched an audio input and heard a hum or buzz, it's amplifying that "signal".
That "signal" is very-low power, and a low impedance (like an Arduino output pin) will greatly attenuate it. But a transistor alone MIGHT be able to amplify it enough to light an LED.
You can also have a static charge on your body and that can be very -high voltage (enough to fry semiconductors) but it discharges very quickly and you probably wouldn't see the LED flash.
Is the Arduino pin set to OUTPUT.
If not, then writing a LOW to the pin makes it high impedance (easy to drive the base with a finger),
and writing a HIGH still drives the transistor, but with the weak pull up current of the pin.
Leo..