151 IOT Practical2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Name : Shubham Waikar

Roll No. : 151


IOT Assignment 2

analogWrite()
To operate different devices with Arduino there are different functions
available that can be used to program the microcontroller. We can call such
functions as input and output functions as they play a crucial role in
controlling the devices attached to the Arduino board. One of these
functions is the AnalogWrite() function and we have discussed the
functionality of the function briefly in this guide.

From the function name we can assume that it writes some value, and this
value will be in the range of 0 to 255. In other words, we can say that this
function is mainly used to control any analog devices attached to the
Arduino by assigning value to the analog pin of Arduino to which that
respective device is attached.
The range 0 to 255 is the duty cycle of the square wave generated for the
analog devices or in other words we can say that the resolution for
analogWrite() function is 8 bits.
Pin: The digital pin number of Arduino on which the device is connected.
Value: The value that is to be assigned to the pin of Arduino either HIGH or
LOW.

syntax:
analogWrite(led,value);

1. To interface RGB LED with Arduino and write a program to blink RGB LED in red, green
and blue color in the interval of 1 seconds.

Code:
void setup()
{
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
}

void loop()
{
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
digitalWrite(12, HIGH);
delay(1000);
digitalWrite(12, LOW);

digitalWrite(11, HIGH);
delay(1000);
digitalWrite(11, LOW);
}

Output:
2. To interface RGB LED with Arduino and write a program to blink RGB LED in different
colors in the interval of 1 seconds.
Code:
int red_light_pin= 13;
int green_light_pin = 11;
int blue_light_pin = 12;
void setup() {
pinMode(red_light_pin, OUTPUT);
pinMode(green_light_pin, OUTPUT);
pinMode(blue_light_pin, OUTPUT);
}
void loop() {
RGB_color(255, 0, 0); // Red
delay(1000);
RGB_color(0, 255, 0); // Green
delay(1000);
RGB_color(0, 0, 255); // Blue
delay(1000);
RGB_color(255, 255, 125); // Raspberry
delay(1000);
RGB_color(0, 255, 255); // Cyan
delay(1000);
RGB_color(255, 0, 255); // Magenta
delay(1000);
RGB_color(255, 255, 0); // Yellow
delay(1000);
RGB_color(255, 255, 255); // White
delay(1000);
}
void RGB_color(int red_light_value, int green_light_value, int blue_light_value) {
analogWrite(red_light_pin, red_light_value);
analogWrite(green_light_pin, green_light_value);
analogWrite(blue_light_pin, blue_light_value);
}
Output:
3. To interface 5 LEDs with Arduino and write a program to blink 5 LEDs, one at a time, in
a back-and-forth direction.

Code:
void setup()
{
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);

void loop()
{ for(int i=13;i>=9;i--)
{
digitalWrite(i, HIGH);
delay(500); // Wait for 1000 millisecond(s)
digitalWrite(i, LOW);
delay(500);
// Wait for 1000 millisecond(s)
}
for(int i=9;i<=13;i++)
{ digitalWrite(i, HIGH);
delay(500); // Wait for 1000 millisecond(s)
digitalWrite(i, LOW);
delay(500);
}

}
Output:

You might also like