arduino dimmer

hi, the following code i m using....

const int ledPin = 12; // the pin that the LED is attached to

void setup() { // initialize the serial communication: Serial.begin(9600); // initialize the ledPin as an output: pinMode(ledPin, OUTPUT); }

void loop() { byte brightness;

// check if data has been sent from the computer: if (Serial.available()) { // read the most recent byte (which will be from 0 to 255): brightness =; // set the brightness of the LED: analogWrite(ledPin, brightness); } }

I have given the input such as 125, 225 , etc. from serial monitor but the LED on the pin 12 is always off(not responding). How can get it ?

Pin 12 is not a PWM pin.

k with a pwm pin its some what working but not going to OFF when i entered zero value, 0.

Where is the serial data coming from? If it is coming from the Serial Monitor, the value is coming in as a string, not a byte. So, 0 becomes '0', which is not the same.

serial data is coming from serial monitor

Then, you need to re-write your code to collect the whole string of characters, convert the string to a number, and use that value in the analogWrite call.

I'd suggest to try this:

ch =;

// let's assume ch is a..z
ch -= 'a';

// map a..z letters to 0..100% pwm duty cycle
perc = map(ch, 0, 25, 0, 255);

analogWrite(pin, perc)