Arduino Timer With 2 IR Sensors

2,174

4

10

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

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

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!

Be the First to Share

    Recommendations

    • Tiny Speed Challenge

      Tiny Speed Challenge
    • Clocks Contest

      Clocks Contest
    • PCB Design Challenge

      PCB Design Challenge

    10 Discussions

    0
    immingcj
    immingcj

    2 months ago

    My apologies for a couple of questions since I am new to Arduino. What is being timed? I assume it is anything moving under the wires held up by the cups between the two sets of emitter and receptors. Separated by only a few inches on your pieces of paper for illustration purposes? But could be several feet or any distance with enough wire. Is that correct? Is the time displayed on the serial post output?

    0
    naseem7
    naseem7

    11 months ago

    Ir timer code full stop
    No display

    0
    Braden W
    Braden W

    Reply 11 months ago

    ?

    0
    naseem7
    naseem7

    11 months ago on Step 2

    Ir timer code full stop
    On display

    0
    naseem7
    naseem7

    11 months ago on Step 4

    IR timer code 8 channel

    0
    naseem7
    naseem7

    11 months ago

    Ir timer code 8 channel rilay

    Great first Instructable. Thanks for sharing it with the community.

    0
    Braden W
    Braden W

    Reply 1 year ago

    Thank you! Did you try making it?

    0
    Drborst1
    Drborst1

    1 year ago

    I like it, But how fast can I break the beams and trigger (is it 1 ms resolution?)

    0
    Braden W
    Braden W

    Reply 1 year ago

    The resolution of the timer is to 1/100th of a second.