AnalogOut
An analog (ie. continuous) output unit that converts a value between 0 and 1 (ie. 0% and 100%) into an analog voltage on one of the analog output pins.
The unit is assigned to a specific pin
on the board.
The mode
specifies the behavior of the component attached to the pin:
in
SOURCE
mode (default) the pin acts as the source of current and the value is expressed as a percentage of the maximum voltage (Vcc, typically 5V)in
SINK
mode the source of current is external (Vcc)
Example
AnalogOut led(9);
void begin() {
led.put(0.5);
}
void step() {
// The LED value is changed randomly by a tiny amount (random walk).
// Mutliplying by samplePeriod() makes sure the rate of change stays stable.
(led + randomFloat(-0.1, 0.1) * samplePeriod()) >> led;
}
Reference
-
class AnalogOut : public AnalogSource, public PinUnit
A generic class representing a simple PWM output.
Public Functions
-
AnalogOut(uint8_t pin, uint8_t mode = DIRECT)
Constructor.
- Parameters:
pin – the pin number
mode – the mode (SOURCE or SINK)
-
virtual float put(float value)
Pushes value into the component and returns its (possibly filtered) value.
-
inline virtual float get()
Returns value in [0, 1].
-
inline uint8_t pin() const
Returns the pin this component is attached to.
-
inline uint8_t mode() const
Returns the mode of the component.
-
inline virtual void mode(uint8_t mode)
Changes the mode of the component.
-
AnalogOut(uint8_t pin, uint8_t mode = DIRECT)
Note
On most Arduino boards analog outputs rely on Pulse Width Modulation (PWM).
After a call to put(value)
, the pin will generate a steady square wave of the
specified duty cycle until the next call to put()
on the same pin. The frequency
of the PWM signal on most pins is approximately 490 Hz. On the Uno and similar boards,
pins 5 and 6 have a frequency of approximately 980 Hz.
Note
On most Arduino boards (those with the ATmega168 or ATmega328P), this functionality
works on pins 3, 5, 6, 9, 10, and 11. On the Arduino Mega, it works on pins
2 - 13 and 44 - 46. Older Arduino boards with an ATmega8 only support AnalogOut
on pins 9, 10, and 11. The Arduino DUE supports analog output on pins 2 through 13,
plus pins DAC0 and DAC1. Unlike the PWM pins, DAC0 and DAC1 are Digital to Analog
converters, and act as true analog outputs.