Tutorial 21: Analog Input

Arduino Course for Absolute Beginners

Analog Input

Using a microcontroller can be a lot like fishing – you throw your sensor out in the world and then check your line every so often. Sometimes you get just the fish you are looking for – but what will you do with it once you have it? Will you keep it or release it? Will you pan fry it or eat some sashimi?

The same questions are involved in the data you capture with your microcontroller. What will you do with the data once you have it?

In the last lesson, we used input data to control the intensity of an output, in this example will use the input data to control the timing of an output.

To demonstrate this, we use a potentiometer attached to analog pin A0 and an LED attached to digital pin 13. We use the input from the analog pin to adjust the timing delay of a blinking LED.


If you like this tutorial, click here to check out FREE Video Arduino course – thousands of people have really enjoyed it.

You Will Need

  1. LED (1)
  2. 220-Ohm Resistor (1)
  3. Potentiometer (1) any resistance
  4. Jumper Wires (3)
  5. Alligator Clip (1)

Step-by-Step Instructions

  1. Place the potentiometer into the breadboard.
  2. Run a jumper wire from the 5-volt pin of the Arduino to either one of the outside pins of the potentiometer.
  3. Run another jumper wire from one of the ground pins on the Arduino (labeled GND) to the other outside pin of the potentiometer.
  4. Run the final jumper wire from pin A0 on the Arduino to the middle pin of the potentiometer.
  5. Place one end of the 220-ohm resistor into pin 13.
  6. Place the short leg of the LED into the ground pin.
  7. Connect the long leg of the LED to the other end of the resistor with the alligator clip.
  8. Plug the Arduino into your computer.
  9. Open the Arduino IDE.
  10. Open the sketch for this section.
  11. Click the Verify button on the top left side of the screen. It will turn orange and then back to blue once it has finished.
  12. Click the Upload button (next to the Verify button). It will turn orange and then back to blue once it has finished.
  13. Now adjust the knob of the potentiometer and watch the LED. The knob should adjust the on/off timing of the LED.

Analog Input Arduino Board

Image created by Fritzing.

The Arduino Code

/*
  Analog Input
 Demonstrates analog input by reading an analog sensor on analog pin 0 and
 turning on and off a light emitting diode(LED)  connected to digital pin 13.
 The amount of time the LED will be on and off depends on
 the value obtained by analogRead().

 The circuit:
 * Potentiometer attached to analog input 0
 * center pin of the potentiometer to the analog pin
 * one side pin (either one) to ground
 * the other side pin to +5V
 * LED anode (long leg) attached to digital output 13
 * LED cathode (short leg) attached to ground

 * Note: because most Arduinos have a built-in LED attached
 to pin 13 on the board, the LED is optional.


 Created by David Cuartielles
 modified 30 Aug 2011
 By Tom Igoe

 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/AnalogInput

 */

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);
}

Discuss the Sketch

This sketch is fun and easy, but at the same time helps you think of creative ways to use an analog input. First we declare and initialize variables for two pins and a variable to hold the data we collect from the sensor (i.e., in this case, the potentiometer is our “sensor”):

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

All the variables are simple integers. Note that the programmer chose not to use the constant qualifier for these pin assignments – it is by no means a requirement, but it is a good practice.

In the setup() of this sketch we need to make sure the pin used to brighten the LED is set as an output:

void setup() {

  // declare the ledPin as an OUTPUT:

  pinMode(ledPin, OUTPUT);

}

Once the setup() is complete we begin with the loop(). This loop() is rather easy. We start by collecting a sample from the analog pin using the analogRead() function. We assign the value we get from analogRead() to the sensorValue variable.

// read the value from the sensor:

sensorValue = analogRead(sensorPin);

Because we want the LED to blink we need to turn it on, to do that all we need is to write the pin as HIGH:

// turn the ledPin on

digitalWrite(ledPin, HIGH);

This applies 5 volts to pin 13, allowing current to flow through the LED which turns it on. Now comes the timing control piece – we want the input from the analog pin to determine the interval the LED stays on and off.

To do this, we delay() the sketch using the sensorValue variable:

// stop the program for <sensorValue> milliseconds:

delay(sensorValue);

Now the LED is bright for the number of milliseconds that corresponds to the potentiometer’s adjustment. Recall that the highest value which analogRead() returns is 1023 and the lowest is 0. There are a thousand milliseconds in a second, therefore, the longest amount of time the LED will be lit is slightly over one second.

The next logical step is to have the LED turn off – we are trying to blink it after all – again we employ the digitalWrite() function, but set the voltage LOW:

// turn the ledPin off:

digitalWrite(ledPin, LOW);

The final step in the program is to hold the LED in the off state for the same period it was on. We pull out the delay() function again and feed it the same sensor value as before:

// stop the program for for <sensorValue> milliseconds:

delay(sensorValue);

Once the loop() finishes it starts at the top and goes over and over. Here are the basic steps of the program:

  1. Sample data at the analog pin and assign it to a variable
  2. Turn the LED on
  3. Delay the sketch for a specified time
  4. Turn the LED off
  5. Delay the sketch for the same specified time

Keep in mind that since we employ the delay() function to control the timing, all the input coming from the potentiometer is halted while the sketch is delayed.

Try On Your Own

  • Adjust the delay time so that the LED off interval is half that of the LED on interval.
  • Add an LED at pin 12 and make it blink the exact opposite as the LED at pin 13.

Further Reading

installing Arduino libraries

Installing Arduino Libraries | Beginners Guide

IoT sewage project

Pumping poo! An IoT sewage project

ESP32 webOTA updates

How to update ESP32 firmware using web OTA [Guide + Code]

error message Brackets Thumbnail V1

expected declaration before ‘}’ token [SOLVED]

Compilation SOLVED | 1

Compilation error: expected ‘;’ before [SOLVED]

Learn how to structure your code