Arduino Nano 33 IoT - Potentiometer

This guide explains how to use the Arduino Nano 33 IoT with a potentiometer. In this lesson, we will learn:

Arduino Nano 33 IoT potentiometer

Hardware Preparation

1×Arduino Nano 33 IoT
1×Micro USB Cable
1×Potentiometer
1×Alternatively, Potentiometer Kit
1×Alternatively, Potentiometer Module with Knob
1×Breadboard
1×Jumper Wires
1×Optionally, DC Power Jack
1×Recommended: Screw Terminal Expansion Board for Arduino Nano
1×Recommended: Breakout Expansion Board for Arduino Nano
1×Recommended: Power Splitter for Arduino Nano

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 potentiometer, also called a rotary angle sensor, is used to adjust settings like speaker volume, room temperature, or lamp brightness.

Potentiometer Pinout

Potentiometer Pinout

A potentiometer normally has three pins.

  • VCC pin: Connect this pin to the power supply (5V or 3.3V).
  • GND pin: Connect this pin to the ground (0V).
  • Output pin: This pin sends the voltage to the Arduino Nano 33 IoT's input pin.
Potentiometer Pinout

※ NOTE THAT:

You can swap the GND pin with the VCC pin.

How Potentiometer Works

You can turn the potentiometer's shaft from 0° (nearest the GND pin) up to an angle (closest to the VCC pin) called ANGLE_MAX.

The voltage at the output pin increases in direct relation to the angle of the shaft, ranging from 0 to VCC. The table below shows how the angle and the output pin voltage are related.

Angle Voltage
0v
ANGLE_MAX°VCC
angle°angle° × VCC / ANGLE_MAX°

※ NOTE THAT:

The ANGLE_MAX value depends on the manufacturer.

How Potentiometer Works

Arduino Nano 33 IoT - Rotary Potentiometer

The Arduino Nano 33 IoT's analog input pin reads the voltage (from 0V to 3.3V) and turns it into whole numbers (from 0 to 4095). These numbers are called ADC values or analog values.

We can attach the potentiometer's output pin to one of the Arduino Nano 33 IoT's analog input pins, and then read the analog value from that pin.

You can convert an analog reading from an input pin into a different value. Let's look at some examples.

  • Convert the analog reading into the knob’s angle.
  • Convert the analog reading into the knob’s voltage.
  • Convert the analog reading into a setting value (for example, speaker volume, room temperature, or lamp brightness).

Rescale Range

FROM TO
Anglerotated by userANGLE_MAX°
Voltagefrom potentiometer's pin 0V3.3V
ADC valueread byArduino Nano 33 IoT 04095
Setting valueconverted byArduino Nano 33 IoT VALUE_MINVALUE_MAX

Wiring Diagram between Potentiometer and Arduino Nano 33 IoT

The wiring diagram between Arduino Nano and 33 IoT Potentiometer

This image is created using Fritzing. Click to enlarge image

※ NOTE THAT:

Please note that the Arduino Nano 33 IoT pins A4 and A5 have built-in pull-up resistors for I2C communication. This can affect analog readings, so it is recommended to avoid using these pins with any devices/sensors that relies on ADC.

How To Program Potentiometer

  • Use the analogRead() function to get a value from an input pin that is connected to the potentiometer's output.
analog_value = analogRead(A6); // Read the analog input on pin A6
  • Change the value so it fits the potentiometer's angle by using the map() function.
angle = map(analog_value, 0, 4095, 0, ANGLE_MAX);
  • Change the scale to match the voltage of the potentiometer.
voltage = map(analog_value, 0, 4095, 0, 3.3);
  • Set the value you can control (for example, the stereo volume, screen brightness, or DC motor speed).
value = map(analog_value, 0, 4095, VALUE_MIN, VALUE_MAX);
  • For example, changing the LED brightness. In this guide, we explain that you can control the LED's brightness with a PWM value ranging from 0 (always off) to 255 (always on). So, you can link the analog value to the LED's brightness—from off to the brightest—as shown below.
brightness = map(analog_value, 0, 4095, 0, 255);

If you want to adjust the LED brightness from a dim nightlight to full brightness,

nightlight = 100; // Set the lower brightness level based on your preference brightness = map(analog_value, 0, 4095, nightlight, 255); // Convert the analog reading into the brightness range desired

※ NOTE THAT:

The map() function can only change an analog value into an int or long number. If you need a float number (one with decimals), use the floatMap() function instead of map().

floatMap() function:

float floatMap(float x, float in_min, float in_max, float out_min, float out_max) { return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; }

Arduino Nano 33 IoT Code

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-potentiometer */ float floatMap(float x, float in_min, float in_max, float out_min, float out_max) { return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; } // the setup routine runs once when you press reset: void setup() { // Initialize the Serial to communicate with the Serial Monitor. Serial.begin(9600); } // the loop routine runs over and over again forever: void loop() { // read the input on analog pin A6: int analog_value = analogRead(A6); // Rescale to potentiometer's voltage (from 0V to 3.3V): float voltage = floatMap(analog_value, 0, 4095, 0, 3.3); // print out the value you read: Serial.print("Analog: "); Serial.print(analog_value); Serial.print(", Voltage: "); Serial.println(voltage); delay(1000); }

Detailed Instructions

If you are new to the Arduino Nano 33 IoT, be sure to check out our Getting Started with Arduino Nano 33 IoT tutorial. Then, follow these steps:

  • Connect the components to the Arduino Nano 33 IoT board as depicted in the diagram.
  • Use a USB cable to connect the Arduino Nano 33 IoT board to your computer.
  • Launch the Arduino IDE on your computer.
  • Select the Arduino Nano 33 IoT board and choose its corresponding COM port.
  • Copy the code shown above and paste it into the Arduino IDE. Then click the Upload button to check and send the code to your Arduino Nano 33 IoT board.
Arduino IDE Upload Code
  • Open the Serial Monitor in the Arduino program.
How to open serial monitor on Arduino IDE
  • Turn the potentiometer.
  • Look at the Serial Monitor. It should show something like this:
COM6
Send
Analog: 0, Voltage: 0.00 Analog: 0, Voltage: 0.00 Analog: 126, Voltage: 0.62 Analog: 281, Voltage: 1.37 Analog: 517, Voltage: 2.53 Analog: 754, Voltage: 3.2 Analog: 4095, Voltage: 3.30 Analog: 4095, Voltage: 3.30
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Video Tutorial

※ 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!