Your microcontroller board has both digital and analog signal capabilities. Some pins are analog, some are digital, and some are capable of both. Check the Pinouts page in this guide for details about your board.
Analog signals are different from digital signals in that they can be any voltage and can vary continuously and smoothly between voltages. An analog signal is like a dimmer switch on a light, whereas a digital signal is like a simple on/off switch.
Digital signals only can ever have two states, they are either are on (high logic level voltage like 3.3V) or off (low logic level voltage like 0V / ground).
By contrast, analog signals can be any voltage in-between on and off, such as 1.8V or 0.001V or 2.98V and so on.
Analog signals are continuous values which means they can be an infinite number of different voltages. Think of analog signals like a floating point or fractional number, they can smoothly transiting to any in-between value like 1.8V, 1.81V, 1.801V, 1.8001V, 1.80001V and so forth to infinity.
Many devices use analog signals, in particular sensors typically output an analog signal or voltage that varies based on something being sensed like light, heat, humidity, etc.
An analog-to-digital-converter, or ADC, is the key to reading analog signals and voltages with a microcontroller. An ADC is a device that reads the voltage of an analog signal and converts it into a digital, or numeric, value. The microcontroller can’t read analog signals directly, so the analog signal is first converted into a numeric value by the ADC.
The black line below shows a digital signal over time, and the red line shows the converted analog signal over the same amount of time.
Once that analog signal has been converted by the ADC, the microcontroller can use those digital values any way you like!
A potentiometer is a small variable resistor that you can twist a knob or shaft to change its resistance. It has three pins. By twisting the knob on the potentiometer you can change the resistance of the middle pin (called the wiper) to be anywhere within the range of resistance of the potentiometer.
By wiring the potentiometer to your board in a special way (called a voltage divider) you can turn the change in resistance into a change in voltage that your board’s analog to digital converter can read.
To wire up a potentiometer as a voltage divider:
- Connect one outside pin to ground
- Connect the other outside pin to voltage in (e.g. 3.3V)
- Connect the middle pin to an analog pin (e.g. A0)
In addition to your microcontroller board, you will need the following hardware to follow along with this example.
- Feather 3.3V to potentiometer left pin
- Feather A2 to potentiometer middle pin
- Feather GND to potentiometer right pin
Note: On WipperSnapper, the ESP32's ADC1 pins are unusable for analog input since WiFi is constantly running.
On the device page, click the New Component (or "+") button to open the component picker.
On the component picker, click the potentiometer.
On the Create Potentiometer Component form:
- Set Potentiometer Pin to A2
- Select "On Change" as the Return Interval
- Select Raw Analog Value as the Return Type
Then, click Create Component
The potentiometer component appears on your board page! Next, learning to read values from it.
What do these values mean?
WipperSnapper reports ADC "raw values" as 16-bit unsigned integer values. Your potentiometer will read between 0 (twisting the pot to the leftmost position) and 65535 (twisting the pot to the rightmost position).
You can update the potentiometer component (or any analog pin component in WipperSnapper) to report values in Volts. To do this, on the right-hand side of the potentiometer component, click the cog button.
Under Return Type, click Voltage.
Click Update Component to send the updated component settings to your board running WipperSnapper.
Now, twist the potentiometer to see the value reported to Adafruit IO in Volts!