ARD (Analog Read)

From Dooba Wiki
Jump to navigation Jump to search
Potentiometer wired to an analog input pin

Introduction

This tutorial will present how to read analog input values using the 'ard' library.

Some pins have the ability to functions as 'analog inputs' - they can be used to read any analog voltage level (comprised between 0V and VCC which is 3.3V on the ioNode).

Note: providing voltages higher than VCC (3.3V on ioNode) may damage the pins! Remember to use voltage dividers or other mechanisms to constrain the voltage before hooking something to your analog inputs.

How to use

First, we need to add the 'ard' library to our dependencies in dfe.conf:

name: example_app
type: app
mmcu: atmega1284p
freq: 10000000
deps:
  - ard

Initialization

Before we can read values, we need to initialize the library. If we're using the substrate, this is as trivial as adding the following to our 'substrate' file:

# Use Analog Read
uses :ard

If, however, we want to do this without substrate, we can do it manually from our C code:

#include <ard/ard.h>

void main()
{
    // Initialize Analog Read Subsystem
    ard_init();

    // ...
}

Reading a value

Once the library is initialized (whether through substrate or manually from the C code), we can start reading analog values by passing a pin number to the 'ard()' function, which will return an 8-bit value representing the voltage level being applied on the pin.

#include <dio/dio.h>

void main()
{
    uint8_t v;

    // ...

    // Read Analog Value on pin 7
    v = ard(7);

    // ...
}