SHED-Article2SOFTWARE - AnalogValue

From ArduinoInfo
Jump to: navigation, search

SHED-Article2SOFTWARE - AnalogValue

This Software Sketch shows how Arduino can read Analog Voltages that result in a range of values from 0 to 1023 as compared to Digital values that are only 0 or 1. Why 1024 values? This is a choice by the ATMEL Engineers who designed the MicroController chip. They decided to create a 10-bit Analog-to-Digital Converter.


"Pin" can be (0 to 5 on Arduino, 0 to 15 on Mega)

analogRead Reads the value from the specified analog pin. The Arduino board contains a 6 channel (8 channels on the Mini and Nano, 16 on the Mega), 10-bit analog to digital converter. This means that it will map input voltages between 0 and 5 volts into integer values between 0 and 1023. This yields a resolution between readings of: 5 volts / 1024 units or, .0049 volts (4.9 mV) per bit.

This example uses a Potentiometer connected as a "Voltage Divider" from +5V to Ground. The "wiper" connection that is changed as the user rotates the pot can go from 0.0V (Ground) to 5.0V

The "wiper" is connected to Analog Pin 0. As the user rotates the pot, the value can go from 0 to 1023. The sketch may also convert that value into the 0 to 5V values it represents.

(Copy the text in the box below and Paste it into a blank Arduino IDE window)
/* SHED Magazine Arduino Sketch: Analog Value - Reads voltage on Analog Pin 0 and displays value - Converts integer value to volts and displays it - SEE the comments after "//" on each line below - CONNECTIONS: - Potentiometer from +5 to Ground, center to pin A0 - V1.01 09/11/12 Questions: */ /*-----( Import needed libraries )-----*/ //none /*-----( Declare Constants and Pin Numbers )-----*/ #define analogPin A0 // In separate group of pins #define ledPin 13 // The onboard LED /*-----( Declare objects )-----*/ //none /*-----( Declare Variables )-----*/ int analogIntValue; // Holds the integer 10 bit value read in float analogVoltsValue; // Value converted to 0..5V void setup() /****** SETUP: RUNS ONCE ******/ { pinMode(ledPin, OUTPUT); Serial.begin(9600); //Start sending to "Serial Monitor" Serial.println("SHED Magazine: Show Analog Value."); }//--(end setup )--- void loop() /****** LOOP: RUNS CONSTANTLY ******/ { /*---( Read and display the 10 bit integer value )------*/ analogIntValue = analogRead(analogPin); //Read value Serial.print("10 Bit Integer VALUE = "); Serial.print(analogIntValue,DEC); // Print value /*---( Convert and display the 0 to 5V value )------*/ analogVoltsValue = analogIntValue * (5.0 / 1024.0); Serial.print(" 0 to 5V VALUE = "); Serial.println(analogVoltsValue, 3); // Print value delay(1000); // Wait 1 second }//--(end main loop )--- /*-----( Declare User-written Functions )-----*/ //none
//*********( THE END )***********