# LDR LED connection with Arduino

Trying to connect a LDR to a LED with NodeMCU

The first code from Loes I am going to try:

```
/*
  created by David Cuartielles
  modified 30 Aug 2011
  By Tom Igoe
  modified 13 March 2020 
  By Loes Bogers
  This example code is in the public domain.
  http://www.arduino.cc/en/Tutorial/AnalogInput
*/

int sensorPin = A0;    // select the input pin for the potentiometer
int ledPin = D1;      // select the pin for the LED that has PWM (~) e.g pin D1 on Node MCU
int sensorValue = 0;  // variable to store the value coming from the sensor
int mappedValue = 0;  //store the mapped values

void setup() {

  // initialize serial communication with computer:
  Serial.begin(115200);     //make sure it matches with the number in the serial port

  // declare the ledPin as an OUTPUT:
  pinMode(ledPin, OUTPUT);
  pinMode(sensorPin, INPUT);
}

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);

//// FIND MIN & MAX RANGE FIRST, THEN COMMENT OUT
  // print values to serial to find lowest and highest values (min and max) in serial monitor
  Serial.println(sensorValue);      // met 10K voltage divider: bijv. range 100-850 zonder een fietslampje erbij (beter voor de video)

 /*    
//PUT THE VALUES YOU FOUND (YOUR_MIN, YOUR_MAX) INTO THE LINE BELOW (line 36)
//put your min value and max value (as seen in the monitor) and map to a range 0- 255 for output
  mappedValue = map(sensorValue, 100, 850, 0, 255); 

    // print values for debugging
    Serial.print("Old Value (no mapping) = ");
    Serial.print(sensorValue);
    Serial.print("\t");   // add a tab between the numbers
    Serial.print("New value (mapped) = ");
    Serial.println(mappedValue);

    // turn the ledPin on using the mappedvalue from the sensorpin
    analogWrite(ledPin, mappedValue);
*/

}
```

I tried to work with this schematic drawing of how the circuit should look like:

![Schematic drawing from Loes](/files/-M44ugc8siMBCyg7Mfzc)

I made this out of it:

![LDR test 1](/files/-M44v3w-UJp4m0D-gujt)

It didn't worked:

![error](/files/-M44vpvfw05Lq1ERJyVS)

I find the LDR LED really difficult so I'll try some old codes I had and re-install my board.

### Step 1

So I started with the basics again. Does my LED really works?

I ran an empty code to see if my LED really worked.&#x20;

```
void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}
```

And it worked!

### Step 2

I went back to the slides and found this image:

![LDR Settings](/files/-M45-taZW7eCPRdod7UI)

I wanted to run the same code i did previously and came across this error:

![USB port of the NodeMCU not found](/files/-M4508exLWzBHJvxrVrG)

The port of my NodeMCU is not found. I checked my settings and also saw that the serial print speeds were also not the same, that's why I couldn't run my code and open the serial monitor port. The serial port speed was around 4200. So I set everything back to 115200 and restarted my NodeMCU. It now connects again.&#x20;

But when I run the code again this happend:

![Nothing](/files/-M450zP2E2cMk2lNIBRU)

Well nothing happened. I send the code to my NodeMCU and I didn't got any variables. I run it again and it still didn't worked, so I started debugging my board.

I changed every piece of cable and finally I found out what was wrong and why my LDR sensor didn't gave any values: The LDR resistor had to be on the GND pin and I think it was not really pushed in it real good.&#x20;

My values are: 3 to 4 and sometimes 5. I still don't really trust it, but it does shows values, but I know it are not the right values.&#x20;

![LDR values](/files/-M452y5Y-U-tGdKdjvig)

I still don't trust the values but I'll go on with the code. So I adjusted my Min and max values on line 36 to 3 and 4.

```
/*
  created by David Cuartielles
  modified 30 Aug 2011
  By Tom Igoe
  modified 13 March 2020 
  By Loes Bogers
  This example code is in the public domain.
  http://www.arduino.cc/en/Tutorial/AnalogInput
*/

int sensorPin = A0;    // select the input pin for the potentiometer
int ledPin = D1;      // select the pin for the LED that has PWM (~) e.g pin D1 on Node MCU
int sensorValue = 0;  // variable to store the value coming from the sensor
int mappedValue = 0;  //store the mapped values

void setup() {

  // initialize serial communication with computer:
  Serial.begin(115200);     //make sure it matches with the number in the serial port

  // declare the ledPin as an OUTPUT:
  pinMode(ledPin, OUTPUT);
  pinMode(sensorPin, INPUT);
}

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);

//// FIND MIN & MAX RANGE FIRST, THEN COMMENT OUT
  // print values to serial to find lowest and highest values (min and max) in serial monitor
  Serial.println(sensorValue);      // met 10K voltage divider: bijv. range 100-850 zonder een fietslampje erbij (beter voor de video)

     
//PUT THE VALUES YOU FOUND (YOUR_MIN, YOUR_MAX) INTO THE LINE BELOW (line 36)
//put your min value and max value (as seen in the monitor) and map to a range 0- 255 for output
  mappedValue = map(sensorValue, 3, 4, 0, 255); 

    // print values for debugging
    Serial.print("Old Value (no mapping) = ");
    Serial.print(sensorValue);
    Serial.print("\t");   // add a tab between the numbers
    Serial.print("New value (mapped) = ");
    Serial.println(mappedValue);

    // turn the ledPin on using the mappedvalue from the sensorpin
    analogWrite(ledPin, mappedValue);


}
```

And my serial still says a lot of thinks with a really high speed:

![Try out 2](/files/-M456tz2dKIhU0qzvhyJ)

After I ran this code my computer crashed.

### Step 2 fersh start

I still didn't get why my LDR sensor was nog working. So I asked my boyfriend if he wanted to help me with a puzzle. I gave him the image of how the board must look like and all the necessities. He has no knowledge abut coding, adruino or anything like that, so it was a fresh start. He made the board look exactly as the image that I showed, which I had done in my previous attempts. I ran the code, and it worked! What!

![Values](/files/-M49UDV285jvZ9_E0Fkx)

I used the min value 560 and max value 611. It worked, but it's crashes my laptop. So I'm going to try it out with a more wider min max value, maybe that will work better.&#x20;

![on off](/files/-M49WDezsa_KGtRmgHzy)

```
/*
  created by David Cuartielles
  modified 30 Aug 2011
  By Tom Igoe
  modified 13 March 2020 
  By Loes Bogers
  This example code is in the public domain.
  http://www.arduino.cc/en/Tutorial/AnalogInput
*/

int sensorPin = A0;    // select the input pin for the potentiometer
int ledPin = D2;      // select the pin for the LED that has PWM (~) e.g pin D1 on Node MCU
int sensorValue = 0;  // variable to store the value coming from the sensor
int mappedValue = 0;  //store the mapped values

void setup() {

  // initialize serial communication with computer:
  Serial.begin(115200);     //make sure it matches with the number in the serial port

  // declare the ledPin as an OUTPUT:
  pinMode(ledPin, OUTPUT);
  pinMode(sensorPin, INPUT);
}

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);

//// FIND MIN & MAX RANGE FIRST, THEN COMMENT OUT
  // print values to serial to find lowest and highest values (min and max) in serial monitor
  Serial.println(sensorValue);      // met 10K voltage divider: bijv. range 100-850 zonder een fietslampje erbij (beter voor de video)
  
//PUT THE VALUES YOU FOUND (YOUR_MIN, YOUR_MAX) INTO THE LINE BELOW (line 36)
//put your min value and max value (as seen in the monitor) and map to a range 0- 255 for output
  mappedValue = map(sensorValue, 560, 611, 0, 255); 

    // print values for debugging
    Serial.print("Old Value (no mapping) = ");
    Serial.print(sensorValue);
    Serial.print("\t");   // add a tab between the numbers
    Serial.print("New value (mapped) = ");
    Serial.println(mappedValue);

    // turn the ledPin on using the mappedvalue from the sensorpin
    analogWrite(ledPin, mappedValue);


}
```

Trying it out with a flash light the values wil be min 560 and max 955:

```
/*
  created by David Cuartielles
  modified 30 Aug 2011
  By Tom Igoe
  modified 13 March 2020 
  By Loes Bogers
  This example code is in the public domain.
  http://www.arduino.cc/en/Tutorial/AnalogInput
*/

int sensorPin = A0;    // select the input pin for the potentiometer
int ledPin = D2;      // select the pin for the LED that has PWM (~) e.g pin D1 on Node MCU
int sensorValue = 0;  // variable to store the value coming from the sensor
int mappedValue = 0;  //store the mapped values

void setup() {

  // initialize serial communication with computer:
  Serial.begin(115200);     //make sure it matches with the number in the serial port

  // declare the ledPin as an OUTPUT:
  pinMode(ledPin, OUTPUT);
  pinMode(sensorPin, INPUT);
}

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);

//// FIND MIN & MAX RANGE FIRST, THEN COMMENT OUT
  // print values to serial to find lowest and highest values (min and max) in serial monitor
  Serial.println(sensorValue);      // met 10K voltage divider: bijv. range 100-850 zonder een fietslampje erbij (beter voor de video)
  
//PUT THE VALUES YOU FOUND (YOUR_MIN, YOUR_MAX) INTO THE LINE BELOW (line 36)
//put your min value and max value (as seen in the monitor) and map to a range 0- 255 for output
  mappedValue = map(sensorValue, 560, 611, 0, 255); 

    // print values for debugging
    Serial.print("Old Value (no mapping) = ");
    Serial.print(sensorValue);
    Serial.print("\t");   // add a tab between the numbers
    Serial.print("New value (mapped) = ");
    Serial.println(mappedValue);

    // turn the ledPin on using the mappedvalue from the sensorpin
    analogWrite(ledPin, mappedValue);


}
```

It worked a lot smoother then the code with a closer min and max value.&#x20;

![New values no flat light - flash light](/files/-M49VObYX9eVQ9Y04H6u)

![Filmed with flash light](/files/-M49VvrZ7mym0nWmL1Dq)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://minormakerslab.gitbook.io/students-1920/andrei/kim/6-coronaweek-untoolkit-electronic-inputs/ldr-led-connection-with-arduino.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
