I am starting to learn how to work with and program my Arduino Mega 2560.
I am writing a program that prints out to the serial the resistance and voltage value from a 10k ohm potentiometer with one side connected to the 5v pin, the other side connected to the ground pin, and the wiper connected to the A0 pin on the Arduino.
I want to print out to the serial both the resistance value that the potentiometer is currently on and the voltage value due to the resistance that the potentiometer is currently on.
Below is my code so far. Am I correct in the way I am finding and printing the resistance and voltage values to the serial?
Thanks.
Stephen
Code:
/*
Potentiometer Voltage Reader
*/
void setup()
{
//initialize the serial communication at 9600 bits per second
Serial.begin(9600);
}
void loop()
{
/*
Create a variable to hold the sensor value at A0
on the Arduino board
*/
int sensorValue = analogRead(A0);
/*
Print out the resistance value of the potentimeter /
Serial.println('R:');
Serial.println(sensorValue);
Serial.println('\n');
/
Create a float variable to hold the voltage
from the sensor value from 0 to 1023
to a voltage value from 0 to 3.3V
*/
float voltage = sensorValue * (3.3/1023.0);
//Print out the voltage value
Serial.println('V:');
Serial.println(voltage);
Serial.println('\n');
}
Your resistance is 10k (10000 ohms) not 1024 ohms. And your voltage is a fraction of Vcc which is around 5V, not 3.3V. Actual Vcc can vary, so if you have a multimeter measure it and use that value in your calculation.
(Also, when posting, the # button wraps your code in code tags which makes it easier for other people to read).
I had to use the 3.3v pin as when I connected the potentiometer to the 5v pin the Arduino shut off. So I am assuming that the usb does not supply enough voltage. Also from the Arduino tutorial on http://arduino.cc/en/Tutorial/ReadAnalogVoltage shows that when using a 10k ohm potentiometer you would use 1023 to divide the voltage by.
StephenD420:
I had to use the 3.3v pin as when I connected the potentiometer to the 5v pin the Arduino shut off. So I am assuming that the usb does not supply enough voltage.
That's bad. If the potentiometer is shorting out the connection, connecting it to 3.3V will keep the Arduino alive but it will overload/burn out the 3.3V regulator. Pretty soon your 3.3V could stop working.
When bad/unexpected things happen with electronics you should try to find out why.
StephenD420:
yea but I have no idea why when I connect the potentiometer to the 5v pin the Arduino shuts off. I got my Arduino already put together from SainSmart.
Got any ideas?
Thanks.
Stephen
Most likely incorrectly wired up pot. It's possible if you have wired ground or +5vdc to the wiper terminal instead of to the two end terminals, that the pot will 'short out' the power source when rotated in the wrong direction.
StephenD420:
yea but I have no idea why when I connect the potentiometer to the 5v pin the Arduino shuts off. I got my Arduino already put together from SainSmart.
Get a multimeter and measure the resistances. See if they're what they're supposed to be. You should have 10k between the two outside pins (which are the ones you connect to +5V and ground).
I went and got a new potentiometer and hooked it up to the 5v on one side, ground on the other and the wiper to the A0 pin. AND IT WORKED!!! It did not shut off my Arduino. So it was a bad potentiometer.
Now I have a quick question about my code.From the Arduino tutorial on http://arduino.cc/en/Tutorial/ReadAnalogVoltage it shows that when using a 10k ohm potentiometer you would use 1023 to divide the voltage by.
int sensorValue = analogRead(A0);
float voltage = sensorValue * (5.0/1023.0);
The value of the pot is immaterial in the calculation pretty much, because it is wired as a potential divider.
The 1023 represents the number of steps in the ADC because it is a ten bit device.