/* * Voltage divider with LDR * * Circuit: * 5V (Vcc) - LDR - R1 - GND * The node between LDR and R1 (V1, voltage drop across R1) is connected to analog input A0 * R1 arbitrary, e.g. 10kOhms * * The voltage V1 is converted to a digital number (dn) by means of an analog-to-digital converter (ADC). * The range of dn: 0 <= dn <= 1023 (10 bit resolution) * * Conversion: * V1 = 0V -> dn = 0 * V1 = 5V -> dn = 1023 * * Reconstruction of voltage from digital number: * V = dn / 1023 * 5V * */ int AIN = A0; // Analog input pin number void setup() { // put your setup code here, to run once: Serial.begin(115200); Serial.println("dn V mV"); } void loop() { // put your main code here, to run repeatedly: int dn = analogRead(AIN); // digital integer number between 0 and 1023 (10 bits, 2^10 different values) float V = (float)dn / 1024 * 5.0; // convert dn to float first, then divide by 1024 int mV = V*1000; // millivolts, converted from float Serial.print(dn); // print to serial without a newline at the end, i.e. the next print is in the same line Serial.print(" "); // spaces Serial.print(V); Serial.print(" "); Serial.println(mV); // last print with newline, i.e. the next print will be in the next line. delay(100); // pause in ms, 100 ms means 10 meas / sec. }