ESP8266 NodeMCU – Simple LDR Lux Meter

In this tutorial, we will learn how to use an LDR resistor to make a simple lux meter and display the light intensity. A lux meter is a device that measures illuminance and luminous emittance using the SI unit of lux.  It effectively measures the amount of power from the light falling on a given unit of area. A simpler way to describe a lux meter is to say that it measures how bright the light falling on the sensor is. In this project, we will characterize an LDR and then build a lux meter.

Parts Required

To build the global setup you must have the following parts:

Note: You do not need to have a NodeMCU board nor an ESP8266 to follow this tutorial. I am using this board because it is very famous and also because I will post future tutorials using wifi communications. In order to use other boards, you just have to change the pin number.


Figure 1

The light sensor for a lux meter can be one of several different types of sensors, including photodiodes and phototransistors, but the easiest to use and often the most readily available type of sensor is an LDR. An LDR, or Light Dependent Resistor, is a passive component. As you would expect, the resistance of an LDR changes as the amount of light falling on it changes. If you can measure the resistance of the LDR and you know the characteristics of your particular LDR, you can quantify the amount of lux falling on the LDR. In general, the brighter the light, the lower the resistance, but unfortunately, the relationship between resistance and lux for an LDR is not a nice linear relationship. It is instead an exponential relationship which is a little trickier to deal with. In the next step, we will characterize the LDR.

Characterizing the LDR

Plug the LDR and ensure that the flat part of the sensor is parallel to the ground. Connect the Multimeter to the leads of the LDR and set it to measure resistance. Place beside the LDR, a smartphone with a lux meter app to acquire the lux. Make sure that the light falls equally on the LDR and on the smartphone. While ensuring that the same levels of light fall on the two sensors, record a reading of the LDR resistance and the smartphone app lux.  Repeat this process for many different lighting levels from very dark to very bright.

Figure 2


Figure 3

Follow figure 2 and connect the LDR and 10k resistor to the NodeMCU pins. To measure a resistance change we have to send a current in the component between two potentials. Therefore create a voltage divider using LDR and a 10kOhm resistor. Everything will be powered from your PC using a micro-USB to USB cable. Let’s try it.


Copy the main sketch bellow to your Arduino IDE project and upload it. Have fun!!

/* LDR Luximeter */
// Constants
#define VIN 3.3 // V power voltage, 3.3v in case of NodeMCU
#define R 10000 // Voltage devider resistor value

// Parameters
const int Analog_Pin = 0; // Analog pin A0

int LDR_Val; // Analog value from the LDR
int Iluminance; //Lux value

void setup(void) {

void loop(void) { 
  LDR_Val = analogRead(Analog_Pin);
  Iluminance = conversion(LDR_Val);
  Serial.print("Analog reading from sensor = "); 
  Serial.println(LDR_Val); // between 0 - 
  Serial.print("Iluminance value = ");
  Serial.print(Iluminance); // Converted value
  Serial.println(" Lux"); 

int conversion(int raw_val)
  // Conversion rule
  float Vout = float(raw_val) * (VIN / float(1023));// Conversion analog to voltage
  float RLDR = (R * (VIN - Vout))/Vout; // Conversion voltage to resistance
  int lux = 500/(RLDR/1000); // Conversion resitance to lumen
  return lux;

Leave a Reply

Your email address will not be published. Required fields are marked *