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 theSIG
pin (it is shorted to aGND
pin).1 to 1022
, proportionally to the applied voltage.1023
, if 7.5V is read on theSIG
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 typeunsigned char
.
Overloads
This function does not have any overloads. It can only be used as described by the main prototype.
Examples
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
-
CrcDuino Hardware Details (Système CrcDuino System) —
This page details the different physical components found on the CrcDuino. Although quite dense in information, it’s a must read!
All voltage values given on this page are using a
GND
pin as reference. AllGND
pins are connected to one another, so using any of them in all situation is acceptable.Page table of content
-
CrcLib: How to access IO pins (Système CrcDuino System) —
This page presents how to access the CrcDuino IO pins using CrcLib.
Pins
-
Debugging - CrcLib Error Codes (Système CrcDuino System) —
What is it?
CrcLib is equipped with a functionality that helps troubleshooting common programming mistakes that cannot be recognize by the compiler before loading the code into the micro-controller.
As your program is executed by the CrcDuino, CrcLib actively monitors the code execution and falls into Error mode if a CrcLib component is incorrectly used.
-
GetAnalogInput() (Système CrcDuino System) —
Purpose
Get the value of an Analog Input (AI) of the board.
When to use
0 Comments