Posted on by & filed under Arduino Tutorials, Electronics, Schools & Curriculum, Software and Apps.

Lesson 3 Hardware:

1. Computer / Laptop or Netbook
2. Arduino Microcontroller
3. USB to Serial Adapter (if your microcontroller does not have a USB port)
4. Appropriate USB cable (Arduino boards draw power from the USB port – no batteries yet)
5. Potentiometer (rotary or linear) Example: DFRobot Rotation Sensor
6. Optional secondary LED.

Open the sample sketch “AnalogInput” under File -> Examples -> Analog. The comments section has been reduced below to make the code clearly visible.

We see several new lines of code here:

int sensorPin = A0;

The “int” is short for “integer”. The name “sensorPin” was chosen only to describe what the variable represents; “sensor pin”. The fact that the ‘P’ is capitalized makes it easier to see that it is actually two words, since spaces cannot be used. The integer “sensor pin” is equal to A0, where “A0″ is Analog pin 0 on the Arduino. On its own, “A0″ is not a reserved term. However, when used in context, the system recognizes it as analog pin 0. The line must be ended with a semicolon. By declaring a variable in the setup, you can use the term, which in this case is “sensorPin”, throughout the code instead of “A0″. There are two main benefits to this:

1) It makes the code more descriptive

2) If you want to change the value of the variable, you only need to do it in one place.

This line uses the term “analogRead” in order to read the voltage of an analog pin. Most Arduino microcontrollers use 10 bit analog (voltage) to digital (numeric) conversion, which is 210 possible numbers = 1024. Therefore a voltage of 0V corresponds to a numeric value of 0. A voltage of 5V corresponds to a numeric value of 1024. Therefore a value of 3V would correspond to a numeric value of:

3/5 =x/1024, x = 3*1024/5 = ~614

Alternatively you could have written: sensorValue = analogRead(A0);

int ledPin = 13;

Once again, the term “ledPin” is not a reserved word in Arduino, it was chosen to describe which pin was connected to the LED. The value “13” is a normal value, but just like “A0″, when used in context represents pin 13.

int sensorValue = 0;

The term “sensorValue” is not a reserved term either.

Connect the potentiometer to pins A0, 5V and GND. The middle (wiper) lead is the one to connect to the analog pin and the voltage varies on this pin. The orientation of the other two pins does not matter. The other option is to connect the potentiometer to pins A0, A1 and A2. However, you will need to add the following code under void setup():

digitalWrite (A1, LOW);

digitalWrite (A2, HIGH);

This sets the corresponding pins to 0V (GND) and 5V (PWR). Once the potentiometer is connected, upload this sketch to the board and change to the Serial monitor. As you rotate the knob (or slide the slider), the values should change between 0 to 1023. Correspondingly, the LED will blink with a faster or shorter delay.

You can now read values and use them within your code. The new function used here is “analogRead();” where the pin selected is pin #2. If you used analog pin #5, you would change the code to read:

int sensorpin = 5;

If the system does not work, check the syntax and ensure the code uploads correctly. Next, check the connections to the potentiometer ensuring that the middle lead goes to the correct pin, and the other pins are powered at 0V and 5V. If you bought a very cheap or old potentiometer, there is a chance it may be mechanically defective. You can test this using a multimeter and connect the ends to the middle pin and an outer pin. Set the multimeter to read resistance and rotate the knob; if the resistance changes slowly, the pot is working. If the resistance is erratic, you need a new potentiometer.

Now what if you want to see the value yourself? Take a look at the code above and write it in the Arduino interface as a new sketch. Some new code you will see is:

Serial.println(sensorValue);

This sends the value contained in the variable “sensorValue” serially via the USB plug and digital pin 1. Verify, then upload this sketch to your Arduino. Once it is done, press on the “magnifying glass” located towards the top right of the window. This is the “Serial monitor” and monitors communications being sent and received by the Arduino. Here you must verify that the Baud rate is also 9600, or else you will see garbage.

If you did not want the values to appear on a new line every time, you could write

Serial.print(sensorValue);

### 7 Responses to “Arduino 5 Minute Tutorials: Lesson 3 – Potentiometer”

1. #### Laurence

I thank the robotshop team for these great lessons, I would like to ask, why isn’t the analog pin declared as an input in “void setup () {}”. I’m just new about everything here with 0 background to robotics and just starting my way in.

• #### Coleman Benson

@Laurence Once you select the board type in the Arduino IDE, it “knows” which pins do what, so you don’t need to declare all pin types.

2. #### Laurence

Thank you for the quick response! This website is very helpful to starters like me I still don’t have a microcontroller by now (can’t experiment), so I would like to know what will happen if I didn’t declare pin 13 as an output, would it still work considering I selected the board type in Arduino IDE? I would also like to know the release date of Lesson 8 – Actuator: DC Motor. Thank you again! 😀 I will definitely recommend this site to my friends who would like to know about robotics too!

• #### Coleman Benson

@ Laurence Yes, pin 13 does not need to be declared; to use it as an output, you might use the command “digitalwrite”. The lesson regarding actuators as part of the Arduino series is actually lesson 5.

3. #### Aïcha

Hello , I need your help. I did a programme and my elec teacher tell me to add a potentiometer on my arduino card so I need a programme to add to my programme. Please help me

• #### Coleman Benson

@Aïcha There is sample code within this lesson on how to use a potentiometer.