Measuring the frequency of a light bulb

As Nyquist and Shannon told us the scanning device must be at least twice as fast as the signal we are going to detect. In the US the frequency of the power line is 60 cycles per second. For a light bulb the direction of the current does not matter, so it will reach its maximum value 120 times per second. In the rest of the world the line goes with 50 cycles per second and the frequency of the intensity of the emitted light will be only 100 cycles per second.

As most of the cheap LDR (light dependent resistors) will be too slow you have to use a photodiode. We were using a spare old BP 101. You will get a newer one. It comes with three wires: the emitter (marked), the base and the collector. We connect the emitter to "A0" and the collector to "A1", leaving the base unconnected.

As we want to read the values as fast as possible we don't have time to print them immediately after reading. Instead we have to store them in an array. Don't make the array too big otherwise you might run into problems (see this for strange behaviour when working with large arrays.

When we read the time before (t1) and taking after (t2) the measurements by using the micros() function we still can plot our readings.

// Photodiode  S472P
// To make it more easy we use Pin-A0 as GND.

const byte cathode = A0;
const byte anode = A1;

int value[3500]; // for such a large array you need the ATmega2560!!
long t1, t2;

void setup() {
  pinMode(cathode, OUTPUT);
  digitalWrite(cathode, LOW);
  pinMode(anode, INPUT);
  digitalWrite(anode, HIGH); // activate the internal pullup-resistance
  Serial.println("ready (press the return key?");
  while (!Serial.available());
  t1 = micros();
  for (int i = 0; i < sizeof(value)/2; i++) value[i] = analogRead(anode);
  t2 = micros();
  for (int i = 0; i < sizeof(value)/2; i++) Serial.println(value[i]);
  Serial.print("time from: ");
  Serial.print("until: ");

void loop() {}

When you copy the contents of the Serial terminal window into your spreadsheet software and add a time column according to the t1 and t2 values that were printed at the end eventually get a diagram like this:

You can clearly count the maxima that occured during the time of 0.1 seconds. That means you got exactly 100 Hertz!

Also, seeing the results for t1 and t2 and the number of samples, you find out you get some 8,900 samples per second. So in case you have in mind to sample audio signals the cut-off frequency will be about 4,400 Hz which will do pretty well for telephone quality applications.

contact: nji(at)