Led Blinking:
We will see blinking an normal dc led with arduino.
Requiremnts:
Arduino UNO Board – 1 No.
LED – 1 No.
Potentiometer – 1 No.
Breadboard – 1 No.
Below program is to blink an led with delay of 500msec of time:
- #define LED 10
- void setup()
- {
- pinMode(LED,OUTPUT);
- }
- void loop()
- {
- digitalWrite(LED,HIGH);
- delay(500);
- digitalWrite(LED,LOW);
- delay(500);
- }
In the first line declared a macro with the led pin number.
In third line function definition of setup(). setup() function is used for Initializing pins and this function will execute only once when as soon as arduino is powered up.
For more detail information about predefined functions go to my previous tutorial click here
In fifth line we initializing the led pin as output using the predefined function pinMode(). In pinMode() first argument is pin number and second argument is mode i.e., OUTPUT or INPUT. pinMode() used to initialize pin mode as either INPUT or OUTPUT.
In 8th line function definition of loop. loop is the main function like in C language. Here loop is an iteration function so led blinking is continuous until arduino powered off.
In 10th line making the led pin as high so that led will glow by using the predefined function digitalWrite(). In digitalWrite() 1st argument is pin number and 2nd argument is mode i.e., HIGH or LOW. Here switching led ON by giving second argument as HIGH in digitalWrite().
In 11th line making arduino sleep for 500msec of time by using delay predefined function. delay() function takes only one argument of time in milliseconds.
In 12th line, here making led OFF by passing 2nd argument as LOW.
In 13th line, again sleep of 500msec of time. This delay will produce blinking observation for our naked eye. If delay is not used then blinking happens but it is in fast, so then blink can’t visible to us.
LED Blinking Schematic |
Led Fading:
Led fading can happen only with pwm signal. So here using the pwm pin of digital pin’s.
There are 6 PWM pins in arduino i.e., 3,5,6,9,10,11. To these pins only fading can be done. Here fading can be adjusted with the potentiometer.
Led Fading program:
- #define LED 10
- #define POT A0
- void setup()
- {
- pinMode(LED,OUTPUT);
- }
- void loop()
- {
- analogWrite(LED,analogRead(POT)/4);
- }
In the 1st line declared a macro for led. In 2nd line declared a Potentiometer pin.
In 6th line initialized led pin as output
In 11th line, two functions are called. analogRead() function reads the analog value from the A0 which is connected with potentiometer. analogRead() return the value in the range of 0-1024 because of 10 bit ADC present in Arduino. This value is divided by 4 because analogWrite accepets the 2nd argument in the range of 0-255. This value represents the duty cycle of the pwm wave to produce on the led pin.
LED Fading |
LED Fading Schematic |