DAC and PWM
Outline
• Introduction to digital-to-analog converter (DAC)
• Pulse-width modulation (PWM)
• Introduction to stepper motor
1
ADC
• We have learned that we can quantify a nature value by ADC.
Analog signal Digital signal
• We can also transform digital value into analog signals, e.g. control
motors, generate sounds, ...
Analog signal
Binary input IN DAC OUT
2
DAC Example – RGB LED
• If we give LED more power, it will be brighter. So we can use this
feature to intensify Red, Blue and Green diode through a DAC.
3
RGB LED Animation
Reference : Matthew Beckler's Home Page (https://www.mbeckler.org/microcontrollers/rgb_led/)
4
Outline
• Introduction to digital-to-analog converter (DAC)
• Pulse-width modulation (PWM)
• Introduction to stepper motor
5
Pulse-width Modulation (PWM)
• We can use DAC to control the output voltage, but this usually requires an
additional chip.
• Pulse-width modulation (PWM) provides the ability to simulate varying
levels of outputs. Thus, we can use PWM to convert digital signals into
analog intensities without DAC.
6
Pulse-width Modulation
• A PWM signal:
• Different duty cycles imply different average power.
• Duty Cycle = (PW/T) * 100%, where PW is pulse width time and T is total period of a
signal.
• Ex.: A 90% duty cycle means the signal is high 90% of the time and low 10% of
the time.
7
Example of Duty Cycle If high voltage = 5 V
• and duty cycle = 20%, then
the average output voltage =
5 x 0.2 = 1 V
5 x 0.4 = 2V
5 x 0.8 = 4V
5 x 1 = 5V
8
PWM for Driving RGB LED
(https://developer.mbed.org/users/4180_1/notebook/rgb-leds/ (https://www.pjrc.com/teensy/rgb_led.gif 9
) )
PWM in Arduino
•6 pins, 3, 5, 6, 9, 10, 11, marked by ~
can produce PWM output via
analogWrite(pinNumber, dutyCycle)
10
PWM in Arduino - analogWrite()
• analogWrite(pin, value)
• Writes an analog value (PWM wave) to a pin.
• Value specifies the duty cycle between 0 (always off) and 255 (always on).
• Pin will generate a steady square wave of the specified duty cycle until the next
call to analogWrite().
• The frequency of the PWM signal on most pins is approximately 490 Hz.
• On Arduino UNO, pins 5 and 6 have a frequency of approximately 980 Hz.
11
12
Outline
• Introduction to digital-to-analog converter (DAC)
• Pulse-width modulation (PWM)
• Introduction to stepper motor
13
Stepper Motor
• An electromagnetic device that
• Converts digital pulses into mechanical shaft rotation
• Ex. analog clock
• Precision
• Determined primarily by the number of steps per revolution
(coecsl.ece.illinois.edu/ge423/sensorprojects/StepMotor.ppt
)
14
Stepper Motor
• Controlled by a series of electromagnetic coils
• The coils are alternately given current.
• The creating magnetic fields will repulse or attract the magnets on the shaft,
causing the motor to rotate.
(http://www.electronics-tutorials.ws/io/io_7.html)
15
Example Stepper Motor: 28BYJ-48
• Number of phases: 4
• Step angle:
• 8-step sequence: 5.625° (64 steps per revolution)
• 4-step sequence: 11.25° (32 steps per revolution)
• Gear reduction ratio: 1/64
• 32*64 = 2048 steps per revolution in
4-step sequence
16
Stepper Motor Driver
• The output voltage provided by Arduino UNO cannot drive 28BYJ-48.
• So we need to use an amplifier, ULN2003, to raise the output voltage.
4 LEDs indicate which coil is currently powered
17
Drive Methods
1-phase excitation 2-phase excitation
• Different drive methods for stepper motors.
• Different methods cause different shakiness, power consumption and
accuracy.
18
Stepper.c • Arduino stepper library
uses 2-phase excitation
to rotate the motor.
19
Sample Code for Stepper Motor
#include <Stepper.h>
// change this to the number of steps on your motor
#define STEPS 100
// create an instance of the stepper class, specifying
# of steps of the motor and the pins it is attached to
Stepper stepper(STEPS, 8, 9, 10, 11);
// the previous reading from the analog input
int previous = 0;
void setup() {
stepper.setSpeed(30); // set the speed to 30 RPMs
}
20
Sample Code for Stepper Motor
void loop() {
// get the sensor value from analog input A0
int val = analogRead(A0);
// move a number of steps equal to the change in the
// sensor reading
stepper.step(val - previous);
// remember the previous value of the sensor
previous = val;
}
21