Drive separate leds with two pole switch

Hi, im having problems with swithing two different leds based on switch position. When switched on left side, the left led blinks no problem, but when switched to right, both leds blink. What could be the cause? Many thanks:)

// Declare the pins for the Button and the LED<br>
int buttonPinR = 12;
int buttonPinL = 13;
int transistorR = 2;
int transistorL = 3;


void setup() {
   // Define pin #12 as input and activate the internal pull-up resistor
   pinMode(buttonPinL, INPUT_PULLUP);
   pinMode(buttonPinR, INPUT_PULLUP);

   // Define pin #13 as output, for the LED
   pinMode(transistorR, OUTPUT);
   pinMode(transistorL, OUTPUT);
}

void loop(){
   // Read the value of the input. It can either be 1 or 0
   int buttonValueL = digitalRead(buttonPinL);
   if (buttonValueL == LOW){
      // If button pushed, turn LED on
      digitalWrite(transistorL,HIGH);
      delay(500);
      digitalWrite(transistorL,LOW);
      delay(500);
   } else {
      // Otherwise, turn the LED off
      digitalWrite(transistorL, LOW);
   }
   // Read the value of the input. It can either be 1 or 0
   int buttonValueR = digitalRead(buttonPinR);
   if (buttonValueR == LOW){
      // If button pushed, turn LED on
      digitalWrite(transistorR,HIGH);
      delay(500);
      digitalWrite(transistorR,LOW);
      delay(500);
   } else {
      // Otherwise, turn the LED off
      digitalWrite(transistorR, LOW);
   }
}

Hi lecka,

welcome to the arduino-forum.
Very good first posting with posting code as a code-section.

The code itself should work as expected. So maybe the reason is the what you called a "two-pole-switch".

I don't have a tinkercad useracount. And I never will register because I don't like the aggressive marketing of autodesk trying to spread everywhere with not the best software

There is another simulator that works very good.

If you make a handdrawn schematic how you have things wired together and post a picture of it I think your problem can be solved

best regards Stefan

I wanted to use wokwi, but didnt find transistors, so used tinkercad, but totally understand:)
Here's the schematics

Hello mklecka
Check the wiring of the so called two pole switch.
In the first step you can use wires to simulate the switch function easily.
Have a nice day and enjoy coding in C++.

Hi leka,

oh my god what a mess of a circuit. This is a good example of how you could draw a riddle that must be solved for an escape-room-game

Except for the pull-downresistors of the MOSFET-transistors does this fit to your wiring?

In real life you need the pulldownresistors otherwise you might have problems to switch the MOS-FETs of.
And in real life your LEDs need a current-limiting resistor which is calculated as shown in the picture. For 10 mA = 0,01A current

As the transistors are just used as switches you could leave them out in the simulator or replace them with relays

best regards Stefan

Here is the code you have written

attention in real life this would damage your arduino!

and a circuit made with WOKWI that has no transistors and no current-limit resistors

In real life you need the current-limiting resistors.

best regards Stefan

@StefanL38 I am very happy when wokwi gets used and promoted.

As you say, it works very well. I use it very heavily. It is very much IMO the front runner.

It does, as the OP found, not have a strong analog section.

It has resistors, however, so I advise anyone (talking to myself because I don't always ) to just take the extra minute needed and put the current limiting resistor on in there, then you don't have to explain and remind noobs why there aren't. Any current limiting resistors.

a7

Sounds like Pin 13 is reading LOW even when not grounded. I suspect that the built-in LED on Pin 13 is causing the pin to read LOW even when set for INPUT_PULLUP and not grounded. That shouldn't be a problem on a genuine UNO because it has a buffer, but may be a problem on clones or other Arduino models.

What exactly would damage my arduino? Thanks

red, green, yellow LEDs have a forward-voltage of 1,7V to 2V.
Blue LEDs have a forward-voltage of 2,5V to 4V similar with white LEDs.
standard LEDs have a maximum current of 20 mA.

As soon as you go a little bit above the forward-voltage the current increases fast to really high values. 100 mA or more. And this too high current drawn out of the IO-pin will damage the IO-pin or even more parts of the microcontroller. And most LEDs can't stand a higher current over a longer time.

The maximum current you can draw from an Arduino Uno-IO-pin is 40 mA. With other microcontrollers it is even less than that. ESP8266 / ESP32 12 mA.

This is the reason why you need a current-limiting resistor in series with the LED if you want to supply the LED from a voltage higher than the forward-voltage.

The resistance of LEDs is highly non-linear. You have to calculate the resistor this way
(supply_voltage - forward_voltage) / current
example:

supply_voltage: 5,0V
forward_voltage 1,8V
current: 10 mA = 0,01A
resistor: (5,0V - 1,8V) / 0,01A = 320 Ohm

supply_voltage: 3,3V
forward_voltage 1,8V
current: 10 mA = 0,01A
resistor: (3,3V - 1,8V) / 0,01A = 150 Ohm

supply_voltage: 12V
forward_voltage 1,8V
current: 10 mA = 0,01A
resistor: (12V - 1,8V) / 0,01A = 1020 Ohm

some super-bright LEDs are so bright that you need a much lower current
otherwise looking into the LED will blind you that you will have a bright dot in your eyefield for some minutes. Almost like if you have looked into the sun directly.
This LED has a really outstanding performance

3mm diameter but 15.000 mcd at 20 mA! A standard LED is between 80 and 500 mcd.

I tested these LEDs how low the current can be while beeing still good visible: 0,3 mA
This means current-limiting resistor to get 0,3 mA from 5V is

(5 - 1,9) /0,0003 = 10.333 Ohm = 10 kOhm !

best regards Stefan

Ok, I understand. My plan is to have the transistors in my original scheme control external 12v source which will power 12v led strip. As I understand, in this instance I shouldn't need current limiting resistors anywhere because the led strips should have built in resistors. And only need the pull down resistors for the mosfets. Is that right?

Yes this is right
best regards Stefan

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.