potentiometre-plotter.ino
The 10-bit converter reads the wiper from 0 to 1023; the IDE serial plotter draws the curve live. Useful for any analog sensor (LDR, TMP36, joystick).
Hardware required
An Arduino Mega 2560 (ATmega2560, 16 MHz, 5 V): 54 digital I/O, 15 PWM, 16 analog inputs, 4 hardware UARTs. 1 10 kΩ potentiometer.
Wiring
Potentiometer ends to 5 V and GND, wiper to A0.
How it works
analogRead samples the voltage divider formed by the potentiometer; conversion is referenced to 5 V by default. In the Arduino IDE, select « Arduino Mega 2560 » and the port, then Upload. PWM on 2-13 and 44-46; UART1=18/19, UART2=16/17, UART3=14/15; SPI on 50-53.
Going further
Swap the potentiometer for an LDR + resistor for a light meter, or a TMP36 for a thermometer.
potentiometre-plotter.ino
// Lecture 10 bits d'un potentiometre sur A0, trace serie.
const int POT = A0;
void setup() {
Serial.begin(9600);
}
void loop() {
int brut = analogRead(POT); // 0..1023
float tension = brut * (5.0 / 1023.0); // vers 0..5 V
Serial.print(brut);
Serial.print('\t');
Serial.println(tension, 2);
delay(50);
}