Arduino-potentiometer

0

 Potentiometer readings are printed to the Arduino Serial Monitor.

        In this demonstration, a potentiometer is used to read analog input from the real world. When its shaft is spun, a potentiometer's resistance level varies. It is a straightforward mechanical device. You can measure the amount of resistance a potentiometer (also known as a pot) produces as an analog value by applying a voltage to it and then reading the result into an analog input on your board. In this demonstration, the serial connection will be established between your Arduino and a computer running the Arduino Software, and you will then check the status of your potentiometer (IDE).

Hardware Required

Image Name Quantity Shop Online
Arduino UNO 1 No
Potentiometer 1 No

Circuit diagram


     The amount of resistance on either side of the wiper, which is attached to the centre pin of the potentiometer, can be adjusted by rotating the potentiometer's shaft. The voltage at the centre pin is altered as a result. The voltage at the centre pin approaches 5 volts when the resistance between the centre and the side connected to 5 volts is close to zero (and the resistance on the other side is close to 10k ohm). The voltage at the centre pin approaches zero volts, or ground when the resistances are switched. The analogue voltage that you are reading as input is this one.

An analogue-to-digital converter, or ADC, is a circuit included inside the Arduino boards that reads this fluctuating voltage and converts it to

Arduino Code

The setup function in the diagram below just requires you to provide the command: to start serial communications between your board and computer at a speed of 9600 bits per second.
 Serial.begin(9600);
Next, create a variable in the main loop of your program to hold the resistance value coming from your potentiometer (which will be between 0 and 1023, ideal for an int datatype
 int sensorValue = analogRead(A0);
Printing this data to your serial monitor window is the last step. In the final line of code, use the command Serial.println() to do this:
 int sensorValue = analogRead(A0);
Now, you should see a continuous stream of numbers between 0 and 1023, corresponding to the location of the pot, when you open your Serial Monitor in the Arduino Software (IDE) (by clicking the icon that resembles a lens, on the right, in the green top bar, or by using the keyboard shortcut Ctrl+Shift+M). These figures will almost immediately change as you adjust your potentiometer.
/*
  AnalogReadSerial

  Reads an analog input on pin 0, prints the result to the Serial Monitor.
  Graphical representation is available using Serial Plotter (Tools > Serial Plotter menu).
  Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/AnalogReadSerial
*/

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // print out the value you read:
  Serial.println(sensorValue);
  delay(1);        // delay in between reads for stability
}

Tags

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !