Learn to code

ARDUINO & CODES

Previous Post
Reading a 4 x 4 keypad with Arduino
Next Post
Using the map function for analog results with Arduino

Reading analog values with Arduino

in Arduino Digital / Analog

In this tutorial, we will use the analogRead() function to read the voltage on a potentiometer.  All Arduino Boards have a 10-bit analog to the digital converter. This means that it will map input voltages between 0 and the operating voltage (5V or 3.3V) into integers between 0 and 1023. On the Arduino Uno Board that we will use, for example, we will use 5volts. We will then use this reading to blink a LED.

Parts you will need

Arduino Uno Rev3 Arduino Uno Rev3 × 1
Dupont Wires Dupont Wires × 1
10K Potentiometer 10K Potentiometer × 7
220 ohm resistor 220 ohm resistor × 1

Arduinoplatform is a participant in several affiliate programs. This means that I will earn a commision if you buy a product from the affiliated websites through clicking on the links provided above.

The Potentiometer

This tutorial will use a potentiometer. As explained in the introduction analogRead() converts the input voltage range, 0 – 5 volts, to a digital value. The input voltage in this tutorial is controlled by the potentiometer.

By turning the shaft of the potentiometer, you can change the amount of resistance on either side of the center pin. You can see potentiometers as variable resistors. Their resistance changes by turning a knob, dial or shaft. A potentiometer has a range of resistance. For example, the potentiometer that we are using, 10Ω can be adjusted from 0Ω to its maximum of 10KΩ by using the shaft.

When the shaft is turned all the way in one direction, there is no resistance between the center pin and the pin connected to ground. The voltage of the center pin will be 0. The code, analogread() will return 0. When the shaft is turned all the way in the other direction, there will be less resistance, there will be no resistance between the center pin and the pin connected to the 5v. The voltage will be 5v and thus analogread() returns the digital value of 1023. In between, the analogread() function will return a number between 0 and 1023 that is proportional to the amount of voltage that is regulated in the center pin by turning the shaft.

BreadBoard Layout

This tutorial will use a potentiometer. As explained in the introduction analogRead() converts the input voltage range, 0 – 5 volts, to a digital value. The input voltage in this tutorial is controlled by the potentiometer.

By turning the shaft of the potentiometer, you can change the amount of resistance on either side of the center pin. You can see potentiometers as variable resistors. Their resistance changes by turning a knob, dial or shaft. A potentiometer has a range of resistance. For example, the potentiometer that we are using, 10Ω can be adjusted from 0Ω to its maximum of 10KΩ by using the shaft.

When the shaft is turned all the way in one direction, there is no resistance between the center pin and the pin connected to ground. The voltage of the center pin will be 0. The code, analogread() will return 0. When the shaft is turned all the way in the other direction, there will be less resistance, there will be no resistance between the center pin and the pin connected to the 5v. The voltage will be 5v and thus analogread() returns the digital value of 1023. In between, the analogread() function will return a number between 0 and 1023 that is proportional to the amount of voltage that is regulated in the center pin by turning the shaft.

The diagram above consists is very simple.  We will use the power rails to connect the GND and 5V to the potentiometer and the LED.

We will connect four wires from the Arduino to the breadboard, potentiometer and LED. The first goes from GND to the negative rail of the breadboard. The second goes from 5v to the positive rail of the breadboard. The third one goes from analog A0 to the center pin of the potentiometer. The last one goes from pin13 to the longer leg of the LED (the positive leg). We will connect one of the outer pins of the potentiometer with the GND rail and one with the 5v rail. The last thing to connect is the shorter leg of the LED. We will use a 220-ohm resistor to connect it with the GND rail of the breadboard.

You are done and ready to upload the code.

The Code

// sketch: analogRead() control blink with potentiometer
  
int sensorPin = A0;    // select the input pin for the potentiometer
int ledPin = 13;      // select the pin for the LED
int sensorValue = 0;  // variable to store the value coming from the sensor
 
void setup() {
  // declare the ledPin as an OUTPUT:
  pinMode(ledPin, OUTPUT);
}
 
void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);
  // turn the ledPin on
  digitalWrite(ledPin, HIGH);
  // stop the program for sensorValue milliseconds:
  delay(sensorValue);
  // turn the ledPin off:
  digitalWrite(ledPin, LOW);
  // stop the program for for sensorValue milliseconds:
  delay(sensorValue);
}

 

Code Explanation

This code is quite easy to write. First, we declare two variables and a variable to hold the value of the potentiometer. We will use the data type int since the value of the potentiometer will be a digital number.

int sensorPin = A0;    // select the input pin for the potentiometer
int ledPin = 13;      // select the pin for the LED
int sensorValue = 0;  // variable to store the value coming from the sensor

Remember, to set the ledPin to output in the void setup() part of our sketch.

pinMode(ledPin, OUTPUT);

In the void loop() part of our sketch, we are using the analogRead function. Firstly, we are going to read the value of the analog input. This will return a value between 0 and 1023 depending on the position of the potentiometer shaft. Secondly, we will send a HIGH signal to the ledPin to turn the LED on. The next thing is interesting, the reading of the potentiometer is used for deciding how long the delay should be before turning the LED off. After the LED is turned off we add another delay before the loop starts all over again by turning the LED on.

 
// read the value from the sensor:
  sensorValue = analogRead(sensorPin);
  // turn the ledPin on
  digitalWrite(ledPin, HIGH);
  // stop the program for sensorValue milliseconds:
  delay(sensorValue);
  // turn the ledPin off:
  digitalWrite(ledPin, LOW);
  // stop the program for for sensorValue milliseconds:
  delay(sensorValue);

After uploading the code, you can adjust the speed of blinking by turning the shaft of the potentiometer. In an upcoming tutorial, Arduino Tutorial: 5.2 Adjust the brightness of an LED with PWM, we will a technique called PWM to control the brightness of the LED.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu