MuTech RoboticsLearning Hub
← Back to learning path

Pins: digital, analog, PWM

8 min read

A board talks to the world through its pins. Each pin can work in one of three main ways.

Digital: on or off

A digital pin is either HIGH or LOW. Use it for buttons, buzzers, relays and LEDs.

digitalWrite(13, HIGH);

Analog: a range of values

An analog pin measures a voltage. Use it for sensors that change smoothly, such as a light sensor or soil sensor.

  • Nano: 0 to 1023, using A0 to A7.
  • ESP32: 0 to 4095, on pins such as D32 to D35.

int light = analogRead(A0);

PWM: fake analog output

PWM switches a pin on and off very fast. The share of time it stays on is the duty cycle. At 50%, an LED looks half as bright and a motor runs slower.

analogWrite(9, 128);

On a Nano, PWM works on D3, D5, D6, D9, D10 and D11. The value runs from 0 to 255.

Rules that protect your board

  • A Nano pin works at 5 V. An ESP32 pin works at 3.3 V and must never get 5 V.
  • Keep each pin under 20 mA. Use a driver for motors, relays and bright lights.
  • Join the grounds of all your supplies together.
  • On an ESP32, D34 to D39 are input only.
  • When Wi-Fi is on, use only the ADC1 pins (D32 to D39) for analog reading.

A floating input reads random values. Use a pull-up or pull-down, or INPUT_PULLUP in your code.

Quick quiz

1. What is the highest safe voltage on an ESP32 pin?

2. analogWrite(9, 128) gives a duty cycle of about…

3. Which pin type suits a push button?

Score: 0 / 3
Sign in to track progress
Pins: digital, analog, PWM · MuTech Robotics