Project 6.00 Using the Potentiometer

In this project, you'll learn how to read the values from a potentiometer and display them using the Serial Monitor!
In this project, you’ll learn how to read the values from a potentiometer and display them using the Serial Monitor!

Project Code:

				
					///////////////////////////////////////////////////////////
// 6.00 - Using The Potentiometer

byte potPin = A0;

void setup() {
  Serial.begin(9600);
  pinMode(potPin, INPUT);
}

void loop() {
 int potValue = analogRead(potPin); 

 Serial.print("The pot value is at: "); Serial.println(potValue);
 delay(100);
}
///////////////////////////////////////////////////////////
				
			

*If you’re copying and pasting the code, or typing from scratch, delete everything out of a new Arduino sketch and paste / type in the above text.

First, we declare the pin that the potentiometer is connected to as potPin. This variable declaration is different from those you’ve seen so far because it starts with an “A”. This “A” means that the pin we are connecting to is an analog pin. Analog pin refers to a pin that can read analog values. A digital value would be a 1 or a 0 (5v or 0v), but an analog value can be anything in between. Not all pins can be used as analog pins on the MC Trainer.

				
					byte potPin = A0; 
				
			

In the setup you’ll notice that the pin is still an INPUT pin. This is because we are using the pin to read a value. It does not matter if it is digital or analog, it is still an input.

				
					 pinMode(potPin, INPUT);
				
			

You can read the analog voltage at an analog pin by using the analogRead function. analogRead returns a 10-bit value (0 – 1023) that represents the voltage at that pin. 5v would be 1023, ~2.5v would be 512, and 0v would be 0.

				
					int potValue = analogRead(potPin);
				
			

After that we print the value read out into the Serial port and delay 100 milliseconds. Make sure and open the Serial port to see the data being printed.

				
					Serial.print("The pot value is at: "); Serial.println(potValue);
delay(100);
				
			

Try rotating the knob of the potentiometer to see the range of values produced!

Learn More In Our Free Instructional Guidebook

This comprehensive guidebook for the 1st Maker Space Microcontroller Trainer provides a comprehensive introduction to the world of Arduino programming for beginners. It guides users through the foundational concepts of microcontrollers, detailing the unique features of the Arduino Leonardo-compatible MCU Trainer board. The manual offers a step-by-step journey from understanding the hardware components and the Arduino programming language to the vibrant global community of Arduino enthusiasts. It delves into the intricacies of each onboard circuit, explaining their functionalities and applications. With a focus on hands-on learning, the manual includes a series of coding exercises, tutorials in C/C++, and insights into the Arduino IDE.

More Projects

Developmental Project 1.00 Vote

In this project, vote using the OLED and buttons.

Developmental Project 2.00 Mind the Dog

The program demonstrates how embedded systems can track time without stopping the program. Instead of using delay(), which pauses execution, it uses the millis() timestamp technique to calculate elapsed time. This allows multiple tasks—timers, display updates, and LED indicators—to run simultaneously.