POTENTIOMETER
By turning the shaft of the potentiometer, we change the amount of resistence on either side of the wiper which is connected to the center pin of the potentiometer. This changes the relative "closeness" of that pin to 5 volts and ground, giving us a different analog input. When the shaft is turned all the way in one direction, there are 0 volts going to the pin, and we read 0. When the shaft is turned all the way in the other direction, there are 5 volts going to the pin and we read 1023. In between, analogRead() returns a number between 0 and 1023 that is proportional to the amount of voltage being applied to the pin.
CODE
(The following code reads the value from the potentiometer and prints it in the serial)
(The following code reads the value from the potentiometer and prints it in the serial)
int potPin = 2;
int val = 0; void setup() { pinMode(potPin, INPUT); } void loop() { val = analogRead(potPin); Serial.println(val); } |
Select the input pin for the potentiometer variable to store the value coming from the sensor
declare the potPin as an INPUT read the value from the sensor Prints in the serial the value the sensor measured |