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

Project 1.00 Blink

In this project, you’ll learn how to blink an LED!

Project 1.01 Blink x2

In this project, you’ll learn how to blink more than one LED!

Bri Wagner

Education Solution Specialist

Bri Wagner brings a bachelor’s degree in Elementary Education and nine years of classroom teaching experience to her role at 1st Maker Space. A former Teacher of the Year Award recipient, Bri is passionate about creating engaging, hands-on learning experiences that inspire student success. She understands the impact that innovative instruction and meaningful classroom resources can have on student achievement and is dedicated to partnering with educators to bring high-quality STEM solutions into classrooms. Bri is committed to empowering teachers and helping students develop the creativity, confidence, and problem-solving skills they need to thrive.