Analog to Digital Conversion

Pages
Contributors: Nate
Favorited Favorite 34

Arduino ADC Example

To show this in the real world let’s use the Arduino to detect an analog voltage. Use a trimpot, or light sensor, or simple voltage divider to create a voltage. Let’s setup a simple trimpot circuit for this example:

alt text

To start, we need to define the pin as an input. To match the circuit diagram we will use A3:

pinMode(A3, INPUT);

and then do the analog to digital version by using the analogRead() command:

int x = analogRead(A3); //Reads the analog value on pin A3 into x

The value that is returned and stored in x will be a value from 0 to 1023. The Arduino has a 10-bit ADC (210 = 1024). We store this value into an int because x is bigger (10 bits) than what a byte can hold (8 bits).

Let’s print this value to watch it as it changes:

Serial.print(“Analog value: “);
Serial.println(x);

As we change the analog value, x should also change. For example, if x is reported to be 334, and we’re using the Arduino at 5V, what is the actual voltage? Pull out your digital multimeter and check the actual voltage. It should be approximately 1.63V. Congratulations! You have just created your own digital multimeter with an Arduino!