You are on page 1of 5

111Equation Chapter 1 Section 1Experime

nt 10: Introduction to the Arduino UNO


Microcontroller
Laboratory Objectives
The objectives of this experiment are to provide the student with an opportunity to:
1)
Be introduced to the Arduino UNO Microcontroller and Arduino programming language.
2)
Create a simple program to specify the brightness of an LED using pulse width modulation.
3)
Modify the program to control brightness using an input from a potentiometer or lightdependent resistor.

Equipment List
1)
2)
3)
4)
5)
6)

Digital Multi-Meter
Arduino UNO Microcontroller
Rotational Potentiometer
LED (Light Emitting Diode)
LDR (Light Dependant Resistor)
Breadboard JE25

7) Wire kit and screwdriver


8) Needle nose pliers and wire Stripper
9) Circuit Component Kit
10)
USB Data Acquisition Device (NI USB6008)

11) Laboratory Instructions


12)
In this experiment, you will program an Arduino UNO microcontroller using the Arduino
development environment. The power will be supplied by the USB connection to your
computer. You will wire and program the Arduino to accept input from a source (potentiometer
or LDR), and use that to control the brightness of an LED using PWM.

13) Part 1, Create an Arduino program to control LED brightness using pulsewidth modulation
1) Connect your Arduino to the computer using the USB cable.
2) Set up the Arduino with an input from the potentiometer and an output to the LED (see Fig.
1).
a.
Use the Arduinos +5V and GND pins to provide power to your potentiometer.
b.
Connect the potentiometer output to one of the Arduinos analog input pins.
c.
Use one of the PWM-capable digital pins (they have a ~ next to the pin number) to
provide power to the LED. Connect the PWM signal to an Oscilliscope so that the pulse
widths are shown.

14)
15)
Figure 1. Arduino pin locations.
3) Open the Arduino development environment.
4) Set up the basic Arduino operation loop with the following code:
16)

17)
18)
19)
20)
21)

void setup() {
}
void loop() {
}

22)

5) Set up your Arduino to accept input from the potentiometer


a.
Prior to the setup() function, declare integer variables for the analog input pin you
selected and the potentiometer value:
23)

24)
25)
26)

b.

int dialPin = A0; // analog input pin for potentiometer


double dialVal = 0; // potentiometer input value, initialized at zero

Within the loop() function (between the curly brackets), acquire the current
potentiometer output:

dialVal = analogRead(dialPin); // reads the potentiometer input pin as a value


between 0-1023
6) Test your input by printing the variable to your screen
a.
Within the setup() function, initialize the serial port:
Serial.begin(9600); // initialize the serial port at a baud rate of 9600
b.

Within the loop() function, print the current potentiometer output:


Serial.println(dialVal);
output

c.
d.

// print the value of the variable dialVal to the serial

Run the Arduino code by clicking the Upload button.


Open the serial monitor by using Ctrl+Shift+M or by clicking Tools->Serial Monitor. As
you turn the potentiometer, you should see values displayed between 0-1023. Record the
maximum and minimum values. Once you have this data, you can comment out the
Serial.println code.

7) Connect the LED light to the Arduino, using a PWM-capable pin using the diagram shown in
Fig 2.
27)

28)
29)

Figure 2. LED Circuit


30)
8) Program the Arduino to dim the LED based on the potentiometer input
a.
Prior to the setup() function, initialize the variables for the LED pin port and value:
31)

32)
33)

int ledPin = 10; // digital pin with PWM used to control the LED brightness
double ledVal = 0; // variable to store the PWM value of the LED

34)

b.

Within the setup() function, declare the selected pin to be an output (it is an input by
default):
pinMode(ledPin, OUTPUT);

c.

// specify the LED pin as an output

Within the loop() function and after the analogRead code for the potentiometer input,
convert the potentiometer input to a 0-255 range, replacing pmax and pmin with your
maximum and minimum measured potentiometer outputs:
ledVal= (dialVal-pmin)*255/(pmax-pmin); // convert dialVal to (0,255)

d.

After the dialVal conversion, use this value to set the current PWM cycle for the LED:
analogWrite(ledPin, ledVal); // sets a PWM cycle for the output from the LED pin

35)

9) Upload the code, and turn the potentiometer to dim the LED. It should be completely off at
one extreme, and at maximum brightness at the other.

36) Part 2, Convert LED brightness to an input signal using a light-dependent


resistor
1)

Measure the resistance of your LDR with the DMM in ambient lighting. Select a resistor
from your kit with a similar value.
2)
Using this resistor and the LDR, create a voltage divider circuit, shown in Fig. 3. Connect
the output to one of the analog input pins on your Arduino.
37)

38)
39) Figure 3. Voltage divider circuit
40)
41)
3)
Program the Arduino to accept input from the LDR circuit. Prior to the setup() function,
initialize the LDR pin and value variables in the same way as the potentiometer pin and value
variables.
4)
Calibrate the input from the LDR circuit using the potentiometer.
a.

Program the Arduino to output the LDR value to the serial monitor, then upload the
code.

b.

Dial the LED between maximum and minimum values using the potentiometer,
recording these values.
c.
Using the same process as with the potentiometer, convert the 0-1023 range LDR
pin input to a 0-255 range.

42)

Part 3, Use the LDR to control the brightness of the LED.

1) Prior to the setup() function, declare a new variable ledDes that will be used to specify the
desired brightness of the LED. You should initialize it with a value between 0-255.
2) Prior to the setup() function, initialize a second variable ledPWM that will be used to specify
the current PWM cycle of the LED. Initialize it at 0.
3) Within the loop() function, we will use a logic statement to modify the current PWM cycle
(brightness) of the LED. Using an ifelse conditional, program the Arduino to add or subtract
from the ledPWM variable based on a comparison with the ledDes value:
43)
44)
if (ledPWM < ledDes)
45)
{
46)
ledPWM = ledPWM + 1; // increase the value of ledPWM if it is less than the
desired value
47)
}
48)
else
49)
{
50)
ledPWM = ledPWM 1; // decrease the value of ledPWM if it is greater than
(or equal to) the desired value
51)
}
52)
4) Modify the previous line of code controlling the LED PWM cycle to use the ledPWM
variable(instead of the dialVal variable).
5) Modify the previous line of code outputting to the serial monitor. Set it to output the value of
the LED brightness that is being read by the LDR

a)

Prior to the setup() function, initialize the variables for the LDR pin port and
value:
53)
54)
int LDRPin = 3; // digital pin with LDR used to measure brightness
55)
b) Within the loop() function, add a function to read the brightness using the LDR
ledPWM = digitalRead(LPRPin)

6) Upload the code, and observe whether the control works. The serial monitor should output
values near the desired LED brightness that you specified.
56)

57)

Part 4, Experiment!

58)
Use the remaining lab time to try other control options or circuits. Try using the
potentiometer to change the brightness control value, or modifying the rate at which the ledPWM
variable increments. Use 3 LEDs to display an indication of how covered the LDR is. Use
MATLAB to acquire the data output from your circuit and plot the effect of the control circuit.
59)

60)

Part 5, Return the lab space to the prior condition.

1) Turn off and unplug all equipment (excluding the lab computer and the NI USB-6008), and rerack all power cords.
2) Remove the leads connecting any equipment to the breadboard and return them to the rack.
61)

62) Post-Laboratory Assignment


63)
64)
65)

There will be no post-laboratory assignment for this experiment.