Arduino Projects

Arduino Analog Sensor Input Code Example

Arduino Analog Sensor Input, Example:

In this example, I am going to explain how to read an Arduino analog sensor and send its value to the Serial monitor. So, in this example, we are going to cover two things, how to read an analog sensor and how to use Serial Communication. For the demonstration purposes I am going to use the Potentiometer as the analog sensor, if you want you can also use an LDR or any other analog sensor.




Amazon Links:

Arduino Nano USB-C Type (Recommended)

SSD1306 Oled display

HC-SR04 Ultrasonic Sensor

5V Buzzer

I2C supported 16×2 LCD

Other Tools and Components:

ESP32 WiFi + Bluetooth Module (Recommended)

Top Arduino Sensors:

Super Starter kit for Beginners

Digital Oscilloscopes

Variable Supply

Digital Multimeter

Soldering iron kits

PCB small portable drill machines

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

Arduino course

The Leftmost and rightmost legs of the potentiometer are connected to the Arduino 5V and GND. Whereas the middle leg of the potentiometer is connected to the Analog pin A0. Now, let’s go ahead and take a look at the programming.



Arduino Analog Input programming:

I simply started off by defining the pin and a variable. To activate Serial communication between Arduino and a computer or any other serial communication supported devices; we use the Serial.begin() function and inside the parenthesis we add the Baud rate. For this example, I have selected the standard baud rate of 9600. The baud rate specifies the speed at which data is transmitted in bits per seconds(bps). Common baud rates include 9600, 115200, 57600, 38400, etc.

analogRead() is a function used to read analog voltage values from an analog input pin on the Arduino board. Arduino boards have several analog input pins labeled with “A0,” “A1,” and so on. These pins can be used to read analog signals, such as those generated by sensors, potentiometers, or other analog devices.

The analogRead() function takes one argument, which is the number of the analog input pin you want to read from. The argument should be a number representing the pin’s analog input number. For example, to read from analog pin A0, you would use analogRead(A0). But, in my case, I have defined it with the name pot.

So, it reads the potentiometer and stores the value in a variable potvalue and then using the Serial.println() function we send the value to the Serial Monitor.



Upload the program and then click on the Serial monitor button.

Arduino course

Then you should be able to see the Potentiometer value; you will see different values as you rotate the knob of the Potentiometer. For the step by step explanation and practical demonstration watch the video tutorial given at the end of this article.

Arduino course



video tutorial

 

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button