Nano dial
A potentiometer on the Nano's A7, the analog-only pin an Uno does not have, fades an LED on pin 9. The whole build, an Arduino Nano 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 $31. On Mokxi they're free.
What you'll learn
- The Nano's A6 and A7 are analog only
- Scaling 10 bit input to 8 bit PWM with a bit shift
- Reading a knob to set brightness
- Reporting on a timer without pausing the loop
- Difficulty: Beginner
- Time: About 15 minutes
- Board: Arduino Nano
- Parts: 5
How it works
A potentiometer on the Nano's A7 fades an LED on pin 9. A7 is special: it is an analog only pin that an Uno does not have, so analogRead is the only way to use it. pinMode, digitalWrite and digitalRead do not work on it.
The sketch reads A7 as a number from 0 to 1023, shifts it right by two bits to get 0 to 255, and gives that to analogWrite on pin 9. Pin 9 has hardware PWM at 490 Hz, so the LED brightness follows the knob.
Every 250 ms it also prints the raw reading and the duty at 115200 baud, using millis() so the LED stays smooth.
Parts list
5 parts, plus the jumper wires. They retail for about $31; on Mokxi they're free.
- 1 × Arduino Nano
- 1 × Full-size breadboard
- 1 × PotentiometerIn Halloween Prop Kit, Starter Projects Kit.
- 1 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × LED, yellowIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
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 pin | Connects to |
|---|---|
| LED, yellow pin C | Arduino Nano pin GND |
| Resistor, 220 Ω pin 1 | Arduino Nano pin 9 |
| Potentiometer pin 1 | Arduino Nano pin GND |
| Potentiometer pin 3 | Arduino Nano pin 5V |
| Potentiometer pin 2 | Arduino Nano pin A7 |
| LED, yellow pin A | Resistor, 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.
// Dial: a potentiometer on A7, the pin an Uno does not have, fading an LED.
//
// A7 is ADC channel 7. On the Nano's TQFP package it is bonded out as an
// analogue input with no port pin behind it, so it can be read with
// analogRead and nothing else: pinMode, digitalWrite and digitalRead ignore
// it, and there is no internal pull-up to switch on. That is the one thing
// this board has that an Uno does not, so it is worth a sketch of its own.
//
// Wire the potentiometer's two ends to 5V and GND and its wiper to A7. The
// LED goes on pin 9 through a 220 ohm resistor to GND; pin 9 is OC1A, so
// analogWrite gives it hardware PWM at 490 Hz rather than a software loop.
const int DIAL_PIN = A7;
const int LAMP_PIN = 9;
unsigned long lastReport = 0;
void setup() {
Serial.begin(115200);
pinMode(LAMP_PIN, OUTPUT);
Serial.println("turn the dial on A7");
}
void loop() {
// 0..1023 from the ten-bit conversion, 0..255 for the eight-bit PWM.
int raw = analogRead(DIAL_PIN);
analogWrite(LAMP_PIN, raw >> 2);
unsigned long now = millis();
if (now - lastReport >= 250) {
lastReport = now;
Serial.print("A7 ");
Serial.print(raw);
Serial.print(" duty ");
Serial.println(raw >> 2);
}
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Starts serial at 115200, makes pin 9 an output, and prints "turn the dial on A7". A7 needs no pinMode.
void setup() {
Serial.begin(115200);
pinMode(LAMP_PIN, OUTPUT);
Serial.println("turn the dial on A7");
}loop sketch.ino
Reads A7, divides by four with raw >> 2, and writes it to the LED. A check on millis() prints "A7" and the duty every 250 ms without pausing the fade.
void loop() {
// 0..1023 from the ten-bit conversion, 0..255 for the eight-bit PWM.
int raw = analogRead(DIAL_PIN);
analogWrite(LAMP_PIN, raw >> 2);
unsigned long now = millis();
if (now - lastReport >= 250) {
lastReport = now;
Serial.print("A7 ");
Serial.print(raw);
Serial.print(" duty ");
Serial.println(raw >> 2);
}
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The serial monitor shows garbage This project
Set the baud rate to 115200 to matchSerial.begin(115200). - The LED does not respond This project
The knob wiper (middle pin) must go to A7, with the outer pins on 5V and GND. - 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.