## Analog Read Serial

### Hardware Required

• 10-kilohm Potentiometer

• Hook-up wire

### Relevant Groundwork

By turning the shaft of the potentiometer, you change the amount of resistance on either side of the wiper which is connected to the center pin of the potentiometer. This changes the voltage at the center pin. When the resistance between the center and the side connected to ~3 volts is close to zero (and the resistance on the other side is close to 10 kilohms), the voltage at the center pin nears 3 volts. When the resistances are reversed, the voltage at the center pin nears 0 volts, or ground. This voltage is the analog voltage that you’re reading as an input.

The LaunchPad has a circuit inside called an analog-to-digital converter that reads this changing voltage and converts it to a number between 0 and 1023. When the shaft is turned all the way in one direction, there are 0 volts going to the pin, and the input value is 0. When the shaft is turned all the way in the opposite direction, there are 3 volts going to the pin and the input value is 1023. In between, `analogRead()` returns a number between 0 and 1023 that is proportional to the amount of voltage being applied to the pin.

By turning the shaft of the potentiometer, you change the amount of resistance on either side of the wiper which is connected to the center pin of the potentiometer. This changes the voltage at the center pin. When the resistance between the center and the side connected to 3 volts is close to zero (and the resistance on the other side is close to 10 kilohms), the voltage at the center pin nears 3 volts. When the resistances are reversed, the voltage at the center pin nears 0 volts, or ground. This voltage is the analog voltage that you’re reading as an input.

The LaunchPad has a circuit inside called an analog-to-digital converter that reads this changing voltage and converts it to a number between 0 and 1023. When the shaft is turned all the way in one direction, there are 0 volts going to the pin, and the input value is 0. When the shaft is turned all the way in the opposite direction, there are 3 volts going to the pin and the input value is 1023. In between, analogRead() returns a number between 0 and 1023 that is proportional to the amount of voltage being applied to the pin.

### Circuit Image developed using Fritzing. For more circuit examples, see the Fritzing project page.

### Schematic ### Code Explanation

In the program below, the only thing that you do will in the setup function is to begin serial communications, at 9600 bits of data per second, between your LaunchPad and your computer with the command:

`Serial.begin(9600);`

Next, in the main loop of your code, you need to establish a variable to store the resistance value (which will be between 0 and 1023, perfect for an int datatype) coming in from your potentiometer connected to Analog Pin 3:

`int sensorValue = analogRead(A3);`

Finally, you need to print this information to your serial window as a decimal (`DEC`) value. You can do this with the command Serial.println() in your last line of code:

`Serial.println(sensorValue, DEC)`

Now, when you open your Serial Monitor in the Energia development environment (by clicking the button directly to the right of the ''Upload'' button in the header of the program), you should see a steady stream of numbers ranging from 0-1023, correlating to the position of the pot. As you turn your potentiometer, these numbers will respond almost instantly.

### Code

```/*
Reads an analog input on pin A3, prints the result to the serial monitor.
Attach the center pin of a potentiometer to pin A3, and the outside pins to ~3V and ground.

This example code is in the public domain.
*/

// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin A3:
int sensorValue = analogRead(A3);
// print out the value you read:
Serial.println(sensorValue);
delay(1); // delay in between reads for stability
}```

### Try it out

• Read all the analog pins and verify they behave properly.