Sei sulla pagina 1di 4

UG 106: Praxis I September 2012 Semester Undergraduate Program Asian Insitute of Technology Handout: Arduino Tutorial Pulse Width

Modulation & Analog Input Instructors: Waqar Shahid, Matthew N. Dailey

Arduino Tutorial Pulse Width Modulation & Analog Input


Introduction: This tutorial will introduce you to pulse width modulation, and make you learn how to generate it using Arduino board. We will also learn how to read an analog input which is necessary to know as some of the input sensors would be analog. Credits: Thanks to Pham Huy Nguyen for pointers to Arduino tutorials.

What is Pulse Width Modulation?


The on(high or 5 volts) and o(low or 0 volts) patten of a digital output is termed as a square wave. The duration of the on time is known as pulse-width. When the width pulse is varied rapidly to produce an eect of varying analog voltage signal, then the output is known as PWM signal. In other words, an analog signal is modulating over a digital signal. In computer controlled circuit where the I/O pins are digital, such signal helps us to control analog circuit. The signal look like as shown in gure

Figure 1: PWM Wave reprinted from http://arduino.cc/en/Tutorial/PWM

Arduino Hardware Summary


For revision the summary of the boards specications is follows:

Microcontroller Operating Voltage Input Voltage (recommended) Input Voltage (limits) Digital I/O Pins Analog Input Pins DC Current per I/O Pin DC Current for 3.3V Pin Flash Memory SRAM EEPROM Clock Speed

ATmega328 5V 7 12V 6 20V 14 (of which 6 provide PWM output) 6 40 mA 50 mA 32 KB (ATmega328) of which 0.5 KB used by bootloader 2 KB (ATmega328) 1 KB (ATmega328) 16 MHz

From the last lab we also remember that, PWM pin-3, pin-5, pin-6, pin-9, pin-10, pin-11 can be congured as Pulse width modulation (PWM) outputs. Arduino board contains a 6-channel 10-bit analog-to-digital conversion. A value from 0 - 5 volts is converted to integer value from 0 to 1023. Therefore a resolution of: 5 volts / 1024 units or, 4.9 mV per unit. Analog input is read after every 0.0001 s, so the maximum reading rate is about 10,000 times a second.

PWM Generation
For this program we need the hardware given as follows: 1. Arduino UNO R3 2. Proto-board 3. Digital Oscilloscope 4. Jumpers (connecting wires) 5. Light emitting diode (LED) 6. Resistors ( 470) Identifying the positive and the negative terminal of the LED is important before implementing the circuit. Identify the value of resistor without the use of a multi-meter. You can connect the circuit as shown in the Figure 2a,2. We use pin-3 as the PWM output, however you can try any of the above PWM output pins. For analog input we use analog pin A-3, but you can also use any pin from A0 - A5. Now coming to the software part, as to how to program our micro-controller. Open the Arduino IDE as shown in the Figure 3. You remember that our program must have to functions setup() and the loop functions. The function analogWrite() will write to the PWM pin. The pin will generate a study square wave until the next call of the same function i.e. analogWrite(),digitalWrite() or a digitalRead() etc. The frequency of the PWM is 490 Hz. It has nothing to do with the analog input pins.
/* /* Fading with pwm-signal using delay factor from potentiometer The circuit: * LED attached from digital pin 3 to ground. Created 1 Nov 2008 By David A. Mellis modified 19 Sep 2012 By Waqar Shahid Qureshi

(a) Schematics generated by Fritzing-0.7.7b

(b) Diagram generated by Fritzing-0.7.7b

Figure 2: Implementation Diagram

Figure 3: Arduino IDE for cross-platform compilation

http://arduino.cc/en/Tutorial/Fading This example code is part of the fading example and analogInput example */

int ledPin = 3; // LED connected to digital pin 3 int sensorPin = A0; // select the input pin for the potentiometer int sensorValue = 0; // variable to store the value coming from the sensor void setup() { // nothing happens in setup } void loop() { // fade in from min to max in increments of 5 points: for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { // sets the value (range from 0 to 255): analogWrite(ledPin, fadeValue);

// read the value from the sensor: sensorValue = analogRead(sensorPin); //read the value delay(sensorValue); //delay for sensor value } // fade out from max to min in increments of 5 points: for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { // sets the value (range from 0 to 255): analogWrite(ledPin, fadeValue); sensorValue = analogRead(sensorPin); // read the value delay(sensorValue); //delay for the sensor value } }

Press cntrl-R to compile the code. If there is no errors, then press cntrl-U to upload the compiled program on your C Flash memory. As told earlier, once the program is uploaded successfully, the board will automatically get a reset and after a few second will start running your code. Check the waveform from pin-3 on the oscilloscope.

References
http://arduino.cc. http://arduino.cc/en/Main/ArduinoBoardUno. http://arduino.cc/en/Guide/Introduction. www.atmel.com/Images/doc8161.pdf. Arduino Programming Notebook by - by Brian W. Evans. Beginning Android ADK with Arduino by Mario Bohmer http://arduino.cc/en/Tutorial/Button http://arduino.cc/en/Tutorial/Blink http://arduino.cc/en/uploads/Main/arduino_Uno_Rev3-02-TH.zip. http://arduino.cc/en/uploads/Main/Arduino_Uno_Rev3-schematic.pdf. http://arduino.cc/en/Main/ArduinoBoardUno.

Potrebbero piacerti anche