Last Updated: 03/09/2021

Functions Lesson 3 - analogRead()

Functions >> Functions Lesson 3 - analogRead()

Lesson Contents

Reading from Analog pins using analogRead()
Converting the returned value to a voltage
Problems with analog pins floating values
Dangers with analog pins

WARNING

DO NOT EXCEED THE VOLTAGE INPUT LIMITS FOR YOUR BOARD

Maximum Voltage Arduino UNO/nano/Mega 2560 5v DC

Maximum Voltage Arduino Due 3.3v DC

Example 1: analogReadv1.ino

Click to Download code:analogReadv1.ino

The diagram below shows how to attach a Potentiometer (I used 1k but 5k or 10k will work). If you do not have a potentiometer you can still do the lesson by putting a wire between A5 and the 5v pin as explained in the video.
potentiometer and Arduino analogRead()

 
/* 03/09/2021

    analogReadv1

    Board used Arduino UNO

    Basics of reading from an analogue pin

    Connect pin A5 to 5v, then try connecting to 3.3v, then to Gnd to see the values change

    Previous lessons used in this sketch:
    lesson 2 "Hello World" Basics of printing to Serial Window http://www.digitaltown.co.uk/lesson2helloworld.php
    Lesson 6 boolean, byte, int, long, unsigned int, unsigned long http://www.digitaltown.co.uk/lesson6bitsandbytes.php 

*/
//Variables
//The variable that will store the returned value
int readPin;

//A const int with the pin number
const int analogPin = A5;



void setup() {
  Serial.begin(9600);
  Serial.println("analogReadv1");
  Serial.println(" ");

}

void loop() {
  //Reads the voltage on the in steps of 0-1023
  //5v on UNO gives 1023
  //3.3v on UNO gives 681/682
  //0v gives 0
  //Connecting to nothing gives a floating value.
  
  readPin = analogRead(analogPin);
  Serial.println(readPin);
  delay(300);

}

 

Example 2: analogReadv2ConvertToVoltage.ino

Converts the analogread() value to a voltage

Click to Download code:analogReadv2ConvertToVoltage.ino

 

 
/* 03/09/2021

  analogReadv2ConvertToVoltage

  Convert the analogRead value into a voltage

  DO NOT EXCEED THE VOLTAGE INPUT LIMITS FOR YOUR BOARD

  Maximum Voltage Arduino UNO/nano/Mega 2560 5v DC
  Maximum Voltage Arduino Due 3.3v DC

  Previous lessons used in this sketch:
  lesson 2 "Hello World" Basics of printing to Serial Window http://www.digitaltown.co.uk/lesson2helloworld.php
  Lesson 6 boolean, byte, int, long, unsigned int, unsigned long http://www.digitaltown.co.uk/lesson6bitsandbytes.php
  Lesson 8 Mathematics Example 4 for information on Float variable http://www.digitaltown.co.uk/lesson8mathematics.php 
  
*/
//Variables

int readPin; //The variable that will store the returned value from the analog pin
float myVoltage; //variable that will store the volatage



//A const int with the pin number
const int analogPin = A5;

void setup() {
  Serial.begin(9600);
  Serial.println("analogReadv2ConvertToVoltage...");
  Serial.println(" ");

}

void loop() {

  readPin = analogRead(analogPin);
  Serial.print("readPin value: ");
  Serial.println(readPin);

  
  //Convert to voltage, voltage is 0.0049 volts per step on 5.0v boards
  myVoltage = readPin * 0.0049;
  Serial.print("myVoltage volts: ");
  Serial.println(myVoltage);
  delay(300);

}

Additional Resource Links

CatWasherV4 Cat deterrent uses analog read to read a TMP36 temp sensor

Don't forget to use the Reference in you Arduino IDE for more help on this lesson see analogRead()

Comments


This site has been designed to be child friendly, this means that comments cannot be added to videos or directly to the site.
To add a comment or ask a question please email the address in this image: and use Functions Lesson 3 - analogRead() as a reference.