Arduino PWM
Arduino PWM
USB
Arduino
https://developer.android.com/things/sdk/pio/pwm.html
Pulse-Width Modulation (PWM)
Pulse-Width Modulation (PWM)
The “shocking truth” behind analogWrite():
• We know that the Arduino can read analog voltages (voltages between 0 and 5 volts)
using the analogRead() function.
• Is there a way for the Arduino to output analog voltages as well? The answer is no... and
yes. Arduino does not have a true analog voltage output. But, because Arduino is so
fast, it can fake it using something called PWM ("Pulse-Width Modulation"). The pins on
the Arduino with “~” next to them are PWM/Analog out compatible.
• The Arduino is so fast that it can blink a pin on and of almost 1000 times per second.
PWM goes one step further by varying the amount of time that the blinking pin spends
HIGH vs. the time it spends LOW. If it spends most of its time HIGH, a LED connected to
that pin will appear bright. If it spends most of its time LOW, the LED will look dim.
Because the pin is blinking much faster than your eye can detect, the Arduino creates
the illusion of a "true" analog output.
• To smooth the signal even more, we will create and use a RC circuit (Lowpass Filter)
Pulse-Width Modulation (PWM)
• The Arduino's programming language makes PWM easy to use; simply call
analogWrite(pin, dutyCycle), where dutyCycle is a value from 0 to 255, and pin is one
of the PWM pins (3, 5, 6, 9, 10, or 11).
• The analogWrite function provides a simple interface to the hardware PWM, but
doesn't provide any control over frequency. (Note that despite the function name, the
output is a digital signal, often referred to as a square wave.)
analogWrite():
https://www.arduino.cc/en/Reference/AnalogWrite
• http://www.instructables.com/id/Analog-
Output-Convert-PWM-to-Voltage/
• http://provideyourown.com/2011/analogwrit
e-convert-pwm-to-voltage/
Electrical Components
Capacitor
Resistor
e.g.,
A capacitor stores and releases electrical energy in a A resistor resists the flow of electrical energy in a
circuit. When the circuits voltage is higher than what is circuit, changing the voltage and current as a result
stored in the capacitor, it allows current to flow in, giving (according to Ohms law, 𝑈 = 𝑅𝐼). Resistor values are
the capacitor a charge. When the circuits voltage is lower, measured in ohms (Ω). The color stripes on the sides
the stored charge is released. Often used to smooth of the resistor indicate their values. You can also use
fluctuations in voltage a Multi-meter in order to find the value of a given
https://en.wikipedia.org/wiki/Capacitor resistor.
These electronics components are typically included in a “Starter Kit”, or they can be bought “everywhere” for a few bucks.
Capacitor e.g.,
Note! You can also easily measure the capacitance using a Multi-
meter. A Multi-meter that cost from 400-500+ NOK has built-in
support for measuring capacitors (same for resistors and
resistance).
𝐿𝐷𝐴𝐶
𝑉𝑅𝐸𝐹
𝑉I𝑈𝑇
𝑉𝑆𝑆
SCK (13) 8 7 6 5
MISO (12)
MOSI (11) MCP4911
SS (10)
1 2 3 4
𝑆𝐶𝐾
𝑆𝐷𝐼
𝑉𝐷𝐷
𝐶𝑆
MISO Not Used, since we get nothing back from DAC IC
MCP49xx Arduino Library Example
#include <SPI.h> //Include the Arduino SPI Library
https://github.com/exscape/electronics/tree/master/Arduino/Libraries
#include <DAC_MCP49xx.h> //Include the MCP49xx Arduino Library
// The Arduino pin used for the slave select / chip select
#define SS_PIN 10
void setup()
{
}
void loop()
{
double u; //Control Signal
// For MCP4911, use values below (but including) 1023 (10 bit)
u = 255; //Simulating the Control Value
dac.output(u);
delay(5000);
Connect the circuit (Arduino + MCP4911) on a
u = 512; //Simulating the Control Value
dac.output(u);
breadboard. Use a multi-meter so see if you get
}
delay(5000); the correct output signal
MCP49xx Arduino Library Example
#include <SPI.h> //Include the Arduino SPI Library
#include <DAC_MCP49xx.h> //Include the MCP49xx Arduino Library
// The Arduino pin used for the slave select / chip select
#define SS_PIN 10
void setup()
{
Connect the circuit (Arduino + MCP4911) on a
}
Serial.begin(9600);
breadboard. Use a multi-meter so see if you get the
void loop()
correct output signal.
{
double u; //Control Signal
int aiPin = 0;
int aiValue;
On the Multimeter you should see the output slowly
for (int i=0; i<1023; i++)
increasing from ~0V to ~5V with intervals of 1000ms.
{
u = i;
dac.output(u); You can also connect the output from the DAC to an
aiValue = analogRead(aiPin);
Serial.print("AIValue=");
Analog Input Pin on the Arduino. Write the value to
Serial.println(aiValue); the Serial Monitor.
delay(1000);
}
}
Alternative Solution
MCP4725
12-bit resolution
I2C Interface The MCP4725 is a little more expensive
(than MCP49xx), but simpler to use.