Arduino Tutorial 03: Analog Inputs

Welcome to the third Arduino Tutorial from our Arduino Tutorial Series. In this tutorial we will learn how the Arduino Analog Inputs pins work and make few examples using a potentiometer and a photocell.

This is a Step by Step Video Tutorial which is easy to be followed. Also, below the video you can find what Parts do we need for this tutorial and the Source Codes of the Examples in the video.

Components needed for this Arduino Tutorial


Disclosure: These are affiliate links. As an Amazon Associate I earn from qualifying purchases.

Circuit schematic of the first example. Using the potentiometer value as analog input


 

Analog Inputs Circuit Schematic 01

 

Circuit schematic of the second example. Using the photocell as a voltage divider and it’s variable value as analog input


 

Analog Inputs Circuit Schematic 02

 

Source Code of the first and the second example


 

void setup() {
Serial.begin(9600);
}

void loop() {
int sensorValue = analogRead(A0);
Serial.println(sensorValue);Code language: Arduino (arduino)

 

Circuit schematic of the third example. Using the potentiometer to control the LED brightness via PWM


 

Analog Inputs Circuit Schematic 03

 

Source Code of the third example


 

int led = 7;

void setup() {
Serial.begin(9600);
pinMode(led, OUTPUT);
}

void loop() {
int sensorValue = analogRead(A0);
int newValeu = map(sensorValue, 0, 1023, 0, 255);
analogWrite(led, newValeu);
}Code language: Arduino (arduino)

10 thoughts on “Arduino Tutorial 03: Analog Inputs”

  1. hi
    When I use AnalogRead code on my Uno with no connected wires, I Serial monitor it keeps showing some varying digits?!?!?!
    how can I set it to Zero when there is no wire connection to the Analog Pin

    Reply
    • Well you need to have something connected to the Arduino in case you are reading, otherwise the digital pin is floating a it doesn’t have a specific value.

      Reply
  2. Hi Dejan,
    Awesome tutorials, really great job! Many thanks!
    It would be even better if you could mention values (parameters) of the passive components that you use, i.e. resistors, capacitors, sensors, etc. Newbies like me may well understand the logic and schematics but be unfamiliar with the usual parameters of components typically used with Arduino boards and most common breakout boards and controllers…
    Many thanks anyway )
    Alex

    Reply
  3. how can I control 2 motors with two potentiometers?

    I tried to duplicate the code twice and changing the names so that it can read from A0 and A1 but didn’t work

    Reply
  4. Hi there
    Would you be able to post code to control 2 stepper motors with a potentiometer, I have tried myself by cant seem to work it out.
    Thanks

    Reply

Leave a Comment