Pendulum and Photoresistor

Hi, can someone please help me?
In my project I have a light attached to the bottom of a pendulum swinging over the top of a photoresistor. I'm using the Arduino R3 basic starter kit.
I need to measure the time period between the the points of low resistance. I'm not sure on how to make a code for it to do this. Or if its even possible as i would normally use different lab equipment to do this. However due to covid we still don't have access to our lab so we are trying to use Arduino as a replacement for that. I have got an Ohm meter circuit and code, so can i maybe add to this to measure the time period between low resistance somehow?

here is the ohm meter code i have:

int analogPin = 0;
int raw = 0;
int Vin = 5;
float Vout = 0;
float R1 = 1000;
float R2 = 0;
float buffer = 0;

void setup(){
Serial.begin(9600);
}

void loop(){
  raw = analogRead(analogPin);
  if(raw){
    buffer = raw * Vin;
    Vout = (buffer)/1024.0;
    buffer = (Vin/Vout) - 1;
    R2= R1 * buffer;
    Serial.print("Vout: ");
    Serial.println(Vout);
    Serial.print("R2: ");
    Serial.println(R2);
    delay(1000);
  }
}

Many thanks in advance as I've been really struggling on this for 2 weeks now. I've found ones similar using a light gate but I've been told I have to do it with the light source as the pendulum.
Thank you

so when the pendulum is at one spot, start a timer, when at the other spot stop a timer and there is a triggering device. Cool and easy peasy. Now you need a counter of time.

By any chance in 2 weeks of research have you found mention of a thing called millis()? And don't let your boss know that the code you have wrote, over the last few weeks, could be obtained in a few minutes on the internet.

Now you got a clue. millis()!!!!!!!!


Oh, your code shows no attempt at obtaining an accumulated count of something.

Are you trying to determine the time duration of the pendulum swing? Nothing in your code about timing.

Threshold the readings, record the value of micros() on every transition, do relevant calculations.

You can count N cycles to increase the accuracy.

Note that some Arduinos (including the Uno alas) have low quality clocks, with +/- 0.6%
specification.

A photoresistor may respond too slow, phototransistor might be a better choice.

Do not be concerned with the voltage, all you need is the point of minimum value from the analogRead() (or maximum, depending on how the photoresistor is wired). Avoid any use of delays, you do not want to increase the time between analogReads.

I would approach this problem (most problems, in fact) incrementally. The first step, given that you have a mechanical setup with a sensor, is to collect a time series of LDR readings and see what you're dealing with from this setup.

e.g. (untested) Code snippet with global variables and time synchronized ADC collection:

unsigned int timeStep = 10000 ;   // Collect sample every 10,000 microseconds (100 Hz)
unsigned long int nextSampleTime ;  // Time of next ADC sample
int adcSample ;   

nextSampleTime = micros() + timeStep ;   // Time of first sample

void loop() {
   if (micros() >= nextSampleTime) {
      adcSample = analogRead(analogPin) ;
      Serial.println(adcSample) ;
      nextSampleTime = nextSampleTime + timeStep ; // Next sample is current time plus step time
   }
}

Run this with the serial output directed to the IDE serial plotter. With this data in hand, the next step is to come up with an algorithm to find the period of the signal given whatever quirks it might exhibit. There are various approaches to this. A straightforward one might be to subtract an average value from each adcSample making it a zero-centered negative and positive number series, detect when successive samples go from a negative to a positive value, and calculate the time between such events.

So the plan of action is

  1. Collect some data to see what the setup produces.
  2. Come up with an algorithm that detects some identifiable feature in that time series once per period.
  3. Use the time between those detections to calculate the period.

You might have better luck with your code if you first lay out the logic, something like:

Setup:
set up serial communications, needed pin functions, etc

Loop:
wait for a flash from the passing pendulum
if flash is detected then:
set timepoint_1
wait for the flash to disappear
is another flash detected?
set timepoint_2
period = timepoint_2 - timepoint_1
display period on serial monitor

Outlining a program in this manner breaks it down into more manageable chunks. Now write actual code to correspond with each chunk. Some of these chunks involve only a single line of code and will be easy. Others will take some thought and experimentation.

There is a lot to be said for starting out as simple as possible. Look at the example code for the simple blinker, which can be found in the Arduino IDE. Make sure you understand every single line of the code, why it is there, and what it is doing. Move on to your project -- see if you can simply detect the flash from the passing pendulum, and write something to the serial monitor when that happens. Once you have this working, you can add the timing loop. Don't try to write an entire complex program right off the bat.
[P.S. -- MrMark posted some actual code as I was writing this...]
S.

The OP may want to determine the best device for the project. I'd not use a photoresistor, LDR. The LDR's inherent hysteresis would make for a poor choice, over a photodiode, for such measurements.

Also, timing granularity may be of concern. words used by the OP such as "lab', indicate a high precision may be desired. I am going to guess that millis() and, perhaps, even micros() may not be good enough.

The ESP32's PCNT module can give pulse precision down to 12.5ns. Each core of the ESP32 can be assigned task. Such as, for example, all the tasks for measurement can be assigned to one core and the storage of the data can be assigned to the other core.

I'm going to guess that "lab" in this context means a school physics lab. ICBW, of course.

There are lots and lots of enhancements and improvements that could be made. Another might be a narrow slit between the pendulum and the detector to sharpen the response threshold. But it's obvious that the OP hasn't much programming experience. I think at this point (s)he needs to learn to walk before running -- get the general outline working and then work on improving it...
S.

You could check what the signal looks like (open Serial Plotter) ...

int analogPin = 0;
int raw;

void setup() {
  Serial.begin(9600);
}

void loop() {
  raw = analogRead(analogPin);
  Serial.print("raw: ");
  Serial.println(raw);
  delay(5);
}

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