Lab Manual Btech - Ardiuno
Lab Manual Btech - Ardiuno
Name:
Reg. No:
Date of
Experiment:
Now write the following code to change the brightness of the LED using PWM.
Arduino Code: PWD Generation
int led_pin = 11; // Initializing LED Pin
int i;
void setup() {
pinMode(led_pin, OUTPUT); // Declare LED pin as output
}
void loop()
{
for( i=0; i<255; i++) // Fading the LED
{
analogWrite(led_pin, i);
delay(10);
}
for( i=255; i>0; i--)
{
analogWrite(led_pin, i);
delay(10);
}
}
Sketch:
Upload the code in the Arduino IDE and on moving the knob of the potentiometer, the
brightness of the LED will change.
int led_pin = 11; // Initializing LED Pin
int pot_pin = A0; // Initializing LED Pin
int data_10_bit, data_8_bit;
void setup() {
pinMode(led_pin, OUTPUT); // Declare LED pin as output
}
void loop()
{
data_10_bit = analogRead(pot_pin); // Reading from potentiometer
// Mapping the Values between 0 to 255 because we can give
// output from 0-255 using the analogwrite() funtion
data_8_bit = data_10_bit >> 2;
// data_8_bit = map(data_10_bit, 0, 1023, 0, 255);
analogWrite(led_pin, data_8_bit);
delay(10);
}