Attention: Safety Recall of Vernier Go Direct Charge Station. Click to learn more.

Pendulum Driver

The Pendulum Driver sketch uses a photogate, a Digital Control Unit (DCU), and an electromagnet to keep a pendulum with a metal bob swinging forever. Set up a bifilar (i.e., two string) pendulum so it swings in a plane. The pendulum bob needs to be made of iron or steel. Connect the DCU to the Digital 2 port and connect an electromagnet wired between the first output of the DCU and the DCU ground. Position the electromagnet right below the bob at the bottom of the swing. Place a photogate, connected to the Digital 1 port, slightly to one side of the electromagnet so that the pendulum bob blocks the photogate as it swings down and approaches the electromagnet.

Pull the pendulum bob up behind the photogate, start the sketch and release the bob. The sketch will turn on the current to the electromagnet as soon as the bob blocks the photogate. This will cause the bob to be pulled downward and increase its speed slightly. The sketch is written so that the next time the bob blocks the photogate, the DCU does nothing. (If it did, it would be slowing down the pendulum bob.) Basically, every time the pendulum bob is swinging down toward the electromagnet, the current is turned on, pulling the bob downward, and adding energy to the system.

VernierPendulumDriver (v 2017.07)
This sketch assumes that a Vernier Photogate is connected to BTD 1 and a 
Vernier Digital Control Unit (DCU) is connected to BTD2.

The sketch is designed to drive a pendulum with a magnetic bob to 
keep it swinging "forever". When the pendulum passes through the Photogate, it
triggers a short digital output from the DCU to an electromagnet. The 
electromagnet should be situated just below the low point of the pendulum bob's
swing. The photogate should be situated so that the bob goes through it as it 
moves downward. The electromagnet will go on to pull the bob downward to add
energy to the pendulumn system.

The sketch also flashes an LED connected to D13 when the photogate is blocked,
and notes the times at which the pendulum blocks the photogate.

Start the sketch and then start the pendulum swinging with the bob going
through the photogate before it reaches the bottom of the swing.

See for more information.

#include "VernierLib.h"
VernierLib Vernier;
unsigned long timems = 0; //Time in ms
unsigned long previousTime= 0;// remember the previous time
int photogatePin =2; //
int photogate = HIGH;// not blocked is HIGH
int previousPhotogate=HIGH;

int count=1; //used to determine which direction the pendulum is swinging
int LEDpin =13;/// line for LED to turn on when photogate is blocked
int DCUline = 6;//This line will turn on the first line of the DCU, if
// the DCU is connected to BTD2
void setup() 
    Serial.begin(9600);           // set up Serial library at 9600 bps
    pinMode (photogatePin,INPUT);
    pinMode(DCUline, OUTPUT);
    pinMode(LEDpin, OUTPUT);
    Serial.println("Vernier Format 2");
    Serial.println("Photogate periods taken using Ardunio");
    Serial.print("\t"); //tab character
    Serial.println ("Time"); //change to match sensor
    Serial.print("\t"); // tab character
    Serial.println ("milliseconds");
  };// end of setup
void loop ()
  photogate = digitalRead(photogatePin);//low when blocked
  if (photogate == LOW) //low when blocked
     digitalWrite(LEDpin, HIGH);
     if (previousPhotogate == HIGH)  // if the photogate has just gone to the blocked state
          count++; //increment count of times photogate has been blocked
          if (count % 2 == 0)// if the bob is moving in the right direction
              timems = millis() ;
              Vernier.DCU(DCUline); // turn on DCU line 1 and the electromagnet DCU line
              // and print out the swing number and period
              Serial.print("\t"); // tab character
              previousTime= timems; // remember the previous time
             }// end of the section for action take at the end of the complete swing
           } // end of photogate just gone blocked section
      }// end of if photogate blocked
      else //photogate not blocked:
          Vernier.DCU(0);// turn off DCU line 1 and the electromagnet
          digitalWrite(LEDpin, LOW);// turn off LED
       previousPhotogate = photogate;
     } ;// end of loop