Built-in project · ESP32-DevKitC V4

MicroPython knob points a servo: GPIO 34 and 15, on an ESP32-DevKitC V4

MicroPython on an ESP32: a potentiometer on GPIO 34 sets the angle of an SG90 servo on GPIO 15, from 0 to 180 degrees. The whole build, an ESP32-DevKitC V4 and 2 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 15 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $13. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Servo control with a 50 Hz PWM signal
  • Mapping an ADC range to an angle
  • Pulse width from 0.5 ms to 2.5 ms
  • Powering a servo separately from the signal
  • Difficulty: Beginner
  • Time: About 15 minutes
  • Board: ESP32-DevKitC V4
  • Parts: 5

How it works

The potentiometer sits between ground and 3.3 V, with its middle pin on GPIO 34. The ADC reads it as a number from 0 to 4095. The program scales that to an angle from 0 to 180 degrees.

The servo gets its signal on GPIO 15 as 50 Hz PWM, and its power from 5 V. The pulse width tells it where to point. The program checks the knob every 50 ms and prints the angle when it changes by more than 2 degrees.

Parts list

5 parts, plus the jumper wires. They retail for about $13; on Mokxi they're free.

Kits that can build it

These 3 starter kits have every part in the circuit. Each kit's page lists what else it can build.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Potentiometer pin 2ESP32-DevKitC V4 pin 34
Hobby servo pin PWMESP32-DevKitC V4 pin 15
Potentiometer pin 1Ground
Potentiometer pin 33.3 V
Hobby servo pin GNDGround
Hobby servo pin VCC5 V

The code

The MicroPython code exactly as the editor opens it. Change a line there and press Run: the board runs it on the official MicroPython firmware, with nothing to compile.

main.py · ESP32-DevKitC V4
# Servo knob: the potentiometer on GPIO 34 points the servo on GPIO 15,
# 0 to 180 degrees across the knob's turn.
from machine import ADC, Pin, PWM
import time

pot = ADC(Pin(34))
pot.atten(ADC.ATTN_11DB)
pot.width(ADC.WIDTH_12BIT)
servo = PWM(Pin(15), freq=50)

def angle(deg):
    # 50 Hz is 20 ms a period: 0.5 ms (duty 26) is 0 degrees and
    # 2.5 ms (duty 128) is 180 degrees
    servo.duty(26 + deg * 102 // 180)

last = -1
while True:
    deg = pot.read() * 180 // 4095
    angle(deg)
    if abs(deg - last) > 2:
        print("angle", deg)
        last = deg
    time.sleep_ms(50)

Code walkthrough

The key functions of the sketch, and what each one does.

angle main.py

Turns degrees into a PWM duty value. At 50 Hz a period is 20 ms. A duty of 26 is a 0.5 ms pulse, which is 0 degrees, and a duty of 128 is 2.5 ms, which is 180 degrees. The line adds a share of the 102-step span for the angle given.

def angle(deg):
    # 50 Hz is 20 ms a period: 0.5 ms (duty 26) is 0 degrees and
    # 2.5 ms (duty 128) is 180 degrees
    servo.duty(26 + deg * 102 // 180)

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The servo jitters or does not move. This project
    Check that the servo VCC is on 5 V and its GND is shared with the board's ground.
  • The servo only turns part of the way. This project
    Check the knob's outer pins: one to ground and one to 3.3 V, so the ADC can reach its full range.
  • The reading jumps around or only covers part of the range. Potentiometer
    Wire the two outer legs to 5 V and ground and the middle leg (the wiper) to an analog pin. Using the wrong two legs gives a dead or partial range.
  • Turning the knob does nothing, or the value is stuck at 0 or 1023. Potentiometer
    Check that the wiper goes to an analog input (A0 to A5 on an Uno), not a digital pin. Make sure both ends are connected, since a floating end gives noise.
  • The servo jitters, twitches or resets the board when it moves. Hobby servo
    Power it from its own 5 V supply, not the Arduino 5 V pin, and join the grounds. A hobby servo can pull several hundred milliamps. Add a 100 uF to 470 uF capacitor across its supply.
  • The servo hums at the end of its travel or will not reach full angle. Hobby servo
    Stay inside the pulse range, about 1000 to 2000 us for most hobby servos. Pushing past the end stop makes it strain. Calibrate the min and max pulse for your servo.
  • The board is not found, or upload fails with a connecting message. ESP32-DevKitC V4
    Use a USB cable that carries data, not charge only. Pick the ESP32 Dev Module board. If it still fails, hold BOOT while uploading starts, then release.
  • Some pins never read right, or the board will not boot with a wire attached. ESP32-DevKitC V4
    GPIO34 to 39 are input only and have no pull-ups. GPIO6 to 11 run the flash, so leave them alone. GPIO0, 2 and 12 are boot pins; avoid pulling them the wrong way at reset.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.