03: Analog Input

    Analog Input Functions

    Analog input is a way to read a voltage at one of the Arduino analog input pins. One thing that differentiates analog input from the other I/O mechanisms is that you do not need to set the pin direction. Analog input pins are always used exclusively for analog input.


    Analog input on an Arduino is done using the analogRead() function. analogRead() takes a single parameter, the analog pint number, and returns an int that is proportional to the voltage at the pin. The int returned will always be in the range 0 to 1023. It is important to know what the default reference voltage is for the particular Arduino hardware being used is.

    NOTE: For Arduino Unos and Megas, the reference voltage is 5V. For Arduino Dues and some other models, the reference voltage is 3.3V. Make sure to check what your reference voltage is prior to using analogRead(), or your code might behave in unexpected ways!

    Example Code:

    reference to undefined name 'syntax' Exception of type 'MindTouch.Deki.Script.Runtime.DekiScriptUndefinedNameException' was thrown. (click for details)


    The map() function can shift a value in a given range into a value in a different range.  If, for example, you wanted to read the voltage, in mV, rather than the normalized range given, you could write:

    reference to undefined name 'syntax' Exception of type 'MindTouch.Deki.Script.Runtime.DekiScriptUndefinedNameException' was thrown. (click for details)

    analogRead() Example

    We will extend the example used in Digital Input by replacing the button with a potentiometer which will control the rate at which the LED blinks. A potentiometer is also called a variable resistor. Like a resistor, there are two pins for each end. Unlike a resistor, there is a third pin connected to a wiper that slides across the internal resistor. This wiper effectively shorts out part of the resistor, allowing the resistence to change as the wiper moves back and forth.



    These schematics are attached and can be viewed with Fritzing.

    Blinking Light with Variable Rate Arduino Sketch

    reference to undefined name 'syntax' Exception of type 'MindTouch.Deki.Script.Runtime.DekiScriptUndefinedNameException' was thrown. (click for details)

    This code can be downloaded here.

    Tag page (Edit tags)
    • No tags
    Pages that link here
    Page statistics
    4469 view(s), 8 edit(s) and 5749 character(s)


    You must login to post a comment.