- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Basic analogRead in Arduino Program
Converting analog values to digital is a common requirement from microcontrollers in general, and Arduino is no different. Arduino IDE has a built-in analogRead function to facilitate the conversion of analog values to digital.
From the programming perspective, the only thing you require to know is the pins of your microcontroller that support ADC. On the Arduino UNO board, the pins A0 to A5 support ADC.
Now, let us assume that you’ve connected your A0 pin to an analog wire (maybe the junction between an LDR and a resistor, or the central leg of a potentiometer).
The basic Arduino code to print the analog readings is −
int sensorPin = A0; // select the input pin for the potentiometer void setup() { } void loop() { // read the value from the sensor: sensorValue = analogRead(sensorPin); print(sensorValue); delay(1000); }
As you can see, there are no initializations required in the setup. The analogRead function will read the input voltage coming in on A0 pin, compare it with 5V, and scale it to 1024 (default resolution is 10-bit). Thus, if there is 5V coming on A0 pin, the value printed will be 1024. If there is 2.5V coming on A0 pin, the value printed will be 2.5/5*1024 = 512.
- Related Articles
- Basic analogRead in Arduino
- Change the resolution of analogRead in Arduino
- How to Perform Basic Linear Algebra on Arduino?
- Structs in Arduino program
- Basic calculator program using Python program
- Basic calculator program using C#
- Basic calculator program using Python
- Basic calculator program using Java
- C Program for Basic Euclidean algorithms?
- Python Program for Basic Euclidean algorithms
- Explain the basic structure of a program in Java?
- How to program a board using Arduino IDE
- C# Program to perform all Basic Arithmetic Operations
- Arduino Uno vs Arduino Leonardo
- Arduino Uno vs Arduino Micro
