Dimming light from two different outputs, not giving the same results problem.

Hello!

I want to dim some leds by using a photoresistor.

This photoresistor is dimming a PWMpin which is connected via three shift registers which in total have 21 different leds that are being dimmed.

This is working fine.

I then wanted to include one more led but i'm out of outputs on my shift registers. I then thought I could use one of the remaining digital outputs on my arduino which has a PWM function and dim that one aswell.

Problem is that the led on this digital output behaves different and opposite of the other ones.
Why is this happening?

I've listet a shot snap of my code below.

I'm using a 470 ohm resistor with the photoresistor. I've read that 10K is optimal but this i what i had.

int ledjorun = 3;
int HourButtonPin=5;
int PWMPin = 9;
int lightPin = 0;  //define a pin for Photo resistor
const byte switchPinminute = 2;
const byte switchPinhour   = 4;


pinMode(ledjorun, OUTPUT);
void setup()
{

  pinMode(ledjorun, OUTPUT);

  pinMode(PWMPin, OUTPUT); 
  Serial.begin(19200);
  

}
void loop(void)
{ 
  analogWrite(PWMPin, analogRead(lightPin)/4); //enable dimming via potentiometer or photoresistor*/
  
delay(20); 

analogWrite(ledjorun, analogRead(lightPin)/2);
  delay (200);

Try connecting the new LED to 5v through the resistor to your output pin.
Or if that's what you're already doing.
Try connecting the new LED to GND through the resistor to your output pin.

Problem is that the led on this digital output behaves different and opposite of the other ones.

Massive clue there.

What does the schematic look like?

KenF:
Try connecting the new LED to 5v through the resistor to your output pin.
Or if that's what you're already doing.
Try connecting the new LED to GND through the resistor to your output pin.

wow that worked!

Im currently using LED to 5V through the resistor to my output pin.

Just tried to do the LED to GND through the resistor to my output pin and it's working!

I have to say i have no idea why tho :slight_smile:

Thanks alot

I have to say i have no idea why tho

Think about which way the current flows during the two parts of the PWM cycle.

Leaving the LED as is, if you would like to reverse it once more, try this:

analogWrite(PWMPin, 255 - (analogRead(lightPin)/4));

dlloyd:
Leaving the LED as is, if you would like to reverse it once more, try this:

analogWrite(PWMPin, (255 - analogRead(lightPin))/4);

Just.. perfect!! Saved me alot of time, now i won't have to resolder. Thank you very much!

Edited my reply .. did a small tweak to the code.