A darkroom enlarger lightmeter

Hello,

My name is Phil and I am a total newbie, and am seeking help from anyone.

I am hoping to build a working photographic darkroom enlarger lightmeter. I found an Arduino sketch for a meter that uses a BH1750 light sensor and a SSD1306 LCD display.

I have installed the Arduino IDE v2.3.10 and the required libraries.

Being absolutely new to Arduino I don't have any knowledge of the programming. This is where I am asking for some help.

So, I pasted the code that I found on the web into the IDE and tred to verify it. This came up with errors that I do not understand.

As a new user I am not allowed to add an attachment, so here is the code copied from the IDE:-

void loop() {

  // put your main code here, to run repeatedly:

#include <Wire.h>

#include <hp_BH1750.h>

#include <Adafruit_GFX.h>

#include <Adafruit_SSD1306.h>


#define SCREEN_WIDTH 128

#define SCREEN_HEIGHT 64

#define OLED_RESET    -1

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);


hp_BH1750 lightMeter;


// Calibration variables

const int buttonPin = 2; 

float referenceLux = 1.0;   // Default baseline

float referenceTime = 10.0; // Your standard base exposure time (e.g., 10 seconds)

bool isCalibrated = false;


void setup() {

  pinMode(buttonPin, INPUT_PULLUP);

  Wire.begin();

  
  // Initialize OLED Display

  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { 

    for(;;); // Halt if screen fails

  }

  display.clearDisplay();

  display.setTextColor(SSD1306_WHITE); // Use red acrylic overlay if display isn't natively red

  

  // Initialize BH1750 Light Sensor

  // One-time high res mode allows maximum sensitivity in low darkroom light

  lightMeter.begin(hp_BH1750::ONE_TIME_HIGH_RES_MODE); 

}


void loop() {

  // Read current light value on the easel

  float currentLux = lightMeter.readLightLevel();

  
  // Check if Calibration Button is pressed (Active LOW)

  if (digitalRead(buttonPin) == LOW) {

    if (currentLux > 0.01) { // Prevent dividing by zero in pitch black

      referenceLux = currentLux;

      isCalibrated = true;

      
      display.clearDisplay();

      display.setTextSize(1);

      display.setCursor(0,0);

      display.print("CALIBRATED!");

      display.setCursor(0,20);

      display.print("Ref Lux: "); display.print(referenceLux);

      display.display();

      delay(1500); // Debounce and message confirmation

    }

  }


  // Calculate new target exposure time based on the light delta

  float calculatedTime = referenceTime \* (referenceLux / currentLux);


  // Update Darkroom Display

  display.clearDisplay();

  display.setTextSize(1);

  display.setCursor(0, 0);

  display.print("CURRENT LUX: "); display.print(currentLux, 2);

  
  display.setCursor(0, 20);

  if (isCalibrated) {

    display.setTextSize(2);

    display.print("TIME: "); 

    display.print(calculatedTime, 1);

    display.print("s");

  } else {

    display.print("Uncalibrated");

    display.setCursor(0, 45);

    
display.setTextSize(1);

    display.print("Press button to set baseline");

  }

  
  display.display();

  
  // Re-trigger sensor configuration for the next reading loop

  lightMeter.configure(hp_BH1750::ONE_TIME_HIGH_RES_MODE);

  delay(200); 

}

}

This is the error message:-
C:\Users\phill\Documents\Arduino\Shetches\sketch_lightmeter_3\sketch_lightmeter_3.ino: In function 'void loop()':
C:\Users\phill\Documents\Arduino\Shetches\sketch_lightmeter_3\sketch_lightmeter_3.ino:21:14: error: a function-definition is not allowed here before '{' token
void setup() {
^
C:\Users\phill\Documents\Arduino\Shetches\sketch_lightmeter_3\sketch_lightmeter_3.ino:37:13: error: a function-definition is not allowed here before '{' token
void loop() {
^
exit status 1

Compilation error: a function-definition is not allowed here before '{' token

Could you possibly help please?

Somehow you got an extra loop() statement at the very beginning.
Delete these two lines

void loop() {

  // put your main code here, to run repeatedly:

AND
This last bracket at the end

  
  // Re-trigger sensor configuration for the next reading loop

  lightMeter.configure(hp_BH1750::ONE_TIME_HIGH_RES_MODE);

  delay(200); 

}

} **<---------**

Thank you jim-p,

I deleted the two lines at the top of the code and the last curly bracket. It still does not compile, the error message is this:-

C:\Users\phill\Documents\Arduino\Shetches\sketch_lightmeter_3\sketch_lightmeter_3.ino: In function 'void setup()':
C:\Users\phill\Documents\Arduino\Shetches\sketch_lightmeter_3\sketch_lightmeter_3.ino:32:31: error: 'ONE_TIME_HIGH_RES_MODE' is not a member of 'hp_BH1750'
   lightMeter.begin(hp_BH1750::ONE_TIME_HIGH_RES_MODE);
                               ^~~~~~~~~~~~~~~~~~~~~~
C:\Users\phill\Documents\Arduino\Shetches\sketch_lightmeter_3\sketch_lightmeter_3.ino: In function 'void loop()':
C:\Users\phill\Documents\Arduino\Shetches\sketch_lightmeter_3\sketch_lightmeter_3.ino:37:33: error: 'class hp_BH1750' has no member named 'readLightLevel'
   float currentLux = lightMeter.readLightLevel();
                                 ^~~~~~~~~~~~~~
C:\Users\phill\Documents\Arduino\Shetches\sketch_lightmeter_3\sketch_lightmeter_3.ino:82:14: error: 'class hp_BH1750' has no member named 'configure'
   lightMeter.configure(hp_BH1750::ONE_TIME_HIGH_RES_MODE);
              ^~~~~~~~~
C:\Users\phill\Documents\Arduino\Shetches\sketch_lightmeter_3\sketch_lightmeter_3.ino:82:35: error: 'ONE_TIME_HIGH_RES_MODE' is not a member of 'hp_BH1750'
   lightMeter.configure(hp_BH1750::ONE_TIME_HIGH_RES_MODE);
                                   ^~~~~~~~~~~~~~~~~~~~~~
exit status 1

Compilation error: 'ONE_TIME_HIGH_RES_MODE' is not a member of 'hp_BH1750'

I really wish that I could understand this. Could you possibly elucidate?

Thanks
Phil

Often there is more than one version of a library available for a particular sensor or other piece of hardware, from different library authors. Often these authors are unaware of each other and give their libraries the same or very similar names. These libraries won't be exactly the same as each other and code written for one library may not be compatible with other libraries.

I suspect you have found some code that was written for a different library than the one you have installed.

Almost all libraries come with some example code that gets installed into the examples menu in the IDE when you install the library. So look in the examples menu for "BH1750" or similar and in the sub menu you should find some example code compatible with the library you installed.

Maybe it's time to give a link to where you found this code as it does not seem to work with that library.

Here's how I locate errors in my code...

First, read entry of the error report... this one shows an error when referencing a library...
(one excerpt: error: ONE_TIME_HIGH_RES_MODE' is not a member of 'hp_BH1750')

Next, because the error says something about the library, "search" on the library shown in the error...

Be sure to be EXACT on the library naming... which is all lower case, with an underscore... which is this ".h" file of the library... (the .h file contains these #define values)...

https://github.com/Starmbi/hp_BH1750/blob/master/src/hp_BH1750.h

Looking back at the error and comparing it to the library, I can see "HIGH_RES_MODE" is slightly different than the library...

enum BH1750Quality
{
  BH1750_QUALITY_HIGH = 0x20,
  BH1750_QUALITY_HIGH2 = 0x21,
  BH1750_QUALITY_LOW = 0x23,
};

So... either the library has changed, or you are using the wrong library... let's search on this part of the error:

ONE_TIME_HIGH_RES_MODE

The search finds THIS library...

https://github.com/claws/BH1750/blob/master/src/BH1750.h
  enum Mode {
    // same as Power Down
    UNCONFIGURED = 0,
    // Measurement at 1 lux resolution. Measurement time is approx 120ms.
    CONTINUOUS_HIGH_RES_MODE = 0x10,
    // Measurement at 0.5 lux resolution. Measurement time is approx 120ms.
    CONTINUOUS_HIGH_RES_MODE_2 = 0x11,
    // Measurement at 4 lux resolution. Measurement time is approx 16ms.
    CONTINUOUS_LOW_RES_MODE = 0x13,
    // Measurement at 1 lux resolution. Measurement time is approx 120ms.
    ONE_TIME_HIGH_RES_MODE = 0x20, // <=== HERE IT IS
    // Measurement at 0.5 lux resolution. Measurement time is approx 120ms.
    ONE_TIME_HIGH_RES_MODE_2 = 0x21,
    // Measurement at 4 lux resolution. Measurement time is approx 16ms.
    ONE_TIME_LOW_RES_MODE = 0x23
  };

So... try replacing your library call...

#include <hp_BH1750.h>

With this library call...

#include <BH1750.h>

And compile again...

Many thanks for that Paul. I found another BH1750 library. I altered the name in the code. It now compiles.

You were absolutely right about different libraries not being compatible.

Best

Phil

Thank you everyone. It was the BH1750 library that was the problem. All is good now, I have been able to compile the sketch.

I am waiting for components to arrive now. I'll post again when I have the project working.

Thanks again all.

but why did the code you copied use the wrong library?

... In four places...

You also need to add this to void setup() to get meaningful output in the Serial Monitor.

  Serial.begin(115200);

Jim, the code that I found did not specify a particular library repository. I simply searched for a BH1750 and tried using 3 different ones before finding the one that worked.

Oh, Ok I will do that. Thank you.

Ok everyone. I shall have to leave off until I have all the components I hope to be able to get it all together in a few days time. Thank you all.

I remember all that darkroom stuff from about 45 years ago. That is taking over the bathroom and setting up an enlarger with trays of chemical washes, all illuminated in red. I've still got loads of B/W the pictures somewhere from that era. In fact in my scrap box I still have my old enlarger timer . . .

Incidentally, I've just put in a battery and it seems to work fine. If there was Arduino back in those days, I'd of course have made something instead of buying.

Hello all,

Jim_P, you suggested that I put up a link to where I found the sketch code. This I got from doing a Google AI search. This did not give any info on where it sourced the code.

However, I found code for a much more sophisticated lightmeter. Again by doing an AI search, and again no clue as to where the code was sourced. This new code has compiled using the same BH1750.h library. Previously it was the hp_BH1750.h library that was incompatible.

I am now waiting for the BH1750 sensor to arrive. Once I have that, I can see if the lightmeter works. Fingers crossed.

This is the new code:-

#include <Wire.h>

#include <Adafruit_GFX.h>

#include <Adafruit_SSD1306.h>

#include <BH1750.h>



#define SCREEN_WIDTH 128

#define SCREEN_HEIGHT 64

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

BH1750 lightMeter;



// Pin Configurations

const int shadowBtn = 2; // Captures brightest easel area (deep print shadow)

const int highlightBtn = 3; // Captures dimmest easel area (bright print highlight)

const int buzzerPin = 4; // Audio feedback buzzer

const int paperBtn = 5;  // Cycle through paper profiles



// Master Calibration Constants (Set using your Calibration Sheet)

float masterBaseLux = 10.0;

float masterBaseTime = 10.0;



float shadowLux = 0.0;

float highlightLux = 0.0;



// Paper Profile Lookup Table Structure

struct PaperProfile {

String name;

float speedFactor; // Multiplier relative to your calibration baseline (1.0)

};



const int NUM_PROFILES = 6;

PaperProfile paperDb\[NUM_PROFILES\] = {

{"Ilford MG V RC", 1.00},  // Your calibration baseline paper

{"Ilford MG FB clas", 1.00},  // establish factor

{"Ilford MG Art 300", 1.00},  // establish factor

{"Ilford Warmtone", 1.35}, // Slower paper, requires \~35% more time

{"Fomatone MG", 2.10},  // Much slower chlorobromide paper

{"Kentmere VC Select", 0.85} // Faster paper, requires less time

};

int currentPaperIndex = 0;



void playTone(int frequency, int duration) {

tone(buzzerPin, frequency, duration);

delay(duration);

noTone(buzzerPin);

}



void setup() {

pinMode(shadowBtn, INPUT);

pinMode(highlightBtn, INPUT);

pinMode(paperBtn, INPUT);

pinMode(buzzerPin, OUTPUT);



Wire.begin();

Serial.begin(115200);  //initialize display. May not be needed

display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

lightMeter.begin();



// Startup melody

playTone(440, 150);

playTone(880, 200);



display.clearDisplay();

display.setTextSize(1);

display.setTextColor(WHITE);

display.setCursor(0,10);

display.println(" DARKROOM MASTER ");

display.println(" v2.0 AUDIO & LUT ");

display.display();

delay(1500);

}



void loop() {

float currentLux = lightMeter.readLightLevel();

if (currentLux < 0.01) currentLux = 0.01;



// 1. Read Shadow Button (Bright light)

if (digitalRead(shadowBtn) == HIGH) {

 shadowLux = currentLux;

 playTone(600, 100); // Sharp registration tone

 delay(200);

}



// 2. Read Highlight Button (Dimmest light)

if (digitalRead(highlightBtn) == HIGH) {

 highlightLux = currentLux;

 masterBaseLux = currentLux;

 playTone(500, 100); // Secondary registration tone

 delay(200);

 }



// 3. Read Paper Selection Button

if (digitalRead(paperBtn) == HIGH) {

 currentPaperIndex = (currentPaperIndex + 1) % NUM_PROFILES;

 playTone(1000, 50); // High click wheel sound

 delay(250); // Prevent multi-triggering

 }



// 4. Compute Stop Range & Contrast Grade

float stopRange = 0.0;

String paperGrade = "N/A";

if (shadowLux > 0 && highlightLux > 0) {

 stopRange = log(shadowLux / highlightLux) / log(2.0);

 if (stopRange >= 1.40) paperGrade = "Grade 0";

 else if (stopRange >= 1.15 && stopRange < 1.40) paperGrade = "Grade 1";

 else if (stopRange >= 1.15 && stopRange < 1.40) paperGrade = "Grade 1.5";

 else if (stopRange >= 1.15 && stopRange < 1.40) paperGrade = "Grade 2";

 else if (stopRange >= 1.15 && stopRange < 1.40) paperGrade = "Grade 2.5";

 else if (stopRange >= 1.15 && stopRange < 1.40) paperGrade = "Grade 3";

 else if (stopRange >= 1.15 && stopRange < 1.00) paperGrade = "Grade 3.5";

 else if (stopRange >= 0.60 && stopRange < 0.75) paperGrade = "Grade 4";

 else if (stopRange >= 0.50 && stopRange < 0.60) paperGrade = "Grade 4.5";

 else if (stopRange < 0.50) paperGrade = "Grade 5";

 }



 // 5. Compute Dynamic F-Stop Timing with Speed Factor scaling

 float stopDifference = log(masterBaseLux / currentLux) / log(2.0);

 float calculatedTime = masterBaseTime \* pow(2.0, stopDifference) \* paperDb\[currentPaperIndex\].speedFactor;



 // 6. Render Dynamic Interface

 display.clearDisplay();

 display.setTextSize(1);

 display.setCursor(0, 0);



 // Show currently active paper emulsion path

 display.print("PAPER: "); display.println(paperDb\[currentPaperIndex\].name);

 display.drawLine(0, 10, 128, 10, WHITE);



 display.setCursor(0, 16);

 display.print("SHD: "); display.print(shadowLux, 1);

 display.print(" | HIG: "); display.println(highlightLux, 1);

 display.print("Range: "); display.print(stopRange, 2); display.println(" stops");

 display.print("Paper Filter: "); display.println(paperGrade);



 display.drawLine(0, 44, 128, 44, WHITE);




// Big text calculated time readout

 display.setCursor(0, 49);

 display.setTextSize(2);

 display.print("TIME: "); display.print(calculatedTime, 1);        

 display.print("s");



 display.display();

 delay(150);

}

:crossed_fingers:

The AI made some coding errors... "escape" characters were added in many location where brackets or asterisks were in the code. Here are some lines... you can see the added "scoop" / "escape" character.

Here is the "corrected" code (removing the "escape"):

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <BH1750.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
BH1750 lightMeter;

// Pin Configurations
const int shadowBtn = 2; // Captures brightest easel area (deep print shadow)
const int highlightBtn = 3; // Captures dimmest easel area (bright print highlight)
const int buzzerPin = 4; // Audio feedback buzzer
const int paperBtn = 5;  // Cycle through paper profiles

// Master Calibration Constants (Set using your Calibration Sheet)
float masterBaseLux = 10.0;
float masterBaseTime = 10.0;
float shadowLux = 0.0;
float highlightLux = 0.0;

// Paper Profile Lookup Table Structure
struct PaperProfile {
  String name;
  float speedFactor; // Multiplier relative to your calibration baseline (1.0)
};
const int NUM_PROFILES = 6;

PaperProfile paperDb[NUM_PROFILES] = {
  {"Ilford MG V RC", 1.00},  // Your calibration baseline paper
  {"Ilford MG FB clas", 1.00},  // establish factor
  {"Ilford MG Art 300", 1.00},  // establish factor
  {"Ilford Warmtone", 1.35}, // Slower paper, requires \~35% more time
  {"Fomatone MG", 2.10},  // Much slower chlorobromide paper
  {"Kentmere VC Select", 0.85} // Faster paper, requires less time
};

int currentPaperIndex = 0;

void playTone(int frequency, int duration) {
  tone(buzzerPin, frequency, duration);
  delay(duration);
  noTone(buzzerPin);
}

void setup() {
  pinMode(shadowBtn, INPUT);
  pinMode(highlightBtn, INPUT);
  pinMode(paperBtn, INPUT);
  pinMode(buzzerPin, OUTPUT);
  Wire.begin();
  Serial.begin(115200);  //initialize display. May not be needed
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  lightMeter.begin();
  // Startup melody
  playTone(440, 150);
  playTone(880, 200);
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 10);
  display.println(" DARKROOM MASTER ");
  display.println(" v2.0 AUDIO & LUT ");
  display.display();
  delay(1500);
}

void loop() {
  float currentLux = lightMeter.readLightLevel();
  if (currentLux < 0.01) currentLux = 0.01;

  // 1. Read Shadow Button (Bright light)
  if (digitalRead(shadowBtn) == HIGH) {
    shadowLux = currentLux;
    playTone(600, 100); // Sharp registration tone
    delay(200);
  }

  // 2. Read Highlight Button (Dimmest light)
  if (digitalRead(highlightBtn) == HIGH) {
    highlightLux = currentLux;
    masterBaseLux = currentLux;
    playTone(500, 100); // Secondary registration tone
    delay(200);
  }

  // 3. Read Paper Selection Button
  if (digitalRead(paperBtn) == HIGH) {
    currentPaperIndex = (currentPaperIndex + 1) % NUM_PROFILES;
    playTone(1000, 50); // High click wheel sound
    delay(250); // Prevent multi-triggering
  }

  // 4. Compute Stop Range & Contrast Grade
  float stopRange = 0.0;
  String paperGrade = "N/A";
  if (shadowLux > 0 && highlightLux > 0) {
    stopRange = log(shadowLux / highlightLux) / log(2.0);
    if (stopRange >= 1.40) paperGrade = "Grade 0";
    else if (stopRange >= 1.15 && stopRange < 1.40) paperGrade = "Grade 1";
    else if (stopRange >= 1.15 && stopRange < 1.40) paperGrade = "Grade 1.5";
    else if (stopRange >= 1.15 && stopRange < 1.40) paperGrade = "Grade 2";
    else if (stopRange >= 1.15 && stopRange < 1.40) paperGrade = "Grade 2.5";
    else if (stopRange >= 1.15 && stopRange < 1.40) paperGrade = "Grade 3";
    else if (stopRange >= 1.15 && stopRange < 1.00) paperGrade = "Grade 3.5";
    else if (stopRange >= 0.60 && stopRange < 0.75) paperGrade = "Grade 4";
    else if (stopRange >= 0.50 && stopRange < 0.60) paperGrade = "Grade 4.5";
    else if (stopRange < 0.50) paperGrade = "Grade 5";
  }

  // 5. Compute Dynamic F-Stop Timing with Speed Factor scaling
  float stopDifference = log(masterBaseLux / currentLux) / log(2.0);
  float calculatedTime = masterBaseTime * pow(2.0, stopDifference) * paperDb[currentPaperIndex].speedFactor;

  // 6. Render Dynamic Interface
  display.clearDisplay();
  display.setTextSize(1);
  display.setCursor(0, 0);

  // Show currently active paper emulsion path
  display.print("PAPER: "); display.println(paperDb[currentPaperIndex].name);
  display.drawLine(0, 10, 128, 10, WHITE);
  display.setCursor(0, 16);
  display.print("SHD: "); display.print(shadowLux, 1);
  display.print(" | HIG: "); display.println(highlightLux, 1);
  display.print("Range: "); display.print(stopRange, 2); display.println(" stops");
  display.print("Paper Filter: "); display.println(paperGrade);
  display.drawLine(0, 44, 128, 44, WHITE);

  // Big text calculated time readout
  display.setCursor(0, 49);
  display.setTextSize(2);
  display.print("TIME: "); display.print(calculatedTime, 1);
  display.print("s");
  display.display();
  delay(150);
}

I think there is a memory leak if you use an Uno or a Nano, maybe due to using String method.

And... the sketch is cluttering the OLED... that will need a little housekeeping.

*Note: the garbled, upper-left, corner of the screen is the same in all setRotation() for the OLED. Looking more like a memory issue.

HI, @philromford1942

Asking for the complete code does nothing for your learning of Arduino code.

Try first to JUST code for the display, get Hello World to be displayed.
Google;
Arduino Tutorial
The display has a library, with the library comes example code, try them first.

You will learn more than just asking for ready made code.
If you develope your code yourself, you will be able to debug it yourself, more and more as you progress.

It may take longer but it will be more satisfying in the end.

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

Why do you think it's AI?

I think that there is a good explanation why that happened and it's not related to AI (in the broader sense).