How to read the value of a photoresistor and print it out in serial monitor

I need help on how to read the value of a photoresistor and print it out in serial monitor.

// C++ code
//
void setup()
{
  pinMode(12, OUTPUT);
  pinMode(A0, INPUT);
  Serial.begin(9600);
}

void loop()
{
  if (digitalRead(A0) == LOW)
  {
  	Serial.println("av");
    delay(5000);
  }
  if (digitalRead(A0) == HIGH)
  {
    Serial.println("Avlest lysmengde er: ");//print out lightlevel value
    digitalWrite(12, HIGH);
    delay(1000); // Wait for 1000 millisecond(s)
    digitalWrite(12, LOW);
    delay(1000); // Wait for 1000 millisecond(s)
  }
}

https://www.instructables.com/Read-a-Photoresistor-With-Analog-Input/#:~:text=Click%20to%20create%20a%20wire%20connecting%20one%20photoresistor,ground%2C%20and%20adjust%20its%20value%20to%204.7k%20ohms.

Why try use digital read with a photoresistor when the photoresistor puts out an analog value?

not tested but something

// C++ code
//
void setup()
{
  pinMode(12, OUTPUT);
  //pinMode(A0, INPUT);
  Serial.begin(9600);
}

void loop()
{
  int aoRead = analogRead(A0);
  
  if (aoRead > 1024/2 )
  {
    Serial.println("av");
    delay(5000);
  }
  if ( aoRead <= 1024/2)
  {
    Serial.print("Avlest lysmengde er:  ");//print out lightlevel value
Serialprint( aoRead );
Serial.println();
    digitalWrite(12, HIGH);
    delay(1000); // Wait for 1000 millisecond(s)
    digitalWrite(12, LOW);
    delay(1000); // Wait for 1000 millisecond(s)
  }
}

I want to know how much light detection it is on the photoresist and print it out in serialmonitor.

consider
find appropriate value for Thresh


#define PinLed  12
#define PinRes  A0

#define Thresh  500

void setup()
{
    Serial.begin(9600);
    pinMode(PinLed, OUTPUT);
}


void loop()
{
    int val = analogRead (PinRes);
    Serial.println (val);
    digitalWrite (PinLed, Thresh < val);
}



is it any way that if the photoresist is max a led flashes?

could do that, but i though you asked how to read sensor value. can you?

can i what?

How to read the value of a photoresistor and print it out in serial monitor

...

If you report the exact type number of your photoresistor, then some calculations could be made to find the amount of light it has detected against the resistance value it exhibits.

could you try to explain what happens in this code and how it works? so I can get a better understanding for future

no i cant.thats why im asking for help here

void loop()
{
  int aoRead = analogRead(A0);
  Serial.print( " a0Read = " );
Serial.print( a0read );
Serial.println();
  if (aoRead > 1024/2 )

are you asking for help to read the photoresistor value or

have you tried the code i posted to "read the photoresistor value"?

yes?

is that a question?

consider


#define PinLed  12
#define PinRes  A0

#define Thresh  500

unsigned long msecLst;

void setup()
{
    Serial.begin(9600);
    pinMode(PinLed, OUTPUT);
}

void loop()
{
    int val = analogRead (PinRes);
    Serial.println (val);

    unsigned long msec = millis ();
    if (Thresh > val)
        digitalWrite (PinLed, LOW);
    else if (msec - msecLst >= 200)  {
        msecLst = msec;
        digitalWrite (PinLed, ! digitalRead (PinLed));
    }
}

It's here
https://docs.arduino.cc/built-in-examples/basics/AnalogReadSerial

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.