A potentiometer is the simplest way to get a variable input into your code - turn a knob, get a number. This tutorial walks through wiring one up to an Arduino Giga R1 WiFi and reading it with MicroPython, converting the raw ADC reading into a voltage you can print to the console.
The full tutorial, with every detail and the line-by-line code explanation, is on our site: Arduino MicroPython Potentiometer
What you need
- Arduino Giga R1 WiFi
- USB Cable Type-A to Type-C (for USB-A PC)
- USB Cable Type-C to Type-C (for USB-C PC)
- Potentiometer
- 10k Ohm Trimmer Potentiometer
- Potentiometer Kit
- Potentiometer Module with Knob
- Breadboard
- Jumper Wires
- Screw Terminal Block Shield for Arduino Uno/Mega/Giga
- Sensors/Servo Expansion Shield for Arduino Mega/Giga
- Breadboard Shield for Arduino Mega/Giga
- Enclosure for Arduino Giga
- Power Splitter for Arduino Giga
- DIYables Sensor Kit (18 sensors/displays)
Wiring
A potentiometer has three pins. The GND pin goes to the board's GND (0 volts), the VCC pin goes to 3.3 volts, and the output pin (the wiper) connects to one of the Arduino's analog input pins, in this case A0. The board reads whatever voltage shows up on that middle pin.
How it works
Inside the potentiometer, turning the shaft moves a wiper along a resistive track between the GND and VCC pins. At 0 degrees the output pin sits at GND, so you read 0 volts. At the maximum angle it sits at VCC, so you read the full 3.3 volts. Anywhere in between, the output voltage scales with the angle: angle times VCC divided by the maximum angle. The Arduino's analog-to-digital converter (ADC) turns that voltage into a number between 0 and 4095, which you can then map into an angle, a voltage, or any other range your application needs, like a volume or brightness level.
Code
The script sets up an ADC on pin A0 and loops forever. Arduino's read_u16() returns a 16-bit reading, so the code shifts it right by 4 bits to get back the 12-bit range (0 to 4095) matching the hardware. A small map_value() helper rescales that number into a 0 to 3.3 volt range, and the loop prints both the raw ADC value and the computed voltage once per second using time.sleep(1).
arduino-micropython-potentiometer.py
"""
This Arduino MicroPython script was developed by newbiely.com
This Arduino MicroPython script is made available for public use without any restriction
For comprehensive instructions and wiring diagrams, please visit:
https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-potentiometer
"""
from machine import ADC, Pin
import time
# Define a function to map a value from one range to another
def map_value(x, in_min, in_max, out_min, out_max):
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
# Initialize the analog pin for reading
adc = ADC(Pin('A0')) # The Arduino Giga WiFi pin A0 connected to the S pin of water sensor
# Infinite loop
while True:
# Retrieve analog value from pin A0:
adc_value = adc.read_u16() >> 4 # Read the 16-bit analog value from the sensor and convert back to 12-bit ADC
# Convert the analog value to a voltage (0-3.3V range):
voltage = map_value(adc_value, 0, 4095, 0, 3.3)
# Output the analog value and corresponding voltage to the console:
print("Analog: {}, Voltage: {:.2f}V".format(adc_value, voltage))
# Wait for a second before repeating the loop:
time.sleep(1)
Full tutorial
The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-potentiometer

Top comments (0)