Beruflich Dokumente
Kultur Dokumente
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
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)
24)
25)
26)
b.
Within the loop() function (between the curly brackets), acquire the current
potentiometer output:
c.
d.
7) Connect the LED light to the Arduino, using a PWM-capable pin using the diagram shown in
Fig 2.
27)
28)
29)
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.
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.
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)
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)
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)