My photoresistor and OLED project not working

Hello, I have made a project using OLED display and photoresistor. It is supposed to display a message on the OLED display based on the level of sunlight, which is measured by the photoresistor. Although the code compiles, I think there is an issue because it doesn't work. I am sure there is no hardware issue because both components work with other codes, just not mine.
Here is the code:


#include <U8glib.h>
int sensorPin = A0;   
int sensorValue = 0;  
U8GLIB_SH1106_128X64  MyOled(U8G_I2C_OPT_NONE);

void draw0(void) {
  MyOled.setFont(u8g_font_unifont);
  MyOled.drawStr( 0, 10, "Today is ");
  MyOled.drawStr( 0, 30, "very cloudy!");
}

void draw1(void) {
  MyOled.setFont(u8g_font_unifont);
  MyOled.drawStr( 0, 10, "Today is ");
  MyOled.drawStr( 0, 30, "cloudy!");
}

void draw2(void) {
  MyOled.setFont(u8g_font_unifont);
  MyOled.drawStr( 0, 10, "Today is ");
  MyOled.drawStr( 0, 30, "a little sunny!");
}

void draw3(void) {
  MyOled.setFont(u8g_font_unifont);
  MyOled.drawStr( 0, 10, "Today is ");
  MyOled.drawStr( 0, 30, "sunny!");
}

void draw4(void) {
  MyOled.setFont(u8g_font_unifont);
  MyOled.drawStr( 0, 10, "Today is ");
  MyOled.drawStr( 0, 30, "very sunny!");
}
void setup() {
 pinMode(sensorPin, INPUT);
}
 
void loop() {
 
  sensorValue = analogRead(sensorPin);
  if(sensorValue>500){
     MyOled.firstPage();
     MyOled.setColorIndex(1);
do {
     draw4();
    }while( MyOled.nextPage() );
    
  }

  else if((sensorValue>400)&&(sensorValue<500)){
           MyOled.firstPage();
     MyOled.setColorIndex(1);
do {
     draw3();
    }while( MyOled.nextPage() );
  }
  else if((sensorValue<300)&&(sensorValue>200)){
     MyOled.firstPage();
     MyOled.setColorIndex(1);
do {
     draw2();
    }while( MyOled.nextPage() );
    
  }

  else if((sensorValue<200)&&(sensorValue>100)){
           MyOled.firstPage();
     MyOled.setColorIndex(1);
do {
     draw1();
    }while( MyOled.nextPage() );
   
  }

  else if(sensorValue<100){
               MyOled.firstPage();
     MyOled.setColorIndex(1);
do {
     draw0();
    }while( MyOled.nextPage() );
  }
}

Please help.

Please describe what you expect the program to do, and what it does instead.

Please post a picture of a hand drawn wiring diagram, with pins, parts and connections clearly labeled. A close up, focused picture of your setup would be useful, too, as there may be a wiring error.

That phrase conveys no useful information. Please elaborate. Does the display show anything? Is the data weird?

Post photos if they may help.

Your best debugging tool is the serial print. Use serial prints, to the serial monitor, to follow code execution and monitor variable values.

1 Like

It is supposed to display different messages on the OLED display, for example, if the analog value of the sensor is more than 500, it will say "Today is very sunny!" etc. however, it allways displays the same message, " Today is very cloudy!" even when the sensor's value is above 500. I don't think a wiring diagram would be necessary, as I already said that my components are working fine (and tested them with different codes).

The problem may exist in your circuit connection too. Please troubleshoot part by part. First check the photoresistor part. Just run a simple code. Print its output value to the serial monitor to see if it is really responding to light intensity. Then check your OLED by printing a 'Hello world' message.

How do you know what the "sensor's value" is?

Put in a Serial.print statement to find out.

You have given us no evidence in support of your claim that the circuit is working properly. We hear such claims often, and they are often wrong.

Thank you for your advice. I have already done this.

Yes I have done this multiple times. I use the light in my room as a tester. According to the serial monitor, the value of the sensor is around 600 when the light is on, and around 200 when the light is off.

Try putting MyOled.setColorIndex(1); inside the do loop

Yes, it compiles without any changes.

cloudy

The sensor value mapping is upside down. That is to say, the low lux values are calling "Very Sunny" and the high lux values are calling "Cloudy"

Check your wiring. Probably a jumper or LDR on the wrong row of a breadboard.

Thanks for helping, I have thoroughly checked the wiring. I think its a program issue.

The code works. Maybe a broken LDR or your threshold values need adjusting. Per my previous email, the mapping is upside down (max lux reports cloudy, min lux reports sunny).

I hope you realize you have no need for the sensor to test the rest of the code. Skip the analog read, plug in simple values(try values above and below any conditional thresholds), and see what your code does. Then, add in the read, but also use Serial Monitor to track what your analog read is giving you for values.
Or, add the analog readout to your displayed "conditions". For example, "LDR 102 cloudy today" would give you an idea of what's going on in the code.

Hi,
Do you have a DMM, Digital MultiMeter.

Check that the output voltage from the sensor is proportional to the light level it receives?

Using the IDE serial monitor and serial print statements will also help.

Thanks.. Tom.... :smiley: :+1: :coffee: :australia:

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