LDR LED connection with Arduino

Week 6 and 8

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:

I made this out of it:

It didn't worked:

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.

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:

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

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.

But when I run the code again this happend:

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.

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.

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:

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!

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.

/*
  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.

Last updated