Nano: the analog-only pins A6 and A7
Read a knob on A7, one of the two Nano pins that can only measure voltage, and use it to dim an LED on a PWM pin.
Basic · Beginner
In a real product: Guitar effects pedal (Consumer electronics): Knobs on analog pins set each effect's level.
The Nano is an Uno's chip on a board half the size, with two extra pins the Uno does not break out: A6 and A7. They are connected to the analog-to-digital converter only. There is no digital input or output circuitry behind them, so digitalRead() and pinMode(OUTPUT) do nothing useful there.
What you'll learn
- That A6 and A7 are analog inputs only, unlike A0 to A5, which can also be digital pins
- Reading a potentiometer with
analogRead(): 0 to 1023 for 0 to 5 V - Scaling the reading to a PWM duty with
map()and writing it withanalogWrite() - Which Nano pins have PWM: 3, 5, 6, 9, 10 and 11
How it works
The potentiometer's ends go to 5 V and GND and its wiper to A7. The sketch reads the wiper's voltage, scales 0-1023 down to 0-255 and writes that to pin 9, which produces a PWM signal: the LED's brightness follows the knob. It also prints the reading when it changes by more than a few counts, so the monitor is not flooded.
Try this
- Move the LED to a pin that has no PWM (pin 8) and see why
analogWrite()only gives fully on or off there. - Read A6 as well and average the two knobs.
- Light the LED on a curve instead of a straight line with a lookup table: the eye is not linear.
sketch.ino
// A potentiometer on A7 (an analog-only Nano pin) sets an LED's brightness on PWM pin 9.
const int KNOB = A7;
const int LED = 9;
int lastReading = -100;
void setup() {
Serial.begin(9600);
pinMode(LED, OUTPUT);
}
void loop() {
int reading = analogRead(KNOB); // 0..1023
analogWrite(LED, map(reading, 0, 1023, 0, 255));
if (abs(reading - lastReading) > 8) { // only report real changes
Serial.print("A7 = ");
Serial.println(reading);
lastReading = reading;
}
delay(20);
}