Arduino Nano 33 IoT - Potentiometer Servo Motor

This guide shows you how to use the Arduino Nano 33 IoT to set the position of a servo motor using a value from a knob. In this lesson, we will learn:

Hardware Preparation

1×Arduino Nano 33 IoT
1×Micro USB Cable
1×Servo Motor
1×Potentiometer
1×Alternatively, Potentiometer Kit
1×Alternatively, Potentiometer Module with Knob
1×Breadboard
1×Jumper Wires
1×Optionally, 5V Power Adapter for Arduino Nano 33 IoT
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 Servo Motor and Potentiometer

If you're new to using the Servo Motor, Potentiometer, and Arduino Nano 33 IoT, please check out these tutorials:

These tutorials explain how Servo Motor and Potentiometer work, their pinouts, how to connect them to the Arduino Nano 33 IoT, and how to program Arduino Nano 33 IoT to work with the Servo Motor and Potentiometer.

Wiring Diagram

The wiring diagram between Arduino Nano and 33 IoT Servo Motor 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

  • Read the value from the potentiometer, which can be any number between 0 and 1023.
int analog_value = analogRead(A3);
  • Change it into an angle between 0 and 180.
int angle = map(analog_value, 0, 1023, 0, 180);
  • Set the servo to the given angle.
servo.write(angle);

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-servo-motor */ #include <Servo.h> #define PIN_POTENTIOMETER A3 // The Arduino Nano 33 IoT pin connected to potentiometer #define PIN_SERVO 2 // The Arduino Nano 33 IoT pin connected to servo motor Servo servo; // create servo object to control a servo void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); servo.attach(PIN_SERVO); // attaches the Arduino Nano 33 IoT pin to the servo object } void loop() { // reads the value of the potentiometer (value between 0 and 4095) int analogValue = analogRead(PIN_POTENTIOMETER); // scales it to use it with the servo (value between 0 and 180) int angle = map(analogValue, 0, 4095, 0, 180); // sets the servo position according to the scaled value servo.write(angle); // print out the value Serial.print("Analog value: "); Serial.print(analogValue); Serial.print(" => Angle: "); Serial.println(angle); delay(100); }

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 above and open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to send the code to the Arduino Nano 33 IoT.
Arduino IDE Upload Code
  • Open the Serial Monitor
  • Rotate the knob on the potentiometer
  • Watch the servo motor move
  • See the result in the Serial Monitor
COM6
Send
Analog: 0, Angle: 0 Analog: 85, Angle: 14 Analog: 201, Angle: 35 Analog: 286, Angle: 50 Analog: 370, Angle: 65 Analog: 444, Angle: 78 Analog: 521, Angle: 91 Analog: 608, Angle: 106 Analog: 690, Angle: 121 Analog: 793, Angle: 139 Analog: 907, Angle: 159 Analog: 1023, Angle: 180 Analog: 1023, Angle: 180
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Code Explanation

Take a look at the step-by-step explanation in the code comments!

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!