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
- Arduino Board …………………………… Amazon / Banggood / AliExpress
- Breadboard and Jump Wires ……… Amazon / Banggood / AliExpress
- LED …………………………………………… Amazon / Banggood / AliExpress
- 220 Ohm Resistor …………………….. Amazon / Banggood / AliExpress
- Potentiometer ……………………………. Amazon / Banggood / AliExpress
- Photocell (Photo-resistor) …………… Amazon / Banggood / AliExpress
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
Circuit schematic of the second example. Using the photocell as a voltage divider and it’s variable value as analog input
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
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)
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
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.
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
Thank you! I will take this into consideration.
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
Sure, you can do that. You can’t duplicate the entire code twice, you need to duplicate and rename the motor and potentiometer definitions.
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
Check my Arduino stepper motor tutorial, it should help you.
good job, thank you!
Thanks!