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 DIRECT 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 INVERTED 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;
}

Important

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.

Reference

class AnalogOut : public AnalogSource, public PinConfig

A generic class representing a simple PWM output.

Public Functions

AnalogOut(uint8_t pin, Engine &engine = Engine::primary())

Constructor with default mode DIRECT.

Parameters

pin – the pin number

AnalogOut(uint8_t pin, uint8_t mode, Engine &engine = Engine::primary())

Constructor.

Parameters
  • pin – the pin number

  • mode – the mode (DIRECT or INVERTED)

virtual float put(float value)

Pushes value into the component and returns its (possibly filtered) value.

inline virtual void invert()

Inverts value by calling put(1-get()) (eg. 0.2 becomes 0.8).

void write(float value)

Direclty writes value in [0, 1] to the pin (bypasses mode and engine).

void rawWrite(int value)

Direclty writes raw value to the pin (bypasses mode and engine).

inline virtual float get()

Returns value in [0, 1].

inline virtual float mapTo(float toLow, float toHigh)

Maps value to new range.

inline float seconds() const

Returns engine time in seconds.

inline uint32_t milliSeconds() const

Returns engine time in milliseconds.

inline uint64_t microSeconds() const

Returns engine time in microseconds.

inline unsigned long nSteps() const

Returns number of engine steps.

inline float sampleRate() const

Returns engine sample rate.

inline float samplePeriod() const

Returns enginesample period.

inline operator float()

Object can be used directly to access its value.

inline explicit operator bool()

Operator that allows usage in conditional expressions.

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.

Public Static Functions

static inline bool analogToDigital(float f)

Converts analog (float) value to digital (bool) value.

static inline float digitalToAnalog(bool b)

Converts digital (bool) value to analog (float) value.

See Also