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.
Analog to Digital Converter (ADC)
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!
Potentiometers
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)
Hardware
In addition to your microcontroller board, you will need the following hardware to follow along with this example.
Potentiometer
- 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.
Create a Potentiometer Component on Adafruit IO
On the device page, click the New Component (or "+") button to open the component picker.
Search for the component name by entering potentiometer
into the text box on the component picker, the list of components should update as soon as you stop typing.
Since WipperSnapper supports such a large number of components, there is keyword filtering. Try searching for various keywords, like:
- component names:
aht20
,servo
,buzzer
,button
,potentiometer
, etc - sensor types:
light
,temperature
,pressure
,humidity
, etc - interface:
i2c
,uart
,ds18x20
,pin
, etc (also I2C addresses e.g.0x44
) - vendor:
Adafruit
,ASAIR
,Infineon
,Bosch
,Honeywell
,Sensirion
, etc
There are also added product and documentation links for every component, follow the links beneath the component descriptions to be taken to the appropriate product page or Learn Guide.
Select the Potentiometer from the list of results to go to the component configuration page.
There will be a back button if you select the wrong component, and you can use the Edit component icon (⚙️) on the device page to update the component configuration in the future.
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).
Read Analog Pin Voltage Values
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!
Text editor powered by tinymce.