# Potentiometer and Joystick

#### A variable Resistor

the potentiometer is a device that can give us a variable resistance value. In more detail it has 3 connections and actually gives two variable resistance values. This is the symbol for a potentiometer. It looks like a resistor but with a third connection that is the arrow. In schematics an arrow generally indicates a variable circuit component. In the potentiometer the connection with the arrow is the one that moves and provides a variable resistance value.

#### A Sliding Connection

using the symbol above you can visualize how the potentiometer works. imagine that you can slide the arrow connection to the right or left so it connects to different points on the resistor symbol. As you move to the right the amount of resistor on the left gets bigger and the amount of resistor on the right gets smaller. The total resistance has some set value, in our case it is 10,000 ohms. and we split this between the left and right sides so we could have something like. 1,000 - 9,000. or 5,000 - 5,000 or 9,000 - 1,000 or even 10,000 - 0. The potentiometer gives us a continuous range of values so it is an Analog Device. This is the physical potentiometer component that comes in the elegoo kit. The potentiometer is the 3 leg part, there is also an optional knob shown here to make adjusting the potentiometer easy. If you do know have the black knob for turning you can use a small screw driver with hex head. size 5/64

#### Circuit Wiring

We will use the potentiometer as a voltage divider circuit. You can review the Voltage Divider at our previous lesson. In our circuit the two resistors of the potentiometer will share or divide the voltage. This way as we adjust the potentiometer we will get a variable voltage on the moving connection. We will be able to adjust from 0 to the full 5V. Here is the picture and the schematic digram of the circuit.

#### Arduino Code

We will start with Arduino code that reads the value of the voltage and prints it to the computer screen. Later we will extend it to a voltage level indicator using 3 LED lights.

The potentiometer circuit provides a continuously variable voltage so it is an Analog component. We will use

```analogRead()
```

to read the value of the voltage. We will read the value into the variable sensorValue and then print it to the serial monitor.

```int sensorPin = A0;

void setup() {
//set up things pin selections and variables
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop() {
// put your main code here, to run repeatedly:
Serial.println(sensorValue);
delay(50);
}
```

Upload and run the code to your arduino board. Open a serial monitor and adjust the potentiometer.

An extension of this code is to add an LED light and use the sensorValue to adjust the blinking rate of the LED. To start we will use the built in LED on the Arduino Uno board. The LED is at pin 13. The LED is either on or off so it is a Digital output device. We will use

` digitalWrite() `

to control the LED and a delay that will be adjusted by the potentiometer. The

` delay() `

takes a value in msec and waits for that amount of time. The

` analogRead() `

gives a value between 0 and 1023, so using this in delay will give that many msec of wait time.

Here is the code:

```
int sensorPin = A0;

void setup() {
//set up things pin selections and variables
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
pinMode(13,OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
Serial.println(sensorValue);
// turn the ledPin on
digitalWrite(13, HIGH);
// stop the program for  milliseconds:
delay(sensorValue);
// turn the ledPin off:
digitalWrite(13, LOW);
// stop the program for for  milliseconds:
delay(sensorValue);
}
```

#### The Joystick

The Joystick is a clever arrangement of 2 potentiometers. This way we can get two variable resistance values one when we move the joystick up and down and another when we move it side to side. Picture of the joystick. You can see the two potentiometers. if you move the stick you can see them move. Most Joystick also have a switch pushbutton.  The pin connections are 5V, GND, axis1, axis2 and switch.

#### The Circuit Here is the picture of wiring. The switch connection , green wire to pin 2 is optional.

#### The Arduino Code

Try to extend the previous code to read the two potentiometers of the joystick and print it to the screen. You will need to use

`analogRead()`

twice. And have two variables probably something like xValue and yValue. Try to print to the screen something like this:

```
xValue = 512
yValue = 128
```

The numbers will change as you move the joystick.

Back to the course
{"ft_lang_label":"__label__en","ft_lang_prob":0.80037725,"math_prob":0.94373065,"size":3718,"snap":"2021-43-2021-49","text_gpt3_token_len":877,"char_repetition_ratio":0.13408723,"word_repetition_ratio":0.11692308,"special_character_ratio":0.23910704,"punctuation_ratio":0.10547945,"nsfw_num_words":1,"has_unicode_error":false,"math_prob_llama3":0.9747469,"pos_list":[0,1,2,3,4,5,6,7,8,9,10],"im_url_duplicate_count":[null,3,null,3,null,3,null,3,null,3,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-18T09:55:03Z\",\"WARC-Record-ID\":\"<urn:uuid:3c3a73d8-3efe-4041-b804-22f2c44f075f>\",\"Content-Length\":\"49034\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:e1da1e3c-77d0-471b-bbbb-c1e3ae455208>\",\"WARC-Concurrent-To\":\"<urn:uuid:3ae5d24b-e7d8-4d7f-bf45-73320ae224bf>\",\"WARC-IP-Address\":\"52.87.36.100\",\"WARC-Target-URI\":\"https://online.rollingrobots.com/potentiometer-and-joystick\",\"WARC-Payload-Digest\":\"sha1:BYWF22GAOMSKP5TBZOG325PICR33JQ67\",\"WARC-Block-Digest\":\"sha1:MFNLNAEUEOA4QFBRDZ6IEBXPCPFQY4ON\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323585201.94_warc_CC-MAIN-20211018093606-20211018123606-00379.warc.gz\"}"}