The signal present around us is always analog in nature but micro-controllers can only understand it digitally. Therefore it becomes necessary to convert these analog values to digital. Apart from this the ATmega chip on Arduino consists of inbuild analog to digital converter (ADC) which can be accessed using the software. So if we connect an analog input device like potentiometer with Arduino, we can easily read the analog voltage signal using this function called analogRead( ). Here we will use this potentiometer to manually adjust the brightness of an LED.

1. CONCEPTS

It is important to note that arduino uses a default 10-bit (2^10=1024) analog to digital converter. This means it can map the operating voltages between 0 to 5V/3V into integer values between 0 to 1023. But this resolution can be changed using analogRefrence() function.

Figure 1 shows the that by default Arduino uses 10-bit resolution (1024 steps) for Input signal whereas 8-bit resolution for (256 steps) an output signal. So we have to divide the input values by a factor of 4 (1024/256=4) in order to write them on the analog output pin.

Comparison between analog Input and output resolutions in arduino
Figure 1: Comparison between analog Input and output resolutions in Arduino

Potentiometer

A potentiometer is a simple electromechanical transducer which converts the rotary or linear movement into a change of resistance. As a result, you can use it to control anything from the volume of a hi-fi system to the direction of a huge container ship.

It consists of three terminals as shown in Figure 2. terminal 1 and 3 are located at the ends of resistance on which a sliding contact connected to terminal 2 can be moved. When the contact is completely turned counterclockwise, the current that enters from 1, does not travel any stretch of the resistance, as it immediately exits terminal 2 (zero resistance). The opposite case when the contact is turned completely clockwise, as the current entering from 1 must travel all the resistance before leaving terminal 2 (maximum resistance).
Obviously, between the two extremes, all intermediate resistance values ​​are possible.

Cross Section of Potentiometer. How it Works. Potentiometer with arduino analogread. Reading analog voltage and writing it on LED
Figure 2. Cross Section of a Potentiometer

2. COMPONENTS

3. CONNECTIONS

Potentiometer with arduino analogread. Reading analog voltage and writing it on LED
Potentiometer with arduino analogread. Reading analog voltage and writing it on LED

Figure 3: Connecting a Potentiometer and an LED with Arduino

4. PROGRAM

Program 1: Use potentiometer with Arduino to vary the brightness of an LED.

/*UNCIA ROBOTICS | www.unciarobotics.com
  PROGRAM: Arduino with Potentiometer
  Read the analog signal from potentiometer and write
  it on the LED

  Connections:
  9      LED
  A0     Potentiometer
*/

const int potPin = A0;        // potentiometer pin
const int ledPin = 9;         // LED pin

void setup() {
}

void loop() {
  int reading = analogRead(A0);   //read the PIN

  //divide reading by 4 to match the IP/OP resolution
  analogWrite(ledPin, reading / 4);
}

Program 2: Use potentiometer with arduino to vary the blinking Speed of an LED.

/*UNCIA ROBOTICS | www.unciarobotics.com
  PROGRAM: Arduino with Potentiometer (LED Blinking)
  Use potentiometer to vary the blinking speed of an LED

  Connections:
  9      LED
  A0     Potentiometer
*/

const int potPin = A0;        // potentiometer pin
const int ledPin = 9;         // LED pin

void setup() {
  pinMode(ledPin, OUTPUT);
}

void loop() {
  int reading = analogRead(A0);   //read the PIN

  digitalWrite(ledPin, HIGH);
  delay(reading);
  digitalWrite(ledPin, LOW);
  delay(reading);
}

5. FUNCTIONS USED


const

const is an abbreviation for the word constant. Also, it’s a Variable qualifier which makes any variable read-only. You can use it like any other variable but you cannot change its value in the program. For the most part, it’s a good practice to name your pins using a constant qualifier.


pinMode( )

Description: Configure specific pin to behave either as input or output.

Syntax: pinMode (pin,MODE);

  • pin: the Arduino pin number to set the mode of.
  • MODE: INPUT, OUTPUT, or INPUT_PULLUP.

Returns: Nothing

Notes and Warnings:

  • It is only applicable to digital pins.

analogRead( )

Description: Reads the value from a specific analog Pin. Arduino boards contain multichannel ADC (Analog to Digital) converter. Furthermore, It has 10 bits of default resolution. For this reason, if you provide 5 Volts of power supply it can divide it into 1024 steps of 0.004.9V or 49 mV each. Below is the list of analog pins in case of Arduino, Nano and Mini.

Board PWM PinsMaximum Resolution
Arduino Uno A0, A1, A2, A3, A4, A510 Bit
Mini, NanoA0, A1, A2, A3, A4, A5, A6, A7 10 Bit

Syntax: analogRead (pin);

  • pin: the Arduino PIN number ( A0 to A5). | Allowed data types: int

Returns: Analog reading on the pin | Allowed data type: int

Notes and Warnings:

  • If the analog input pin is not connected to anything it will return random values.

analogWrite( )

Description: Writes an analog value (PWM) to the pin. Therefore you can use it to change the brightness of an LED or vary the speed of the motor. Whenever analogWrite function is called, the PWM PIN generates a rectangular wave of the specified duty cycle.

PWM PinsFrequency
3, 5, 6, 9, 10, 11490 Hz (pins 5 and 6: 980 Hz)

Syntax: digitalWrite (pin,VALUE);

  • pin: the Arduino PIN number. | Allowed data types: int
  • value: a range of values between 0 (Lowest voltage) to 255(Highest Voltage). | Allowed data types: int

Returns: Nothing

Notes and Warnings:

  • The output resolution for analogWrite() lies between 0 to 255. In particular, 0 stands for minimum Volt and 255 stands for maximum voltage.
  • Above all there is no need to set pinMode() before using analogWrite( ). Also the analogWrite( ) function has nothing to with analog pins or analogRead( ).
  • Pin 5 and 6 can have a higher-than-expected duty cycle because of delay() and millis( ) as they share the same timer which is used to generate PWM outputs.

delay( )

Description: Pauses the program for the amount of time (in milliseconds) as specified inside the bracket. (1sec = 1000 ms).

Syntax: delay (ms);

  • ms: the number of milliseconds to pause. Allowed data types: unsigned long

Notes and Warnings:

  • While it is easy to use delay function for short pauses in a program, the use of delay() has significant drawbacks. No other reading of sensors, mathematical calculations, or pin manipulation can go on during the delay function, so in effect, it brings most other activity to a halt.
  • Certain things do go on while the delay function is controlling the Atmega chip, however, because the delay function does not disable interrupts. Serial communication that appears at the RX pin is recorded, PWM (analogWrite) values and pin states are maintained.

6. FAQ’s

Can we use any other value for Potentiometer other than 10K?

Yes, you can use 10K, 50K, 100K, 500K, etc. It really doesn’t matter when you connect a potentiometer with analog input pin of Arduino.

How to locate positive, negative and output pins of a potentiometer?

Basically, there are two categories of Potentiometers that are available in the Market; Linear and Rotary. Potentiometers do not have polarity and therefore you can connect the two ends either way you want, just make sure that you identify the output pin first. The figure below shows Linear and Rotary Potentiometers along with their pin description.

Linear and Rotary Potentiometer
Figure : Linear and Rotary Potentiometers with Power Pins(+/-) and Output pin
Still, having doubts?

Ask your questions in comment section below or Contact us.

Leave a Reply