Skip to end of metadata
Go to start of metadata

You are viewing an old version of this content. View the current version.

Compare with Current View Version History

« Previous Version 2 Next »

Purpose

Get the value of an Analog Input (AI) of the board.

When to use

Whenever you need to use the state of an AI.

Where to use

Most probably in the void loop() part of your .ino file, since we’d usually want to actively monitor the value.

Click here to learn more on the AIOs of the 9880 board.

Analog ports, contrary to Digital ports, do not need to be initialized as inputs or outputs when used with CrcLib. They can only be used as inputs, so they are automatically considered as such.

This function requires the use of the following CrcLib functions at some point of your .ino file in order to work properly:

Returns

This function returns the value of the AI mentioned as the function’s argument as an unsigned int type. The value returned will range between 0 and 1023, proportionally to the voltage applied to the the SIG pin of an Analog Port. The returned value will be:

  • 0, if 0V is read on the SIG pin (it is shorted to a GND pin).

  • 1 to 1022, proportionally to the applied voltage.

  • 1023, if 7.5V is read on the SIG pin.

Main prototype and parameters

static unsigned int CrcLib::GetAnalogInput(unsigned char pin)

The following parameters must be passed to the function for it to work properly:

  • pin: The name of the analog pin you want to read the state of. Must be of the type unsigned char.

Overloads

This function does not have any overloads. It can only be used as described by the main prototype.

Examples

 #1: We want to print on the serial monitor the voltage read on the Analog Port #2.
#include <CrcLib.h>

//Variable declaration
unsigned int value;

void setup() {
    CrcLib::Initialize();

    //Open the serial monitor
    Serial.begin(9600);

    /* 
    The rest of your setup code
    ...
     */
}
void loop() {
    CrcLib::Update();

    //Read the value on analog input Port #2, then print it on the serial monitor
    value = CrcLib::GetAnalogInput(CRC_ANA_2);
    Serial.println(value);
    
    /* 
    The rest of your looping code
    ...
    */
}

More on this function

Why can the function only return values from 0 to 1023, while an unsigned int theoretically ranges from 0 to 65 535 ?

Analog voltage readings on micro-controllers are performed with an analog-to-digital converter (ADR). The ADR on CrcDuino has a 10-bit resolution, meaning it can only represent voltages as 1024 different subdivisions of it’s reference (maximum) voltage value. Therefore, the function return is limited to 1024 different values, from 0 to 1023.

The default voltage reference of the CrcDuino ADR is 5V, but a voltage divider circuit incorporated to each individual analog input pins divides the voltage effectively read by the micro-controller pin by 1.5, boosting the reading range to 7.5V.

What is the smallest voltage change that can be detected by GetAnalogInput() ?

The analog inputs can divide a range of 0 to 7.5V into 1024 “steps”. Therefore, each “step” is a multiple of 7.5/1024=0.007V. The resolution of the reading is 7mV, so changes of less than 7mV cannot be recorded on the input. 7mV is very small, and is far than enough considering the precision of the components and sensors used in recreational robotics.

Nothing is connected to the pin I am reading, but the returned value is not 0.

It is normal; this value will fluctuate based on various, seemingly unrelated factors like how close a hand is to the pin or the voltage applied to other analog inputs. A return value of 0 is almost only possible to obtain if the SIG pin is directly shorted to a GND pin.

Related articles

  • No labels

0 Comments

You are not logged in. Any changes you make will be marked as anonymous. You may want to Log In if you already have an account.