Introduction: Arduino Timer With 2 IR Sensors

About: I enjoy programming, swimming, and playing Dungeons and Dragons.

This project is written for beginners who have little experience in one or more of the following:

  • Arduinos
  • IR break beam sensors
  • Serial Monitors

Step 1: Get Your Hardware

The hardware

2 sets of IR Break Beams

14 Wires (Only 10 are pictured, but 14 will be used.)

An Arduino Uno

A Breadboard

2 Plastic Containers

Step 2: Download the Code

In the Arduino IDE app, paste in this code:

#define LEDPIN 13<br>#define ENDPIN 4
#define STARTPIN 7int endState = 0;
int startState = 0;
int timePassed = 0;
int timerStarted = false;
void setup() {
  // initialize the LED pin as an output:
  pinMode(LEDPIN, OUTPUT);
  
  // initialize the end pin as an input:
  pinMode(ENDPIN, INPUT);     
  digitalWrite(ENDPIN, HIGH); // turn on the pullup
  // initialize the start pin as an input:
  pinMode(STARTPIN, INPUT);     
  digitalWrite(STARTPIN, HIGH); // turn on the pullup
  
  Serial.begin(9600);
}
 
void loop(){
  // read the state of the pushbutton value:
  endState = digitalRead(ENDPIN);
  startState = digitalRead(STARTPIN);
  
  // check if the start beam is broken
  // if it is, startState is LOW:
  if (startState == LOW) {     
    // turn LED on:
    digitalWrite(LEDPIN, HIGH);  
  } 
  else {
    // turn LED off:
    digitalWrite(LEDPIN, LOW); 
  }
  
  if (!endState && timerStarted) {
    Serial.println("FINISH!");
    float actualSeconds = timePassed;
    Serial.println(actualSeconds/1000);
    Serial.println("\n");
    timerStarted = false;
  }
  
  if (!startState && !timerStarted) {
    Serial.println("START!");
    timePassed = 0;
    timerStarted = true;
  }
  
  if (timerStarted) {
    timePassed += 1;
    delay(1);
  }
}

Save this code in a file then set it aside for later.

Step 3: Attach the Breadboard to the Arduino

Attach the positive and negative terminal of both sides of the bread board to the arduino.

Left Side: Positive to 5V, Negative to ground

Right Side: Positive to 3.5V, Negative to ground

Step 4: Attach the Wires

Attach each of the ends of the wires coming from the IR break beams to a wire. I've found folding them together and taping works best.

Step 5: Attach the Sensors to the Breadboard

  1. Attach the red wire of the first transmitter to the positive terminal and the black wire to the negative terminal on the left side of the board
  2. Attach the red wire of the second transmitter to the positive terminal and the black wire to the negative terminal on the right side of the board.
  3. Do the same on the left and right for the sensors, ignoring the yellow wires for now

Step 6: Attach the Yellow Sensor Wires to the Arduino

Attach the first sensor wire to pin #7 on the arduino.

Attach the second sensor wire to pin #4 on the arduino.

Step 7: Run Some Test Code to Make Sure You Have Attached Your Wires Correctly

Point both of the sets of break beams at each other and run this code:

#define LEDPIN 13
#define ENDPIN 4 #define STARTPIN 7 int endState = 0; int startState = 0; int timePassed = 0; void setup() { // initialize the LED pin as an output: pinMode(LEDPIN, OUTPUT); // initialize the end pin as an input: pinMode(ENDPIN, INPUT); digitalWrite(ENDPIN, HIGH); // turn on the pullup // initialize the start pin as an input: pinMode(STARTPIN, INPUT); digitalWrite(STARTPIN, HIGH); // turn on the pullup Serial.begin(9600); } void loop(){ // read the state of the pushbutton value: endState = digitalRead(ENDPIN); startState = digitalRead(STARTPIN); // check if the start beam is broken // if it is, startState is LOW: if (startState == LOW) { // turn LED on: digitalWrite(LEDPIN, HIGH); } else { // turn LED off: digitalWrite(LEDPIN, LOW); } if (endState) { Serial.print(" Unbroken end"); } if (!endState) { Serial.print(" Broken end"); } if (startState) { Serial.println(" Unbroken start "); } if (!startState) { Serial.println(" Broken start "); } }

While the code is running, open the serial monitor (shown above) and point each pair of IR break beams at each other, and it should say whether the beam is broken or not.

Step 8: Finish Your Project

Tape your pairs of IR break beams to opposite sides of sheets of paper, pointing at each other.

Use the plastic containers to hold the wires out of the path.

Run the code from step 2, open the serial monitor, then voila! You have a functioning IR break beam timer!