Embedded Systems Report Lab 4
Embedded Systems Report Lab 4
Embedded Systems Report Lab 4
1
Table of Contents
INTRODUCTION.........................................................................................................................................................3
PART THAT WE WILL NEED..................................................................................................................................3
BREADBOARD LAYOUT..........................................................................................................................................4
ARDUINO SKETCH....................................................................................................................................................4
CODE EXPLANATION..............................................................................................................................................4
LED BRIGHTNESS CONTROL USING PROTEUS 8...........................................................................................5
PULSE WIDTH MODULATION CONCEPT..........................................................................................................5
THE THEORY BEHIND THE POTENTIOMETER...............................................................................................6
2
Introduction
In this lab, we will make a LED dimmer by using a potentiometer to control the brightness
of the LED. The potentiometer is like a button that sends an analog signal to the Arduino
Board. We will use PWM to manipulate the signal from the Arduino Board to the LED.
Dupont Wires ×1
10K Potentiometer ×1
Blue LED ×1
3
BreadBoard Layout
Arduino sketch
void setup() {
pinMode(LED, OUTPUT);
pinMode(AnalogInputPin,INPUT);
Serial.begin(9600);
}
void loop () {
// reading the value of the potentiometer
potValue = analogRead(AnalogInputPin);
// send a value to control brightness LED
// we need to convert the value of 0 - 1023 to 0 - 255
var=AnalogRead(AnalogInputPin);
Y=(255./1023.)*var;
Serial.println(Y);
analogWrite(LED, Y);
delay(10); // delay for 10 ms
}
Code Explanation
After you wired everything correctly we can begin with the coding part. First, we will declare
our variables.
4
The next step is to set the led as an output pin in the void setup() part of our sketch.
Now we are ready to code the loop. We assigned a variable for the readings of the
Potentiometer. To be able to read the value of the potentiometer we need to read the analog pin.
The analogRead() command converts the input voltage range, 0 to 1023, to 0 to 5 voltage. After
the reading is successful we are able to control the brightness of the LED depending on the value
that is read. An interesting part of this sketch is the way we convert the reading to a digital
number for controlling the LED.
First, we need to consider that PWM uses a range of values between 0 and 255 while
analogRead() returns a value between 0 and 1023.
To send the reading of the potentiometer to the LED, we are using the analogWrite() command.
Pulse Width Modulation, or PWM, is a technique for getting analog results with digital means.
Digital control is used to create a square wave, a signal switched between on and off. This on-off
pattern can simulate voltages in between full on (5 Volts) and off (0 Volts) by changing the
portion of the time the signal spends on versus the time that the signal spends off. The duration
of “on time” is called the pulse width. To get varying analog values, you change or modulate,
5
that pulse width. If you repeat this on-off pattern fast enough with an LED, for example, the
result is as if the signal is a steady voltage between 0 and 5v controlling the brightness of the
LED.
In the graphic below, the green lines represent a regular time period. This duration or period is
the inverse of the PWM frequency. In other words, with Arduino’s PWM frequency at about
500Hz, the green lines would measure 2 milliseconds each. A call to analogWrite() is on a scale
of 0 – 255, such that analogWrite(255) requests a 100% duty cycle (always on), and
analogWrite(127) is a 50% duty cycle (on half the time) for example:
6
volume controller of a radio transistor is a popular example of a rotary potentiometer
where the rotary knob of the potentiometer controls the supply to the amplifier.