Potentiometer
This chapter will show you how to connect a potentiometer to your Arduino board read the current value of the potentiometer from JavaScript.
What is a potentiometer?
Potentiometers are everywhere. You probably have just been calling them “knobs” up until now. You have them in your hand ever day, when turning up the volume on your stereo or dimming the light in your room.
Technically speaking, potentiometers are variable resistors. By turning the knob you are adjusting, how much electricity will flow from the outer pin to the middle pin of the poti.
Another thing that is typical for potentiometers: They have a start- and endpoint and - contrary to an rotary encoder - you can not turn the knob beyond these points.
A potentiometers gets connected to an analog pins of your Arduino.
Connecting the potentiometer with p5js
The basic syntax is:
let poti = connectSensor( SENSOR_POTENTIOMETER, pin, minValue, maxValue );
In the follwing example we will setup a potentionmeter which was connected to the A0
(Analog 0) pin.
If you don’t define a minValue
and maxValue
, the values returned by calling poti.vale()
will range
from 0
to 1024
:
let poti = connectSensor( SENSOR_POTENTIOMETER, 'A0' );
Here we would like the potentiometer to be mapped to values 0
and 100
:
let poti = connectSensor( SENSOR_POTENTIOMETER, 'A0', 0, 100 );
Hardware
Wiring
![Wiring Arduino p5js Javascript](../../_images/20-potentiometer.jpg)
Code
let poti;
function setup () {
createCanvas(400, 400);
poti = connectSensor( SENSOR_POTENTIOMETER, 'A0', 0, width );
}
function draw () {
background( 255 );
circle( 200, 200, poti.value() );
}