me330 experiment 10 introduction to the arduino uno microcontroller(1)

5
111Equation Chapter 1 Section 1Experimen t 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 light-dependent resistor. Equipment List 1) Digital Multi-Meter 2) Arduino UNO Microcontroller 3) Rotational Potentiometer 4) LED (Light Emitting Diode) 5) LDR (Light Dependant Resistor) 6) Breadboard – JE25 7) Wire kit and screwdriver 8) Needle nose pliers and wire Stripper 9) Circuit Component Kit 10) USB Data Acquisition Device (NI USB-6008) Laboratory Instructions 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. Part 1, Create an Arduino program to control LED brightness using pulse- width 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 Arduino’s +5V and GND pins to provide power to your potentiometer. b. Connect the potentiometer output to one of the Arduino’s 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.

Upload: martin-vail

Post on 15-Jan-2016

219 views

Category:

Documents


0 download

DESCRIPTION

Yep

TRANSCRIPT

Page 1: ME330 Experiment 10 Introduction to the Arduino Uno Microcontroller(1)

111Equation Chapter 1 Section 1Experiment 10: Introduction to the Arduino UNO Microcontroller

Laboratory ObjectivesThe 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 light-dependent resistor.

Equipment List1) Digital Multi-Meter 2) Arduino UNO Microcontroller3) Rotational Potentiometer4) LED (Light Emitting Diode)5) LDR (Light Dependant Resistor)

6) Breadboard – JE257) Wire kit and screwdriver8) Needle nose pliers and wire Stripper9) Circuit Component Kit10) USB Data Acquisition Device (NI USB-6008)

Laboratory InstructionsIn 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.

Part 1, Create an Arduino program to control LED brightness using pulse-width 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 Arduino’s +5V and GND pins to provide power to your potentiometer.b. Connect the potentiometer output to one of the Arduino’s 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.

Figure 1. Arduino pin locations.

Page 2: ME330 Experiment 10 Introduction to the Arduino Uno Microcontroller(1)

3) Open the Arduino development environment.4) Set up the basic Arduino operation loop with the following code:

void setup() {}

void loop() { }

5) Set up your Arduino to accept input from the potentiometera. Prior to the setup() function, declare integer variables for the analog input pin you selected and the

potentiometer value:

int dialPin = A0; // analog input pin for potentiometerdouble dialVal = 0; // potentiometer input value, initialized at zero

b. 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 screena. 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); // print the value of the variable “dialVal” to the serial output

c. Run the Arduino code by clicking the “Upload” button.d. 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.

Figure 2. LED Circuit

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

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

b. Within the setup() function, declare the selected pin to be an output (it is an input by default):

Page 3: ME330 Experiment 10 Introduction to the Arduino Uno Microcontroller(1)

pinMode(ledPin, OUTPUT); // specify the LED pin as an 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

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.

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.

Figure 3. Voltage divider circuit

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.

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 if…else conditional, program the Arduino to add or subtract from the ledPWM variable based on a comparison with the ledDes value:

if (ledPWM < ledDes){

ledPWM = ledPWM + 1; // increase the value of ledPWM if it is less than the desired value}

Page 4: ME330 Experiment 10 Introduction to the Arduino Uno Microcontroller(1)

else{

ledPWM = ledPWM – 1; // decrease the value of ledPWM if it is greater than (or equal to) the desired value}

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:

int LDRPin = 3; // digital pin with LDR used to measure brightness

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.

Part 4, Experiment!

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.

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 re-rack all power cords.2) Remove the leads connecting any equipment to the breadboard and return them to the rack.

Post-Laboratory AssignmentThere will be no post-laboratory assignment for this experiment.