Professional Documents
Culture Documents
Arduino Tutorial "Pulse Width Modulation & Analog Input
Arduino Tutorial "Pulse Width Modulation & Analog Input
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 configured 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
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);
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.