Arduino MicroPython Potentiometer

This guide teaches you how to use a potentiometer with an Arduino and MicroPython. In detail, you will learn:

Arduino MicroPython potentiometer

Hardware Preparation

1×Arduino Giga R1 WiFi
1×USB Cable Type-C
1×Potentiometer
1×Potentiometer Kit
1×Breadboard
1×Jumper Wires
1×(Recommended) Screw Terminal Block Shield for Arduino Uno/Mega/Giga
1×(Recommended) Breadboard Shield For Arduino Uno/Mega/Giga
1×(Recommended) Enclosure For Arduino Giga

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Potentiometer

A rotary potentiometer, which is also called a rotary angle sensor, is used to adjust settings manually like stereo volume, lamp brightness, or how much you zoom in on an oscilloscope.

Potentiometer Pinout

Pinout

A potentiometer usually has three pins.

  • GND pin: connect to GND (0 volts)
  • VCC pin: connect to VCC (3.3 volts)
  • Output pin: sends voltage to Arduino's input pin.
Potentiometer Pinout

※ NOTE THAT:

You can switch the positions of the GND pin and the VCC pin.

How It Works

The potentiometer's shaft turns from 0 degrees, near the GND, to a maximum position near the VCC pin, called ANGLE_MAX.

The voltage at the output pin ranges from the ground (GND) voltage to the supply voltage (VCC). As you rotate the shaft, the output voltage changes accordingly.

  • If the angle is 0 degrees, there is no voltage (0 volts) at the output pin.
  • If the angle is ANGLE_MAX, the output pin's voltage is the same as VCC’s voltage.
  • For angles between 0 degrees and ANGLE_MAX, the output voltage is calculated as: angle × VCC / ANGLE_MAX.

※ NOTE THAT:

The value of ANGLE_MAX changes depending on the manufacturer. Normally, we do not focus on the value of ANGLE_MAX unless we need to calculate the rotation angle (see the use cases section).

How Potentiometer Works

Arduino - Rotary Potentiometer

The Arduino's ADC pins work as analog inputs, converting a voltage range from 0 volts to VCC into numbers between 0 and 4095. These numbers are called ADC values or analog values.

You can connect a potentiometer's output pin to one of the Arduino's analog input pins, allowing the Arduino to read the ADC value and convert it into a usable number.

The number received by the Arduino isn't an angle or a direct voltage but a value between 0 and 4095. This value can then be mapped or adjusted to a different range depending on the application.

Use Cases

  • Converting the ADC value into an angle.
  • Converting the ADC value into a voltage.
  • Converting the ADC value into a controllable value (like adjusting the volume of a stereo, the brightness of a light, or the speed of a motor). This is one of the most common applications.

Rescale Range

FROM TO
Anglerotated by userANGLE_MAX
Voltagefrom potentiometer's pin 0V3.3V
ADC valueread by Arduino 04095
Other valueconverted by Arduino VALUE_MINVALUE_MAX

Wiring Diagram

The wiring diagram between Arduino MicroPython Potentiometer

This image is created using Fritzing. Click to enlarge image

Arduino MicroPython Code

""" 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)

Detailed Instructions

Here’s instructions on how to run the above MicroPython code on Arduino with Thonny IDE:

  • Make sure Thonny IDE is installed on your computer.
  • Make sure MicroPython firmware is installed on your Arduino board.
  • If you are new to Arduino with MicroPython, see the Getting Started with Arduino and MicroPython.
  • Connect the Arduino board to the potentiometer according to the provided diagram.
  • Connect the Arduino board to your computer with a USB cable.
  • Open Thonny IDE and go to Tools Options.
  • Under the Interpreter tab, select MicroPython (generic) from the dropdown menu.
  • Select the COM port corresponding to your Arduino board (e.g., COM33 on Windows or /dev/ttyACM0 on Linux).
  • Copy the provided Arduino MicroPython code and paste it into Thonny's editor.
  • Save the MicroPython code to your Arduino by:
    • Clicking the Save button or pressing Ctrl+S.
    • In the save dialog, choose MicroPython device and name the file main.py.
  • Click the green Run button (or press F5) to execute the code.
  • Adjust the potentiometer
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot Analog: 0, Voltage: 0.00V Analog: 0, Voltage: 0.00V Analog: 0, Voltage: 0.00V Analog: 503, Voltage: 0.41V Analog: 1124, Voltage: 0.91V Analog: 2068, Voltage: 1.67V Analog: 3015, Voltage: 2.43V Analog: 3622, Voltage: 2.92V Analog: 4095, Voltage: 3.30V Analog: 4095, Voltage: 3.30V
MicroPython (generic) • Giga Virtual Comm Port in FS Mode @ COM33 ≡

Video Tutorial

Learn More

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!