Tutorial.ReadAnalogVoltage History

Hide minor edits - Show changes to markup

February 02, 2014, at 11:41 AM by Scott Fitzgerald -
Changed lines 50-51 from:

float voltage= sensorValue * (5.0 / 1024.0);

to:

float voltage= sensorValue * (5.0 / 1023.0);

January 31, 2013, at 11:54 AM by Roberto Guido - minor typo in example code, thanks to Nicolas Charvin for alert
Changed lines 50-51 from:

float voltage= sensorValue * (5.0 / 1023.0);

to:

float voltage= sensorValue * (5.0 / 1024.0);

May 02, 2012, at 03:35 AM by Scott Fitzgerald -
Changed line 59 from:
to:
April 16, 2012, at 07:41 PM by Scott Fitzgerald -
Changed lines 54-55 from:

Serial.println(voltage, DEC)

to:

Serial.println(voltage)

April 16, 2012, at 07:31 PM by Scott Fitzgerald -
Changed lines 66-68 from:
to:
Changed lines 69-70 from:
to:
April 16, 2012, at 07:29 PM by Scott Fitzgerald -
Changed lines 14-16 from:
  • breadboard
  • hook-up wire
to:
Changed lines 19-22 from:

Attach: AnalogReadSerial_BB.png

to:

April 16, 2012, at 07:29 PM by Scott Fitzgerald -
Changed lines 50-53 from:

To change the values from 0-1023 to a range that corresponds to the voltage the pin is reading, you'll need to create another variable, a float, and use the map function. Map takes the value you stored in sensorValue and scales it between 0.0. and 5.0 :

float voltage=map(sensorVal, 0, 1023, 0.0, 5.0);

to:

To change the values from 0-1023 to a range that corresponds to the voltage the pin is reading, you'll need to create another variable, a float, and do a little math. To scale the numbers between 0.0 and 5.0, divide 5.0 by 1023.0 and multiply that by sensorValue :

float voltage= sensorValue * (5.0 / 1023.0);

April 16, 2012, at 07:26 PM by Scott Fitzgerald -
Changed lines 21-24 from:

Attach: ReadAnalogVoltage.png

to:

Attach: AnalogReadSerial_BB.png

Changed lines 27-32 from:

Connect three wires to the Arduino board. The first two, red and black, connect to the two long vertical rows on the side of the breadboard to provide access to the 5 volt supply and ground. The third wire goes from digital pin 2 to one leg of the pushbutton. That same leg of the button connects through a pull-down resistor (here 10 KOhms) to ground. The other leg of the button connects to the 5 volt supply.

Pushbuttons or switches connect two points in a circuit when you press them. When the pushbutton is open (unpressed) there is no connection between the two legs of the pushbutton, so the pin is connected to ground (through the pull-down resistor) and reads as LOW, or 0. When the button is closed (pressed), it makes a connection between its two legs, connecting the pin to 5 volts, so that the pin reads as HIGH, or 1.

If you disconnect the digital i/o pin from everything, the LED may blink erratically. This is because the input is "floating" - that is, it doesn't have a solid connection to voltage or ground, and it will randomly return either HIGH or LOW. That's why you need a pull-down resistor in the circuit.

to:

Connect the three wires from the potentiometer to your Arduino board. The first goes to ground from one of the outer pins of the potentiometer. The second goes from 5 volts to the other outer pin of the potentiometer. The third goes from analog input 2 to the middle pin of the potentiometer.

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 5 volts is close to zero (and the resistance on the other side is close to 10 kilohms), the voltage at the center pin nears 5 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 Arduino 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 5 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.

Changed line 37 from:
to:
Changed lines 46-62 from:

Next, initialize digital pin 2, the pin that will read the output from your button, as an input:

pinMode(2,INPUT);

Now that your setup has been completed, move into the main loop of your code. When your button is pressed, 5 volts will freely flow through your circuit, and when it is not pressed, the input pin will be connected to ground through the 10-kilohm resistor. This is a digital input, meaning that the switch can only be in either an on state (seen by your Arduino as a "1", or HIGH) or an off state (seen by your Arduino as a "0", or LOW), with nothing in between.

The first thing you need to do in the main loop of your program is to establish a variable to hold the information coming in from your switch. Since the information coming in from the switch will be either a "1" or a "0", you can use an int datatype. Call this variable sensorValue, and set it to equal whatever is being read on digital pin 2. You can accomplish all this with just one line of code:

int sensorValue = digitalRead(2);

Once the Arduino has read the input, make it print this information back to the computer as a decimal value. You can do this with the command Serial.println() in our last line of code:

Serial.println(sensorValue);

Now, when you open your Serial Monitor in the Arduino environment, you will see a stream of "0"s if your switch is open, or "1"s if your switch is closed.

to:

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:

int sensorValue = analogRead(A0);

To change the values from 0-1023 to a range that corresponds to the voltage the pin is reading, you'll need to create another variable, a float, and use the map function. Map takes the value you stored in sensorValue and scales it between 0.0. and 5.0 :

float voltage=map(sensorVal, 0, 1023, 0.0, 5.0);

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

Serial.println(voltage, DEC)

Now, when you open your Serial Monitor in the Arduino 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.0 - 5.0. As you turn the pot, the values will change, corresponding to the voltage coming into pin A0.

Changed line 75 from:
to:
April 16, 2012, at 06:12 PM by Scott Fitzgerald -
Changed line 64 from:
to:
April 16, 2012, at 06:11 PM by Scott Fitzgerald -
Added lines 1-78:

Share