Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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.

Info

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

Info

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.

Note

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

Objectif

Obtenir l'état d’une entrée analogique du CRCduino

Quand l’utiliser

Dès que vous voulez mesurer un voltage.

Où l’utiliser

Typiquement dans la section void loop() de votre programme, pour mesurer continuellement sa valeur.

Info

Cliquer ici pour en apprendre plus sur les entrées analogiques du CRCduino

Info

Les ports analogiques, contrairement aux ports numériques, ne peuvent pas être utilisés comme sorties. Il n’est donc pas nécessaire de les configurer comme entrées, puisqu’ils le sont par défaut.

Note

C’est fonction requiert l’utilisation des fonctions suivantes dans votre programme:

Valeur de retour

Cette fonction retourne la valeur du port analogique (ANA) correspondant dans une valeur de type unsigned int. Cette valeur sera entre 0 (0 V) et 1023 (7.5 V), proportionnellement à la tension présente sur le port (broche SIG).

Prototype principal

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

Les paramètres suivants sont requis par la fonction:

  • pin: Le nom de la broche à vérifier, de type unsigned char.

Overloads

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

Examples

Cette fonction n’a pas d’overloads, elle ne peut être utilisée qu’avec le prototype principal.

Exemples

Expand
title#1: We want to print on the serial monitor the voltage read on the Analog Port Nous souhaitons afficher sur la console le voltage présent sur le port analogique #2.
Code Block
languagecpp
#include <CrcLib.h>

//Variable
declaration
unsigned int value;

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

    //Open theOuvrir serialla monitorconsole
    Serial.begin(96002000000);

    /* 
    TheReste restdu of your setup codecode d'initialisation
    ...
     */
}
void loop() {
    CrcLib::Update();

    //Read theLire valuela onvaleur analogsur inputle Portport #2,analogique then2
print it on the serialunsigned monitorint    valeur value = CrcLib::GetAnalogInput(CRC_ANA_2);
    // L'afficher sur la console
    Serial.println(value);
    
    /* 
    TheReste restde ofla yourboucle looping codeprincipale
    ...
    */
}

More on this function

Info
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.

Info
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.

Info
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

En apprendre plus

Info
Pourquoi est-ce que la fonction ne retourne des valeurs qu’entre 0 et 1023, alors qu’un unsigned int peut théoriquement avoir des valeurs entre 0 et 65 535 ?

Les lectures analogues sur des micro-contrôleurs sont effectuées par des convertisseurs analogique-numérique (ADC). Celui du CRCduino a une résolution de 10 bits, ce qui veut dire qu’il ne peut mesurer que 1024 divisions de son voltage de référence (maximum). La fonction est donc limitée à 1024 valeur, soit de 0 à 1023.

L’ADC par défaut du Arduino Mega est de 5V, mais un diviseur de tension incorporé au circuit du CRCduino divise la tension lue par 1.5, augmentant ainsi la valeur maximum à 7.5 V.

Info
Quel est la plus petite variation de tension qui peut être lue par GetAnalogInput() ?

L’entrée analogique divise la plage d’entrée de 0 à 7.5 V en 1024 pas. Par conséquent, chaque pas englobe 7.5 V / 1024 = 0.007 V. La résolution de la lecture est de 7 mV, alors une variation de moins de 7 mV ne peut pas être nécessairement détectée. 7 mV, c’est très petit, et amplement suffisant en considérant la précision des composantes et capteurs des robots amateurs.

Info
Rien n’est connecté à la broche que nous lisons, mais la valeur retournée n’est pas 0.

C’est normal : cette valeur fluctue en fonction d’une multitude de facteurs qui, à première vue, ne devraient pas avoir d’impact, comme la proximité d’une main ou la tension de circuits à proximité. Une valeur de 0 est presque uniquement possible à obtenir si la broche SIG est directement connectée à la broche GND.

Articles reliés

Filter by label (Content by label)
sorttitle
excerptTyperich content
cqllabel = "ar-analog_io"