Built-in project · Arduino Pro Mini, 3.3 V 8 MHz

Pro Mini at 3.3 V

A knob across 3.3 V on A7 fading an LED on pin 9. A full turn reads 1023, which is 3.3 V on this board, and the sketch keeps time at 8 MHz. The whole build, a 3.3 V Arduino Pro Mini and 3 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 $9.65. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Reading a knob with analogRead
  • Fading an LED with PWM
  • A reading is a fraction of the supply, not volts
  • Why a 3.3 V board needs the right supply value
  • Difficulty: Beginner
  • Time: About 15 minutes
  • Board: Arduino Pro Mini, 3.3 V 8 MHz
  • Parts: 5

How it works

A knob on A7 fades a yellow LED on pin 9. The knob's ends go to VCC and GND, and on this board VCC is 3.3 V. The LED has a 220 ohm resistor.

The sketch reads the knob with analogRead and shifts the result right by two bits. That turns 0 to 1023 into 0 to 255, which analogWrite uses for brightness on pin 9.

Every 250 ms it also prints the raw reading and the same value in millivolts. A full turn still reads 1023, because the converter measures against the same 3.3 V supply. So the sketch must know the supply is SUPPLY_MV, 3300.

Parts list

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

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
LED, yellow pin CArduino Pro Mini, 3.3 V 8 MHz pin GND
Resistor, 220 Ω pin 1Arduino Pro Mini, 3.3 V 8 MHz pin 9
Potentiometer pin 3Arduino Pro Mini, 3.3 V 8 MHz pin GND
Potentiometer pin 1Arduino Pro Mini, 3.3 V 8 MHz pin VCC
Potentiometer pin 2Arduino Pro Mini, 3.3 V 8 MHz pin A7
LED, yellow pin AResistor, 220 Ω pin 2

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Pro Mini, 3.3 V 8 MHz
// Dial: a potentiometer on A7 fades an LED on pin 9, and reports volts.
//
// The potentiometer's ends go to VCC and GND, and on this board VCC is 3.3 V.
// The ADC measures against AVCC, which is the same rail, so a full turn still
// reads 1023: the count is a fraction of the supply, not a number of volts.
// To turn it back into volts the sketch has to know the supply, and that is
// the line a sketch copied from a 5 V board gets wrong.
//
// A7 is an analogue-only input, as on the Nano: analogRead reaches it and
// nothing else does. Pin 9 is OC1A, so analogWrite is hardware PWM, at
// 245 Hz here rather than 490 Hz, because the timer runs from an 8 MHz clock.

const int DIAL_PIN = A7;
const int LAMP_PIN = 9;
const long SUPPLY_MV = 3300;

unsigned long lastReport = 0;

void setup() {
  Serial.begin(57600);
  pinMode(LAMP_PIN, OUTPUT);
  Serial.println("turn the dial on A7");
}

void loop() {
  int raw = analogRead(DIAL_PIN);
  analogWrite(LAMP_PIN, raw >> 2);

  unsigned long now = millis();
  if (now - lastReport >= 250) {
    lastReport = now;
    long mv = (long)raw * SUPPLY_MV / 1023;
    Serial.print("A7 ");
    Serial.print(raw);
    Serial.print("  ");
    Serial.print(mv);
    Serial.println(" mV");
  }
}

Code walkthrough

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

setup sketch.ino

It starts the serial port at 57600 and sets the lamp pin as an output. Then it prints a message telling you to turn the dial on A7.

void setup() {
  Serial.begin(57600);
  pinMode(LAMP_PIN, OUTPUT);
  Serial.println("turn the dial on A7");
}

loop sketch.ino

It reads A7 and writes the reading shifted right by two bits to pin 9 with analogWrite. Every 250 ms it converts the reading to millivolts using SUPPLY_MV, and prints the raw value and the millivolts.

void loop() {
  int raw = analogRead(DIAL_PIN);
  analogWrite(LAMP_PIN, raw >> 2);

  unsigned long now = millis();
  if (now - lastReport >= 250) {
    lastReport = now;
    long mv = (long)raw * SUPPLY_MV / 1023;
    Serial.print("A7 ");
    Serial.print(raw);
    Serial.print("  ");
    Serial.print(mv);
    Serial.println(" mV");
  }
}

Troubleshooting

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

  • The printed millivolts are wrong. This project
    A sketch copied from a 5 V board uses 5000. On this board the supply is 3.3 V, so SUPPLY_MV must be 3300.
  • The LED does not fade. This project
    Check the knob's middle pin goes to A7 and the LED is on pin 9, the PWM pin, through the 220 ohm resistor.
  • 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 LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
    Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
  • A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
    Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground.
  • The LED does not light, or it lights once and then dies. LED
    LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin.
  • A blue or white LED stays dark on 3.3 V. LED
    Blue and white LEDs drop about 3 V, so 3.3 V leaves almost nothing for the resistor. Use a 5 V supply, or a smaller resistor and a red or green LED.

Change it and keep it

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