Analog & PWM Cheatsheet
Analog & PWM Cheatsheet
analogRead, analogWrite, map() ߀” sensors and variable outputs.
Analog Input
| analogRead(A0) | Read voltage on pin A0 ߀” returns 0 (0 V) to 1023 (5 V) |
| analogRead(A0) * (5.0 / 1023.0) | Convert raw ADC value to actual voltage in volts |
| Pins: A0, A1, A2, A3, A4, A5 | Analog input pins on Arduino Uno ߀” no pinMode() call required |
| Resolution: 10-bit ߆’ 0 to 1023 | Each step ߉? 4.9 mV on a 5 V Arduino |
PWM Output
| analogWrite(pin, value) | Output a PWM signal ߀” value 0 (off) to 255 (fully on) |
| PWM pins (Uno): 3, 5, 6, 9, 10, 11 | Only these pins support analogWrite ߀” marked with ~ on the board |
| analogWrite(9, 128) | ~50% duty cycle ߀” LED at half brightness, motor at half speed |
| PWM frequency ߉? 490߀“980 Hz on Uno | Fast enough that LEDs and motors respond as if to an average voltage |
Scaling Values
| map(x, inLow, inHigh, outLow, outHigh) | Rescale x from one range to another ߀” returns a long integer |
| map(pot, 0, 1023, 0, 255) | Map pot reading to PWM range ߀” link a knob to LED brightness |
| constrain(x, low, high) | Clamp x to [low, high] ߀” prevents out-of-range values |
| abs(x) | Absolute value of x |