Labsheet 3 IOT
Labsheet 3 IOT
Objective: To control the brightness of LEDs using potentiometer with PWM control
List of Experiments:
1. To get analog reading from the serial monitor using potentiometer
2. To control the brightness of an LED using potentiometer
3. To control RGB LED using three potentiometers
Introduction:
1. Potentiometer:
Potentiometer is a commonly used electrical circuit that is used to manually change
oscilloscope zoom levels, brightness, volume, and other parameters. A potentiometer, often
referred to as a “pot”, is a type of variable resistor that can be used to control the flow of
electric current in a circuit. It consists of 3 terminals, two of which are connected to the ends
of the resistive element and the third is connected to a movable contact that can slide along
the resistive element. By adjusting the position of the movable contact, the resistance between
the two end terminals can be varied, allowing for the control of the current flow in the circuit.
Circuit Diagram
Circuit Explanation:
The middle pin of the potentiometer is connected to the analog input pin A0 on the Arduino.
The other pins are connected to 5V and GND respectively. The output voltage is directly
proportional to the rotated angle of the movable contact. When the movable contact is turned
all the way in one direction (nearest by GND), voltage at the analog pin is reading 0V, which
corresponds to 0 input level. When the movable contact is turned all the way in other direction
(nearest by 5V), voltage at the analog pin is reading 5V, which corresponds to 1023 input level.
The analog input levels then map to a range from 0 to 255.
Programming Code:
// C++ code
int pot = 0;
void setup()
{
pinMode(A0, INPUT);
Serial.begin(9600);
}
void loop()
{
pot = analogRead(A0); // Read input value on analog pin
pot = map(pot, 0, 1023, 0, 255); // The result is mapped to range 0-255
Serial.println(pot); // print result on the serial monitor
delay(15); // Wait for 15 millisecond(s)
}}
Results:
As the movable contact of the potentiometer is turned, the output value is printed on the serial
monitor.
Circuit Diagram
Circuit Explanation:
The middle pin of the potentiometer is connected to the analog input pin (A0) on the Arduino.
The other pins are connected to 5V and GND respectively. The output voltage is directly
proportional to the rotated angle of the movable contact. When the movable contact is turned
all the way in one direction (nearest by GND), voltage at the analog pin is reading 0V, which
corresponds to 0 input level. When the movable contact is turned all the way in other direction
(nearest by 5V), voltage at the analog pin is reading 5V, which corresponds to 1023 input level.
An LED is taken and its Cathode is grounded while anode is connected to digital pin 9 through
a 220 ohm resistor. The analog input levels then map to a range from 0 to 255. After mapping,
this value is utilized to adjust the brightness of an LED connected to digital pin 9 using pulse
width modulation (PWM).
Programming Code:
// C++ code
int pot = 0;
int out = 0;
void setup()
{
pinMode(A0, INPUT);
pinMode(9, OUTPUT);
}
void loop()
{
pot = analogRead(A0);
pot = map(pot, 0, 1023, 0, 255);
analogWrite(9, pot);
delay(15); // Wait for 15 millisecond(s)
}
Results:
As the movable contact of the potentiometer is turned, the brightness of the LED changes
respectively.
Hardware Required: Arduino board, two LEDs, two resistors (220 ohm),
potentiometer, connecting wires, Bread board.
Circuit Diagram
Circuit Explanation:
The middle pin of the two potentiometers are connected to the analog input pins (A0,A1) on
the Arduino. The other pins are connected to 5V and GND respectively. The output voltage is
directly proportional to the rotated angle of the movable contact. When the movable contact is
turned all the way in one direction (nearest by GND), voltage at the analog pin is reading 0V,
which corresponds to 0 input level. When the movable contact is turned all the way in other
direction (nearest by 5V), voltage at the analog pin is reading 5V, which corresponds to 1023
input level. Two LEDs are taken and their Cathode is grounded while anode is connected to
digital pin 9 and 10 through 220 ohm resistors. The analog input levels then map to a range
from 0 to 255. After mapping, this value is utilized to adjust the brightness of each LED
connected to digital pin 9 and 10 using pulse width modulation (PWM).
Programming Code:
// C++ code
int pot1 = 0;
int pot2 = 0;
void setup()
{
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
}
void loop()
{
pot1 = analogRead(A0);
pot2 = analogRead(A1);
pot1 = map(pot1, 0, 1023, 0, 255);
pot2 = map(pot2, 0, 1023, 0, 255);
analogWrite(9, pot1);
analogWrite(10, pot2);
delay(15); // Wait for 15 millisecond(s)
}
Results:
As the movable contact of the each potentiometer is turned, the brightness of the each
Led changes respectively.
Circuit Diagram:
Circuit Explanation:
The middle pin of the Three potentiometers are connected to the analog input pins (A0,A1,A2)
on the Arduino. The other pins are connected to 5V and GND respectively. The output voltage
is directly proportional to the rotated angle of the movable contact. When the movable contact
is turned all the way in one direction (nearest by GND), voltage at the analog pin is reading 0V,
which corresponds to 0 input level. When the movable contact is turned all the way in other
direction (nearest by 5V), voltage at the analog pin is reading 5V, which corresponds to 1023
input level. Three Leds are taken and their Cathode is grounded while anode is connected to
digital pin 9 , 10 and 11 through 220 ohm resistors. The analog input levels then map to a range
from 0 to 255. After mapping, this value is utilized to adjust the brightness of each LED
connected to digital pin 9 , 10 and 11 using pulse width modulation (PWM).
Programming Code:
// C++ code
int pot1 = 0;
int pot2 = 0;
int pot3 = 0;
void setup()
{
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
}
void loop()
{
pot1 = analogRead(A0);
pot2 = analogRead(A1);
pot3 = analogRead(A2);
pot1 = map(pot1, 0, 1023, 0, 255);
pot2 = map(pot2, 0, 1023, 0, 255);
pot3 = map(pot3, 0, 1023, 0, 255);
analogWrite(9, pot1);
analogWrite(10, pot2);
analogWrite(11, pot3);
delay(15); // Wait for 15 millisecond(s)
}
Results:
As the movable contact of the each potentiometer is turned, the brightness of the each
Led changes respectively.