• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

LDR on ESP32 2432S028R is not working

Q&A Forum › Category: ESP32 › LDR on ESP32 2432S028R is not working
0 Vote Up Vote Down
Mike Kümmerling asked 10 months ago

I checked the LDR on the board. The component works correct dependend on the light changed the voltage. The same voltage was on GPIO 34. But my little progam does not work. I get only zero.

void setup() {
  Serial.begin(115200);
 }

void loop() {
  Serial.println (analogRead(34));
  delay(500);
}

 
Is further initialization required?
 
BR, MIke
 

Question Tags: AnalogRead(34)
32 Answers
0 Vote Up Vote Down
Alex Chalmers answered 10 months ago

Me too, I only see zero Luminosity displayed.

0 Vote Up Vote Down
Mike Kümmerling answered 10 months ago

I checked the voltage over LDR R21 (GT36516). If the resistor is dark then I have 80mV max. It seams it is to few. I would expect about 600mV. R15 and R19 are mounted correct. I asume a bug on the board or inside of ESP-WROM-32 module.But also for 80mV, I should see a value higher than zero. 

0 Vote Up Vote Down
Sara Santos Staff answered 10 months ago

Hi.
I’m not sure. But I think it’s an hardware issue.
Also happened to us while testing. But when we pressed the LDR with our finger, it started working. So, I guess it must be soemthing related to the hardware not soldered properly.
Regards.
Sara

0 Vote Up Vote Down
Mike Kümmerling answered 10 months ago

Hi,
Yesterday I ordered another board from a different supplier and received it today and tried it out. The board has the same version “V1.2”. The PCB is from a different batch (“2403” instead of “2404” : manufacturing date on the PCB). Unfortunately, this board also shows the same faulty behavior. The luminosity remains zero. I could not measure any short circuit or interruption up to GPIO34. The circuit corresponds exactly to the circuit diagram. It is very unlikely that the same soldering error should be present here by chance. The solder joints are of the best quality and fully comply with the IPC 610 specifications.
 

0 Vote Up Vote Down
Sara Santos Staff answered 10 months ago

Hi again.~
Thanks for your feedback.
Maybe we are mistaken and the LDR doesn’t work at all in that scenario.
I’ll have to order some boards too and try it out.
At the moment, I’m out of the office, but I’ll take a look when I came back.
Regards,
Sara

0 Vote Up Vote Down
Mike Kümmerling answered 10 months ago

Thank you for your support

0 Vote Up Vote Down
Mike Kümmerling answered 10 months ago

Thank you for your support

0 Vote Up Vote Down
Dana Gregory answered 10 months ago

I am also experiencing this. The analogRead function is returning zero regardless of the amount of light falling on the LDR.
I tried a library, ESP32AnalogRead, which is available in the Arduino IDE Library Manager (Tools-> Manage Libraries…). With this library I get values from 140mv to 1500mv depending on the amount of light on the LDR.

0 Vote Up Vote Down
Mike Kümmerling answered 10 months ago

Hello Dana, I have tried the library you suggested. But without any success. I got a lot of compiler errors. What additional board manager URL did you use? I set it to “https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json”.

0 Vote Up Vote Down
Mike Kümmerling answered 10 months ago

correction: I set it to “https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json”. 

0 Vote Up Vote Down
Mike Kümmerling answered 10 months ago

correction: I set it to
https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
 
 

0 Vote Up Vote Down
Dana Gregory answered 10 months ago

board manager URL:
https://dl.espressif.com/dl/package_esp32_index.json
What errors are you seeing?

0 Vote Up Vote Down
Mike Kümmerling answered 10 months ago

I see:
ESP32AnalogRead.cpp:182:35: error: ‘ADC_WIDTH_12Bit’ was not declared in this scope;
ESP32AnalogRead.cpp:182:17: error: ‘adc1_config_width’ was not declared in this scope
ESP32AnalogRead.cpp:214:49: error: ‘ADC_ATTEN_11db’ was not declared in this scope
It looks like the right file of “esp_adc_cal.h” is missing.

0 Vote Up Vote Down
Alex Chalmers answered 10 months ago

I’m also getting the same errors using that ESP32AnalogRead library.

0 Vote Up Vote Down
Dana Gregory answered 10 months ago

On my system, ADC_WIDTH_12Bit is defined in “adc.h” which is here:
/Users/<user>/Library/Arduino15/packages/esp32/hardware/esp32/2.0.11/tools/sdk/esp32/include/driver/include/driver/adc.h
I see that “adc.h” is included by “esp_adc_cal.h” which is here:
/Users/<user>/Library/Arduino15/packages/esp32/hardware/esp32/2.0.11/tools/sdk/esp32/include/esp_adc_cal/include/esp_adc_cal.h
I have Arduino IDE 2.3.2 with “esp32” board manager 3.0.2
 

0 Vote Up Vote Down
Mike Kümmerling answered 10 months ago

I have Arduino IDE2.3.2 too and also “esp32” board manager 3.0.2. 
But I don’t have files and directories “/Library/Arduino15/….
I have the folder “Users/<user>/librarries/…. only. What for a package or library I have to add to get folder “Arduino15/…” ?

0 Vote Up Vote Down
Dana Gregory answered 10 months ago

I should say that I am on MacOS 12.7.5 Monterey.
I Googled “where does arduino15 folder come from” it says it comes from a standard Arduino IDE installation…

0 Vote Up Vote Down
Dana Gregory answered 10 months ago

In my sketch, when I hover over

#include <ESP32AnalogRead.h>

it shows /Users/<user>/Documents/Arduino/libraries/ESP32AnalogRead/ESP32AnalogRead.h
 
When I right-click on that and select “Go to definition…” it opens that file, ESP32AnalogRead.h
 
In ESP32AnalogRead.h if I hover over

#include “esp_adc_cal.h”

It shows /Users/<user>/Library/Arduino15/packages/esp32/hardware/esp32/2.0.11/tools/sdk/esp32/include/esp_adc_cal/include/esp_adc_cal.h
 
What do you see on your system?

0 Vote Up Vote Down
Mike Kümmerling answered 10 months ago

I decided to reinstall the IDE. The hover over doesn’t work. So I made backups and cleaned up the PC. I deleted also all arduino files inside of AppData and renamed my sketch-dir. I installed the IDE new, installed board manager and the ESP32analog read lib. I compiled and got the same errors. The hover over function works now so that I found the esp_adc_cal.h.
But where are the definitions “ADC_WIDTH_12Bit” and “adc1_config_width” stored on your system?

0 Vote Up Vote Down
Dana Gregory answered 10 months ago

On my system, ADC_WIDTH_12Bit is defined in “adc.h” which is here:
/Users/<user>/Library/Arduino15/packages/esp32/hardware/esp32/2.0.11/tools/sdk/esp32/include/driver/include/driver/adc.h

I see that “adc.h” is included by “esp_adc_cal.h” which is here:
/Users/<user>/Library/Arduino15/packages/esp32/hardware/esp32/2.0.11/tools/sdk/esp32/include/esp_adc_cal/include/esp_adc_cal.h

0 Vote Up Vote Down
Mike Kümmerling answered 10 months ago

I found “esp_adc_cal.h” in /Users/<user>/AppData/Local/Arduino15/packages/esp32/tools/esp32-arduino-libs/idf-release_v5.1-bd2b9390ef/esp32/include/esp_adc/deprecated/include/
There is a warning inside of this file: 

#if !CONFIG_ADC_SUPPRESS_DEPRECATE_WARN
#warning “legacy adc calibration driver is deprecated, please migrate to use esp_adc/adc_cali.h and esp_adc/adc_cali_scheme.h”
#endif

 
In my “esp_adc_cal,h”-file are the following includes:

#include <stdint.h>
#include “sdkconfig.h”
#include “esp_err.h”
#include “hal/adc_types.h”
#include “driver/adc_types_legacy.h”
#include “esp_adc_cal_types_legacy.h”

 
 
In file “adc_types_legacy.h” I found :

typedef enum {
#if CONFIG_IDF_TARGET_ESP32
    ADC_WIDTH_BIT_9  = 9, /*!< ADC capture width is 9Bit. */
    ADC_WIDTH_BIT_10 = 10, /*!< ADC capture width is 10Bit. */
    ADC_WIDTH_BIT_11 = 11, /*!< ADC capture width is 11Bit. */
    ADC_WIDTH_BIT_12 = 12, /*!< ADC capture width is 12Bit. */
#elif SOC_ADC_RTC_MAX_BITWIDTH == 12
    ADC_WIDTH_BIT_12 = 12, /*!< ADC capture width is 12Bit. */
#elif SOC_ADC_RTC_MAX_BITWIDTH == 13
    ADC_WIDTH_BIT_13 = 13, /*!< ADC capture width is 13Bit. */
#endif
    ADC_WIDTH_MAX,
} adc_bits_width_t;

 
But I did not found “ADC_WIDTH_12Bit”.
 

0 Vote Up Vote Down
Dana Gregory answered 10 months ago

I am using ESP-IDF 4.4.5 so that is the reason it works for me.
There is some information in the ESP32AnalogRead GitHub here:
https://github.com/madhephaestus/ESP32AnalogRead/issues/13
Sounds like there is direct support for this in the later versions of ESP-IDF.

0 Vote Up Vote Down
Mike Kümmerling answered 10 months ago

The code of the example worked, yes. But now I’m back to the beginning of the problem. The measurement results are 0 or in milliVolts 142 regardless of the light on LDR.
This is the example for oneshot reading:

void setup() {
  // initialize serial communication at 115200 bits per second:
  Serial.begin(115200);

  //set the resolution to 12 bits (0-4095)
  analogReadResolution(12);
}

void loop() {
  // read the analog / millivolts value for pin 34:
  int analogValue = analogRead(34);
  int analogVolts = analogReadMilliVolts(34);

  // print out the values you read:
  Serial.printf("ADC analog value = %d\n", analogValue);
  Serial.printf("ADC millivolts value = %d\n", analogVolts);

  delay(100);  // delay in between reads for clear read from serial
}

This say seriall monitor:
ADC analog value = 0
ADC millivolts value = 142
ADC analog value = 0
ADC millivolts value = 142



0 Vote Up Vote Down
Dana Gregory answered 10 months ago

I get 142 mV unless I put the device in a black box or it is night with no lights on.
Also, if the backlight of the display is on, always get 142 mV due to the spill over from the backlight to the sensor. It would need a baffle, like a black tube or something, to protect the sensor.
I have experimented with turning the backlight off before taking the reading and it has to be off for 2-4 seconds before the residual backlight illumination stops affecting the sensor.

0 Vote Up Vote Down
Mike Kümmerling answered 10 months ago

With the test program from above, the backlight remains off. From my point of view, reading GPIO 34 does not work on this module. Further initialization may be necessary. Something seems to be missing.
According to the data sheet of the LDR GT36516, it takes up to 10 seconds to display the correct measured value from 10 lux to 0 lux. So it is quite normal that you have to wait 2-4 seconds.

0 Vote Up Vote Down
Mike Kümmerling answered 10 months ago

I have now found the error and fixed it myself.
At first I wanted to rule out the possibility that the hardware was faulty.
LDR (R21) is connected to the GPIO34 and ground. Parallel to this is the voltage divider R15 and R19, each with 1MOhm.
I have connected a 51 kOhm resistor in parallel to R15 and have already obtained different measured values. The circuit now works depending on the incident light.
The LDR is therefore at least 20 times lower impedance than the GT36516. Obviously a different type was installed here. I now get an analog value of around 400 and a millivolt value of around 800mV in the dark.
Maybe someone could tell us what the analog value on his board is in the dark.

0 Vote Up Vote Down
Dana Gregory answered 10 months ago
Code
void setup()
{
  Serial.begin(115200);
  Serial.println("IDF " + String(IDF_VER));
}

void loop()
{
  Serial.print("Value = " + String(analogRead(34)) + "\t");
  Serial.println("Voltage = " + String(analogReadMilliVolts(34)) + "mv");
  delay(5000);
}

Output - first the device is in normal room light,
then I put it in a box and close the box,
finally I wrap the box in a black shirt

IDF v5.1.4-358-gbd2b9390ef-dirty
Value = 0 Voltage = 142mv
Value = 0 Voltage = 142mv
Value = 1063 Voltage = 1027mv
Value = 1418 Voltage = 1318mv
Value = 1519 Voltage = 1387mv
Value = 1535 Voltage = 1425mv
Value = 1600 Voltage = 1463mv
Value = 1647 Voltage = 1501mv
Value = 1667 Voltage = 1518mv
Value = 1680 Voltage = 1529mv
Value = 1683 Voltage = 1534mv
Value = 1687 Voltage = 1538mv
0 Vote Up Vote Down
Alex Chalmers answered 10 months ago

Thx Dana, that simple program works on my system also. Hmm so perhaps the includes are messing up the config somehow ?  My results  :
Sensor Covered :

Value = 1089	Voltage = 1016mv


Sensor uncovered :
Value = 0	Voltage = 142mv
0 Vote Up Vote Down
Alex Chalmers answered 10 months ago

Aargh !  If I cover the LDR device and run the as-downloaded 3_4 Table program I do get  an initial LDS Luminosity sensor value of 1126 reported.
It doesnt change though. So now I think the device is working and theres a sw issue in the code.

0 Vote Up Vote Down
Dana Gregory answered 10 months ago

If you are running the table program then the backlight of the display is on. The backlight spills over into the LDR sensor.
If the backlight of the display is on, always get 142 mV due to the spill over from the backlight to the sensor. It would need a baffle, like a black tube or something, to protect the sensor.
I have experimented with turning the backlight off before taking the reading and it has to be off for 2-4 seconds before the residual backlight illumination stops affecting the sensor.

0 Vote Up Vote Down
Alex Chalmers answered 10 months ago

Well done Mike and Dana ! I see,  the backlight escapes from the side of the display, saturates the LDR and it reads 0. BTW I can block a good portion of that backlight by putting a small piece of electrical tape on the edge of the display and onto the PCB to the left of the LDR. Also I get a useful working range out of the device if I COVER the LDR with a small piece of electrical tape (poweron in low daylight : 563, cover device with card :1205, expose to mobile phone flashlight : 85 ).
And yes apparently it can take seconds for the exotic material in the LDR to respond to removal of light (weird science).

0 Vote Up Vote Down
Mike Kümmerling answered 10 months ago

I have also found that the circuit board is a very good light conductor. It is not enough just to cover the LDR. The entire assembly must be placed in a light-tight box. But without an additional resistor, I can only achieve an analog value of 95 or 200mV at best. With an additional resistor, the measuring range is significantly larger. Then I have an analog value of 3200 and 2700mV. With additional resistor I get values above 142mV even in daylight.
I have the same IDF-version.

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.