Help: Using LDR to control brightness LED


I want to use 3 LDR’s to change a LED to 3 different levels of brightness (low, medium, bright). What happens now is that the LED is off but when I ‘activate’ one of the LDR’s, the LED turns ON to full power instead of the corrisponding brightness. My code is as follows:

int sensorPin = A0;
int sensorPin1 = A1;
int sensorPin2 = A2;
int led = 11;
int sensorValue = 0;
int sensorValue1 = 0;
int sensorValue2 = 0;

void setup() {
pinMode(led, OUTPUT);
pinMode(sensorPin, INPUT);
pinMode(sensorPin1, INPUT);
pinMode(sensorPin2, INPUT);

void loop () {

sensorValue = analogRead(sensorPin);
sensorValue1 = analogRead(sensorPin1);
sensorValue2 = analogRead(sensorPin2);

if (sensorValue<500)
{digitalWrite(led, 50);}

if (sensorValue1<500)
{digitalWrite(led, 150);}

if (sensorValue2<500)
{digitalWrite(led, 255);}



Thanks in advance.

You need to use analogWrite and not digitalWrite for all your writes to the LED.

 {digitalWrite(led, 50);}

 {analogWrite(led, 50);}

Yep! I just found it! beginners fail :wink: