This tutorial explains how to take analog input to Arduino. One of the basic tutorials for Arduino. This input is shown through LED and Serial monitor.
1. Introduction:
A step by step illustrated basic tutorial for Arduino. Here we are taking analog input form a potentiometer. And this input is shown on LED as PWM and analog values on Serial monitor.
Arduino gives analog output in range of 0 to 255. Technically the output is digital but in the form of PWM, but it seems to be analog.
Arduino Boards have 6 PWM(Analog Pins) these are PIN No. 3,5,6,9,10,11.
Arduino Boards have 6 Analog Input pins these are PIN A0, A1, A2, A3, A4 and A5.
1.2 Hardware required
S.No. | Item | Quantity |
1 | Arduino UNO | 1 |
2 | Breadboard | 1 |
3 | LED | 1 |
4 | Resistor 1k | 1 |
5 | Jumper Male to male | 7 |
6 | Preset 10k | 1 |
2. Building Circuit
Schematic Diagram:
Circuit Layout:
3. Programming:
Once we are done with circuit part, here is our programme to this circuit.
A few points to understand:
1. Arduino has got 10 bit ADC so input we are taking from potentiometer will give us 10 bit data i.e. 0 to 1023.
2. Arduino’s PWM (output for LED) has got a range of 0 to 255.
3. Thus in order to take analog input to PWM we have divided the input value by 4. Please go through the coding you will be clear about these points.
4. This programme runs for ever, every time it takes reads value from input, if it is near about the same to last value it will not do anything, if we dont apply this algo of comparing last value to new value we will receive every value on serial monitor and it will be a difficult thing to even read the serial monitor.
You may download this code (Arduino Sketch) from here.
/* Tutorial for Analog input and Output. Input through Potentiomete and output through PWM on LED and serial monitor. Prepared by Robo India. www.roboindia.com */ int analog_ip = A0; // select the input pin for the potentiometer int LED = 3; // select the pin for the LED int inputVal = 0; // variable to store the value coming from potentiometer int old_ip_val = 0; // varialbe to store historic value of analog input. void setup() { pinMode(LED, OUTPUT); // Defining LED Pin as output Serial.begin(9600); // Setup Serial Communication. Serial.print("ROBO INDIA\nroboindia.com\nTutorial on Analog input and output.\n"); } void loop(){ inputVal = analogRead(analog_ip); // Reading and storing analog input value. // following if condition is to check last value with new value if new value = +/-5 // then we are not interested to do anything. For other values we want to it to to - if (inputVal <old_ip_val-5 || inputVal >old_ip_val+5) { Serial.print("Input Value : "); Serial.print(inputVal); // Printing Analog input value (0-1023) Serial.print("\nLED Brightness : "); Serial.print(inputVal/4); // Printing PWM values for LED (0-255) Serial.print("\n"); analogWrite(LED, inputVal/4); // Sending PWM value to LED. delay(500); // Waiting for a while. old_ip_val = inputVal; // Storing historic value of analog input. } }
4. output:
Here is the output of this tutorial-
When we rotate potentiometer, The output voltage changes. This change is detected by Arduino Analog Input code, This value is sent to serial monitor. And the same value after dividing by 4 is sent to LED’s PWM. So the brightness of LED changes with the change of potentiometer.
Here is an still image from this video so that you can be clear about circuit.
If you have any query please write us at support@roboindia.com
Thanks and Regards
Content Development Team
Robo India
https://roboindia.com