ADC — Maple v0.0.9 Documentation

### Navigation

- next
- previous |
- LeafLabs |
- Docs Home |
- Index »

# ADC

Analog-Digital Conversion is the process of reading a physical voltage as a number. The Maple has a large number of pins which are capable of taking 12-bit ADC measurements, which means that voltages from ground to +3.3v are read as numbers from 0 to 4095; this corresponds to a theoretical sensitivity of just under 1 millivolt. In reality, a number of factors introduce noise and bias into this reading and a number of techniques must be used to get good precision and accuracy.

The header pins with ADC functionality (marked as “AIN” on the silkscreen) are:

> D0, D1, D2, D3, D10, D11, D12, D13, D15, D16, D17, D18, D19, D20, D27, D28

Note that pins 3, 27, and 28 are not marked AIN on the silkscreen for Maple revisions through Rev 5, however, they do work as analog input pins.

Contents

- Noise and Bias
- Function Reference
- Recommended Reading

## Noise and Bias

The biggest issues with analog-digital conversion are noise and bias. With the Maple, we have tried to isolate the ADC pins and traces from strong noise sources but there are always trade–offs between noise, additional functionality, cost, and package size.

The 6 ADC pins in a bank (D15–D20) generally have the least noise and should be used for fine measurements. If the input voltage changes relatively slowly, a number of samples can be taken in succession and averaged together, or the same voltage can even be sampled by multiple ADC pins at the same time.

An important factor when taking a voltage reading is the reference voltages that the sample is being compared against. In the case of the Maple, the high reference is Vcc and the low reference is ground. This means that noise or fluctuations on either Vcc or ground will affect the measurement. It also means that the voltage you are trying to sample must be between ground and 3.3V. In the case of a variable reading, it is best if the voltage varies over the entire range of 0–3.3V; otherwise, only a fraction of the sensitivity is being leveraged. Resistor dividers and constant voltage diodes are basic tools which can help bring a given voltage signal into the appropriate range; opamps and other powered components can also be used.

## Function Reference

uint32 analogRead(uint8 pin)

  Read an analog value from pin.

  This function blocks during ADC conversion, and has 12 bits of resolution. The pin must have its mode set to INPUT_ANALOG. Ignoring function call overhead, conversion time is 55.5 cycles.

  Parameters

  - `pin` -

    Pin to read from. One of: 0, 1, 2, 3, 10, 11, 12, 13, 15, 16, 17, 18, 19, 20, 27, 28.

  Return
    ADC-converted voltage, in the range 0–4095, inclusive (i.e. a 12-bit ADC conversion).
  See
    pinMode()
void pinMode(uint8 pin, WiringPinMode mode)

  Configure behavior of a GPIO pin.

  Parameters

  - `pin` -

    Pin to configure. One of: 0-38 (pin numbers as labeled on silkscreen), or D0-D38 (symbols for same)

  - `mode` -

    Mode corresponding to desired pin behavior.

  See
    WiringPinMode
WiringPinMode enum

  Specifies a GPIO pin behavior.

  Each of the GPIO pins on a Maple board may be configured using pinMode() to behave in a number of ways: as a digital output pin, or as an analog input pin, etc., depending on the particular pin.

  This enum specifies the complete set of possible configurations; not every pin can have all of these modes. For example, on the Maple, pin 15 may be configured as INPUT_ANALOG, but not as PWM. See your device’s silkscreen and and the GPIO documentation for more information.

  See
    pinMode()

  Values:

  - `OUTPUT` -

    Basic digital output: when the pin is HIGH, the voltage is held at +3.3v (Vcc) and when it is LOW, it is pulled down to ground.

  - `OUTPUT_OPEN_DRAIN` -

    In open drain mode, the pin indicates “low” by accepting current flow to ground and “high” by providing increased impedance.

    An example use would be to connect a pin to a bus line (which is pulled up to a positive voltage by a separate supply through a large resistor). When the pin is high, not much current flows through to ground and the line stays at positive voltage; when the pin is low the bus “drains” to ground with a small amount of current constantly flowing through the large resistor from the external supply. In this mode no current is ever actually /sourced/ from the pin.

  - `INPUT` -

    Basic digital input.

    The pin voltage is sampled; when it is closer to 3.3v (Vcc) the pin status is high, and when it is closer to 0v (ground) it is low. If no external circuit is pulling the pin voltage to high or low, it will tend to randomly oscillate and be very sensitive to noise (e.g., a breath of air across the pin might cause the state to flip).

  - `INPUT_ANALOG` -

    This is a special mode for when the pin will be used for analog (not digital) reads.

    Enables ADC conversion to be performed on the voltage at the pin.

  - `INPUT_PULLUP` -

    The state of the pin in this mode is reported the same way as with INPUT, but the pin voltage is gently “pulled up” towards +3.3v.

    This means the state will be high unless an external device is specifically pulling the pin down to ground, in which case the “gentle” pull up will not affect the state of the input.

  - `INPUT_PULLDOWN` -

    The state of the pin in this mode is reported the same way as with INPUT, but the pin voltage is gently “pulled down” towards 0v.

    This means the state will be low unless an external device is specifically pulling the pin up to 3.3v, in which case the “gentle” pull down will not affect the state of the input.

  - `INPUT_FLOATING` -

    Synonym for INPUT.

  - `PWM` -

    This is a special mode for when the pin will be used for PWM output (a special case of digital output).

  - `PWM_OPEN_DRAIN` -

    Like PWM, except that instead of alternating cycles of LOW and HIGH, the voltage on the pin consists of alternating cycles of LOW and floating (disconnected).

## Recommended Reading

- Wikipedia article on Analog-to-digital converter
- Arduino Analog Input Tutorial
- STMicro documentation for STM32F103RB microcontroller:
  - All
  - Datasheet (pdf)
  - Reference Manual (pdf)
  - Application Note on ADC Modes (pdf)
  - Application Note on ADC Oversampling (pdf)

### Navigation

- next
- previous |
- LeafLabs |
- Docs Home |
- Index »