Mozzi  version v2.0
sound synthesis library for Arduino
Analog

Efficient analog input functions for sensors and audio. More...

Detailed Description

Efficient analog input functions for sensors and audio.

Helps produce glitch-free audio by allowing analog input functions which normally block processing to be performed in the background.

Functions

void setupFastAnalogRead (int8_t speed=FAST_ADC)
 This is automatically called in startMozzi. More...
 
void disconnectDigitalIn (uint8_t channel_num)
 Prepare an analog input channel by turning off its digital input buffer. More...
 
void reconnectDigitalIn (uint8_t channel_num)
 Reconnect the digital input buffer for an analog input channel which has been set for analog input with disconnectDigitalIn(). More...
 
void adcDisconnectAllDigitalIns ()
 Prepare all analog input channels by turning off their digital input buffers. More...
 
void adcReconnectAllDigitalIns ()
 Reconnect the digital input buffers for analog input channels which have been set for analog input with disconnectDigitalIn().
 
template<byte RES>
uint16_t mozziAnalogRead (uint8_t pin)
 See mozziAnalogRead(). More...
 
uint16_t mozziAnalogRead16 (uint8_t pin)
 See mozziAnalogRead() but always returns the value shifted to 16 bit range. More...
 
template<byte RES>
uint16_t getAudioInput ()
 See getAudioInput(). More...
 
template<byte RES>
uint16_t getAudioInput16 ()
 See getAudioInput(). More...
 

Function Documentation

◆ adcDisconnectAllDigitalIns()

void adcDisconnectAllDigitalIns ( )
inline

Prepare all analog input channels by turning off their digital input buffers.

This helps to reduce noise, increase analog reading speed, and save power.

Definition at line 111 of file mozzi_analog.h.

◆ disconnectDigitalIn()

void disconnectDigitalIn ( uint8_t  channel_num)
inline

Prepare an analog input channel by turning off its digital input buffer.

This helps to reduce noise, increase analog reading speed, and save power.

Here's more detail from http://www.openmusiclabs.com/learning/digital/atmega-adc/:

The DIDR (Data Input Disable Register) disconnects the digital inputs from whichever ADC channels you are using. This is important for 2 reasons. First off, an analog input will be floating all over the place, and causing the digital input to constantly toggle high and low. This creates excessive noise near the ADC, and burns extra power. Secondly, the digital input and associated DIDR switch have a capacitance associated with them which will slow down your input signal if you are sampling a highly resistive load.

And from the ATmega328p datasheet, p266:

When an analog signal is applied to the ADC pin and the digital input from this pin is not needed, this bit should be written logic one to reduce power consumption in the digital input buffer. Note that ADC named_pins ADC7 and ADC6 do not have digital input buffers, and therefore do not require Digital Input Disable bits.

Parameters
channel_numthe analog input channel you wish to use.

Definition at line 86 of file mozzi_analog.h.

◆ getAudioInput()

template<byte RES>
uint16_t getAudioInput ( )

See getAudioInput().

This returns audio input from the input buffer, if @ref MOZZI_AUDIO_INPUT is enabled in the config (see also the related option MOZZI_AUDIO_INPUT_PIN).

The template parameter specifies the desired value range in bits.

The audio signal needs to be in the range 0 to VCC volts (i.e. 5 volts on Arduino Uno R3). Circuits and discussions about biasing a signal in the middle of this range can be found at http://electronics.stackexchange.com/questions/14404/dc-biasing-audio-signal and http://interface.khm.de/index.php/lab/experiments/arduino-realtime-audio-processing/ . A circuit and instructions for amplifying and biasing a microphone signal can be found at http://www.instructables.com/id/Arduino-Audio-Input/?ALLSTEPS

Note
The value range returned by this function follows the same rules as detailed in the documentation for mozziAnalogRead(): For portable code, define MOZZI_ANALGO_READ_RESOLUTION at the top of your sketch, or use the templated version of this function.
Returns
audio data from the input buffer

Definition at line 172 of file MozziGuts.h.

◆ getAudioInput16()

template<byte RES>
uint16_t getAudioInput16 ( )
inline

See getAudioInput().

Equivalent to getAudioInput<16>().

Definition at line 149 of file MozziGuts.h.

◆ mozziAnalogRead()

template<byte RES>
uint16_t mozziAnalogRead ( uint8_t  pin)

See mozziAnalogRead().

Reads the analog input of a chosen channel, without blocking other operations from running.

The template parameter RES specifies the number of bits to return.

It actually returns the most recent analog reading and puts the chosen pin or channel on the stack of channels to be read in the background before the next control interrupt.

Note
Analog reads have different hardware resolution on different platforms. E.g. an analog read on an Arduino Uno R3 will return a value in the range 0-1023 (10 bits), on a Raspberry Pi Pico it will return 0-4095 (12 bits). For portable code, it is thus necessary to specify the desired resolution of reads. This can be done by setting MOZZI_ANALOG_READ_RESOLUTION to the resolution in bits, near the top of your sketch. All reads will then be adjusted to that range, automatically (using a simple bit-shift). Alternatively, the templated version of mozziAanalogRead() allows to specifiy the target resolution per read. If MOZZI_ANALOG_READ_RESOLUTION is not defined, this (non-templated) function returns a value in the default hardware resolution, with a warning.
Parameters
pin_or_channelthe analog pin or channel number.
Returns
the digitised value of the voltage on the chosen channel. See the note above regarding the output range!

Definition at line 174 of file mozzi_analog.h.

◆ mozziAnalogRead16()

uint16_t mozziAnalogRead16 ( uint8_t  pin)
inline

See mozziAnalogRead() but always returns the value shifted to 16 bit range.

THis is exactly equivalent to mozziAnalogRead<16>(pin);

Definition at line 152 of file mozzi_analog.h.

◆ reconnectDigitalIn()

void reconnectDigitalIn ( uint8_t  channel_num)
inline

Reconnect the digital input buffer for an analog input channel which has been set for analog input with disconnectDigitalIn().

Parameters
channel_numthe analog input channel you wish to reconnect.

Definition at line 99 of file mozzi_analog.h.

◆ setupFastAnalogRead()

void setupFastAnalogRead ( int8_t  speed = FAST_ADC)

This is automatically called in startMozzi.

It makes mozziAnalogRead() happen faster than the standard Arduino analogRead(), changing the duration from about 105 in unmodified Arduino to about 16 microseconds for a dependable read with the default speed parameter FAST_ADC. If you want to set on of the faster modes (see params) you can call this after startMozzi(). See: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1208715493/11, and http://www.marulaberry.co.za/index.php/tutorials/code/arduino-adc/

Parameters
speedFAST_ADC, FASTER_ADC or FASTEST_ADC. If no parameter is supplied, the default is FAST_ADC, which sets the analog conversion clock predivide rate to 16, giving 1Mhz on a 16MHz board, the fastest rate for which behaviour is defined (~16us per sample). However, divisors of 8 and 4 also show usable results in the graphs in this paper: http://dam.mellis.org/Mellis%20-%20Sensor%20Library%20for%20Arduino%20-%20Paper.pdf, so you can also try FASTER_ADC or FASTEST_ADC for divide rates of 8 or 4, giving times of about 8us or 4us per sample. Beware, reliable results will depend on the sort of input signal you have. Only use FASTER_ADC or FASTEST_ADC if you know what you're doing.