LED Dimmer not working

Hi,
I am trying to make a simple LED dimmer. The values are taken in Serially.
Here is my code

const int analogOutPin = 9; 

int sensorValue = 0;        

void setup() {
  
  Serial.begin(9600);
}

void loop() {
  
  if (Serial.available() > 1){
  sensorValue = Serial.read();
 
  
  analogWrite(analogOutPin, sensorValue);

 
  Serial.println("output = ");
  Serial.print(sensorValue);
  
 
  delay(2);
  }
}

The LED is just not changing light intensity. I am using a 10K Resister at the LED. I even tried using the dimmer using a Potentiometer. But no significant changes in LED was seen. What could be going wrong?

Switch your Serial.print and Serial.println

What are you seeing in Serial Monitor?

What's happening is your sensor is probably returning values between 0-1023, but analogWrite only writes between 0-255.

But why use >1 for serial available?

What sensorValues do you see?

Does the [u]Fade Example[/u] work?

I am using a 10K Resister at the LED.

That's awfully high. Does the LED get bright enough?

Actually if the sensor is coming in over serial, like you’re using some wireless setup, then it would really help to know what info you’re getting. Problem should be obvious if you told us what Serial Monitor is saying you’re receiving.

Also:
What values are you expecting to read?
ASCII/binary/several characters?
10K for a simple LED series resistor is large, try 220 ohms

.

I am not receiving from any wireless device. I’m manually putting in values between 0-255 in the serial monitor.

Alright. So here is the code

const int analogOutPin = 9;

int sensorValue = 0;        

void setup() {
  
  Serial.begin(9600);
}

void loop() {
  
  if (Serial.available() > 0){
  sensorValue = Serial.read();
  
  analogWrite(analogOutPin, sensorValue);

  
  Serial.print("value = ");
  Serial.println(sensorValue);
  
    delay(2);
}
}

When I input 250 in the Serial Monitor, this is the output:

value = 50
value = 53
value = 48
value = 13
value = 10

Time to read Serial IInput Basics - updated and apply the principles. I think the first example uses a line feed to detect the end of the received data.

50 (equals 0x32 and) is the digit 2 that you typed
53 (equals 0x35 and) is the digit 5 that you typed
48 (equals 0x30 and) is the digit 0 that you typed
13 (equals 0x0D and) is the carriage return that you have configured in serial monitor.
13 (equals 0x0A and) is the linefeed that you have configured in serial monitor

OK. I am actually using an esp8266 and communicating over internet.
This is the input parameters I get from the incoming command

void callback(char* topic, byte* payload, unsigned int length)

Comparing is the PAYLOAD is ON or OFF is easy. AS simple as

if((char)payload[0] == (char)LIGHT_ON[0] && (char)payload[1] == (char)LIGHT_ON[1])
do............(further actions)

But now when values for the brightness level are being received? How do i write those values to LED using "analogWrite"??

Hi,

10K for a simple LED series resistor is large, try 220 ohms

Quoted from LarryD.

Did you try it?

Is the LED connected the correct way round.

Tom.... :slight_smile:

TomGeorge:
Hi,Quoted from LarryD.

Did you try it?

Is the LED connected the correct way round.

Tom.... :slight_smile:

Yes I did. I am using a lower ohm resister now. That is what i am thinking....the LED did not glow bright even with the Potentiometer to the least level...
But now it works fine.