Problem somewhere in the system while trying to control LEDs with Arduino Micro

Hello everyone,

I had a long detailed message about my problem but it was all wiped out due to a little too large image.

So here's the short story:

Arduino Micro controlling LED's. LED's should only lit up once button is kept pressed for set amount of time. LED's lit up over set time to set brightness. LED's shut down immediately after button is pressed again for set (shorter) time.

Problem:
LED lits up to near full brightness once power is turned on and first button press will only make LED even brighter over set time. Second press of button will make LED dim back to original brightness. LED never is shut down.

I'm a fresh electrician and have no clue or understanding on coding. This code was written to me by a pro almost a year ago and they have no interest in my peril anymore. I've only resently progressed in my project into needing this code to work.

I hope someone sees/finds something I've missed.

Thank you!

Parts I've used (images are rather soddy so thought this might help):
PN2222
L7805CV
C 0,1mF 2x
R 1k ohm
R 150 ohm (for each 5 LED's) forgot to write these into the picture.

Also the image has cut out a bit: (Button - & LED+ from PWR (LED has 150 ohm R each)).

P.S.
I tried to look for the correct section for this initial posting of mine, I'm terribly sorry if it's completely bonkers and all over the place. Please move it if deemed necessary.

int LED = 3;
int button = 4;
int brightness = 0;
int maxBrightness = 255;

long buttonTimer = 0;
long buttonTime = 1000;

boolean buttonActive = false;
boolean longPressActive = false;

boolean LedActive = false;

void setup() {
  // put your setup code here, to run once:
  pinMode(LED, OUTPUT);
  pinMode(button, INPUT_PULLUP);
}

void loop() {
  // put your main code here, to run repeatedly:
  if(digitalRead(button) == LOW){
    if(buttonActive == false){
      Serial.println("Button pressed");
      buttonActive = true;
      buttonTimer = millis();
      if(LedActive == true){
        LedActive = !LedActive;
        digitalWrite(LED, LedActive);
        brightness = 0;
      }
    }
  }else{
    buttonActive = false;
    longPressActive = false;
    buttonTimer = 0;
  }

  if((buttonActive == true) && (millis() - buttonTimer > buttonTime) && (longPressActive == false)){
    longPressActive = true;
    Serial.println("Longpress active");

    LedActive = !LedActive;
  }

  if(LedActive == true){
    if(brightness < maxBrightness){
      analogWrite(LED, brightness);

      brightness = brightness + 5;

      delay(60);
    }
  }
}

Arduino 2 small.png

Arduino 2 small.png

I had a long detailed message about my problem but it was all wiped out due to a little too large image.

Normally the message is written only the images are not posted, so you can go to your post and it will be there but just not the images. Alternatively when you write a long post it is saved every so often and put in your draft message folder. Access that by clicking on your own avatar and the on show drafts.

The top image is a bit unnecessary but the bottom one is very useful, if a bit hard to follow, mainly due to the power triangles pointing in the wrong direction and also not using the right symbol for a switch however from a hardware point of view I can’t see anything wrong appart from using just 150R when driving an LED from 5V. That is way too low and will put too much current through the LED.

I am not sure from your explanation what it supposed to do. You should set the initial brightness in the setup function after you set the pin mode with an analogWrite call.

Connect a single led and series resistor between the pwm pin and ground, disconnecting the transistor. Does that led behave as you wanted?

Grumpy_Mike:
Normally the message is written only the images are not posted, so you can go to your post and it will be there but just not the images. Alternatively when you write a long post it is saved every so often and put in your draft message folder. Access that by clicking on your own avatar and the on show drafts.

The top image is a bit unnecessary but the bottom one is very useful, if a bit hard to follow, mainly due to the power triangles pointing in the wrong direction and also not using the right symbol for a switch however from a hardware point of view I can’t see anything wrong appart from using just 150R when driving an LED from 5V. That is way too low and will put too much current through the LED.

I am not sure from your explanation what it supposed to do. You should set the initial brightness in the setup function after you set the pin mode with an analogWrite call.

Saved Drafts is empty, I received a 404 like (not 404, instead error was in all letters) when sending and that was the end of that post. Can not find it from anywhere and unlike this message even, I saw no Draft last saved: timestamps when I wrote it.
LED's draw power directly from 12,6V battery and are controlled by 5V Arduino Micro.
What should happen:

Power on -> LED's are off -> Continued press of button (say three seconds) -> LED's light up by adding brightness from 0 up to set brightness X during set amount of time Y -> LED's burn on the set brightness until a bit shorter continued press of button -> LED's shut down immediately.
What happens at the moment:
LED's burn on quite high brightness straight away when power is put on, button press makes them even brighter and the only way to make LED stop emitting light is to kill the power.
Hope this makes more sense.

PaulRB:
Connect a single led and series resistor between the pwm pin and ground, disconnecting the transistor. Does that led behave as you wanted?

This fixed the issue right away! Is something wrong somewhere then? Should I have that transistor or just rip it off? Not entirely sure why it has been added in if it all works well and dandy without it?

I mean surely the said pro should know what they were doing while mapping that board out to me right?

wandaii:
This fixed the issue right away! Is something wrong somewhere then? Should I have that transistor or just rip it off? Not entirely sure why it has been added in if it all works well and dandy without it?

The arduino pin can power 1 led from 5V. You need to power 5 leds from 12V, those voltages and currents would damage the Arduino if connected directly. You need a transistor.

But what this test showed is that your sketch is not the reason that the leds never goes off. The problem is the transistor part of the circuit. The way you drew it in the schematic looks fine, so I think you must have done something incorrectly or the transistor is damaged. Can you post some bright, clear photos?

Other concerns I have:

You described the transistor as PW2222, which is a hi-fi speaker, according to Google. Did you mean 2N2222 or PN2222?

150R seems too low with normal 5mm leds and 12V supply. Please post a link to the spec of your leds.

With your current circuit, no external voltage regulator is needed. The Micro's on-board regulator will be sufficient. If you later add more components drawing current at 5V, then the on-board regulator might no longer be sufficent, and could overload and shut down or be damaged.

With 12V supply, there are more efficient ways to connect your 5 leds, connecting some of them in serial, and requiring less current.

PaulRB:
The arduino pin can power 1 led from 5V. You need to power 5 leds from 12V, those voltages and currents would damage the Arduino if connected directly. You need a transistor.

But what this test showed is that your sketch is not the reason that the leds never goes off. The problem is the transistor part of the circuit. The way you drew it in the schematic looks fine, so I think you must have done something incorrectly or the transistor is damaged. Can you post some bright, clear photos?

Goes to show I do not really know Arduino that well. I have no idea how or why, but now everything works fluently. I guess something was not connected properly etc. As now when I connected everyhting back to original 5 LED state and ran it (mostly for giggles really) they light up and cut off as they were supposed to.

Well nearly.

Lights went off immediately, they should require one full second long press of button before LED's light cut off.

Where in the code should this be added in order for it to happen?

PaulRB:
Other concerns I have:

You described the transistor as PW2222, which is a hi-fi speaker, according to Google. Did you mean 2N2222 or PN2222?

150R seems too low with normal 5mm leds and 12V supply. Please post a link to the spec of your leds.

With your current circuit, no external voltage regulator is needed. The Micro's on-board regulator will be sufficient. If you later add more components drawing current at 5V, then the on-board regulator might no longer be sufficent, and could overload and shut down or be damaged.

With 12V supply, there are more efficient ways to connect your 5 leds, connecting some of them in serial, and requiring less current.

That W is a typo, it is PN2222. My bad that one.
As I wrote earlier in this post, 5 red LED's worked perfectly with one 150R and 12,6V power.
I managed to fry two Arduino Micros with my 12,6V and my local components dealer also checked and verified that new Micro chips has components in them that can handle power only up to somewhere that I do not recall but was way less than even 12V. But in my rig I power LED's straight from 12,6V and Arduino via 5V pin.

Do these more efficient ways allow me to set up time for how long button needs to be pressed in order for LED power up to set brightness in set time and also for the LED's to cut off after the said button is pressed for another set time?

And that is about the size of it.

It is best to regard the on-board regulator on the UNO/ Nano/ Pro Mini/ (Pro) Micro as essentially useless, due to the lack of any adequate heat-sinking and the possibility that the regulator fitted is not rated for more than 12 V and a "12 V" supply may not actually be well controlled and may be much more (particularly in a car!).

So as a rule, always operate the Arduino from a (regulated) 5 V supply via the "5V" pin or with limitations, the USB connector. That will keep you out of trouble.

Now, what about these "fried" Micros? Do they still operate from 5 V? Have you checked their current draw, and if it is unreasonable, have you tried removing the regulator?

I'm confused as to what your circuit is now, and how much you do or do not care are about abusing components. There are ways to make your leds more efficient, meaning to use less current and fewer resistors. They will not alow or prevent you from changing your code to work the way you want. So if that is not of interest, forget I mentioned it.

Paul__B:
And that is about the size of it.

It is best to regard the on-board regulator on the UNO/ Nano/ Pro Mini/ (Pro) Micro as essentially useless, due to the lack of any adequate heat-sinking and the possibility that the regulator fitted is not rated for more than 12 V and a "12 V" supply may not actually be well controlled and may be much more (particularly in a car!).

So as a rule, always operate the Arduino from a (regulated) 5 V supply via the "5V" pin or with limitations, the USB connector. That will keep you out of trouble.

Now, what about these "fried" Micros? Do they still operate from 5 V? Have you checked their current draw, and if it is unreasonable, have you tried removing the regulator?

Those first two Micros do no longer respond to anything and PC does not recognize anything when they're plugged into PC.

PaulRB:
I'm confused as to what your circuit is now, and how much you do or do not care are about abusing components. There are ways to make your leds more efficient, meaning to use less current and fewer resistors. They will not alow or prevent you from changing your code to work the way you want. So if that is not of interest, forget I mentioned it.

I'm currently running my original circuit and it works all dandy with a set of five LED's and a 150R ohm. Precisely as in my schematic in my first post. And obviously I wish to operate as fluently and efficiently. I just have hard time understanding what these alternatives are in practice, these you've mentioned so far, sir. Feel free to enlighten me.

Ok then. With your 12V supply, you can connect several leds in series. Perhaps not all 5, depending on your leds, but maybe a group of 3 in series and the remaining group of 2 also in series. Each group of leds in series can share the same current, and needs only one series resistor. So, assuming you are using ordinary red 5mm leds and 20mA current per led, instead of having all 5 connected in parallel, requiring 5 series resistors and consuming a total of 100mA, you can arrange them in two series groups, requiring only 2 series resistors and a total of only 40mA current.

Here is an array wizard which should help. Lets say your leds are white and have a forward voltage of 3.2V. You key in 12V, 3.2V, 20mA, 5 and see what the wizard suggests.

Right, so I found this (datasheet of my LEDs):

So if I understand correctly, the info I key in is: 12,6V, 2,4V, 20mA, 10 (amount of total LEDs per build, 5 per each lens).

Results are either pairs of four (82R) and six (1R) or five (47R) and five (47R) series of LEDs. By the R results 150R seems to be unnecessarily big for resistors paired with five said LEDs, as a mere 47R would do.

Assuming I read the data correctly.

I am very suspicious as to what your "12.6 V battery" actually is. If it is a car battery, then 12.6 V is a wild approximation as the charge voltage would be more like 13.8 V. In fact, any rechargeable battery would have a significantly higher charge voltage.

While it may be tempting to think that LEDs specified at 2.5 V maximum drop could be used in a group of four in series at 9.6 V, it would be much safer to use groups of three and a larger resistance.

It actually and precisely is a hand made Li-ion battery made out of 16 li-ion uh... bars? With such values printed on it: 12,6V 23,5Ah (296Wh) Out max 2,5A. Made by Ecakit Oy over here in Finland, they have mere 33 years experience on custom battery manufacturing etc. Purpose built battery if I may add.

According to the data sheet, your leds could have forward voltage between 1.8V and 2.4V, which is a wide range. For safety, you should assume the lowest limit of the range when calculating your series resistors, not the highest. If you assume the lowest voltage, then if the actual led is higher, less current will flow, making the led dimmer but safe. If you assume the highest voltage, then if the actual led is lower, more current will flow, possibly damaging or shortening the life of the led.

The data sheet also seems to imply that the leds are sold as "F02", "F03" or "F04" variants with much narrower ranges of forward voltage. It might be worth measuring your 10 leds to see what their actual forward voltage is, and use the lowest figure from those.

Only problem with that is that it encourages the use of more LEDs in series, and then a smaller resistor value which gives less tolerance for variation in the supply voltage.

Yes, what the led array calculator suggests is correct in theory but in practice you sometimes need to discount some of its solutions. With some of them, modest variations in supply voltage and forward voltage of leds as their temperature changes can result in quite large changes in current.

I suggest using the calculator with the full range of supply voltages you might expect, and the range of forward voltages you measure in your leds. Obviously this will result in different resistor valyes, but if one of the solutions dissapears completely at the extremes, maybe give that one a miss.