Pages

Thursday, July 24, 2014

Photogate Tutorial Part 1: The Right Parts for the Job

Now that I'm starting to get the hang of Arduino, I've decided to write a tutorial explaining how to build (and program) the specific type of motion detector we are using for our feeder. This first installment will explain the basics of the parts we used and how they work. The second part will teach you how to build a simple photogate circuit, and Part 3 will teach you how to make the more complicated system we are using for this project. No previous experience with electronics is necessary to understand these tutorials. I've included prices for the components, but these are just to give you an idea of how much this project would cost to make, and the online stores I've linked to don't necessarily have the lowest price. That said, at the time of this writing it's possible to buy all the things you need for the simple photogate circuit for around $45 plus shipping.

The brain of our circuit is the Arduino Uno. Arduino is a brand of microcontroller board designed to help you quickly build circuits and write programs that interact with the physical world. You can think of an Arduino as a tiny, and limited, computer. You can write programs on your laptop or desktop, then upload them to the Arduino board through a USB cable. 

Modified from a photo found at http://arduino.cc/en/Main/arduinoBoardUno

An Arduino Uno costs about $30. A guide to setting up your Arduino and downloading the software can be found at Adafruit, and some simple projects for getting familiar with the Arduino can be found here.

To detect when bees land at the feeder, we are using a sensor called a photointerrupter, from Sparkfun. A photointerrupter, or photogate, is a U-shaped sensor that shoots a beam of light from one side (the infrared emitter) to the other (the infrared detector).


Image Source: https://www.sparkfun.com/products/9299

 When the infrared detector receives the beam, the photogate outputs "high" voltage (about 5V). If something comes between the emitter and the detector, blocking the beam, the photogate outputs "low" voltage (0V). The Arduino can be programmed to read the photogate's output voltage and record when the beam is blocked. This is how the system will determine when a bee has landed at the feeder. Sparkfun's photointerrupters cost about $2 each.

When you are tinkering with a circuit, you want to be able to move components around, and this is where breadboards come in. Breadboards are platforms used for building and testing circuits quickly. The front of a breadboard is a grid of small rectangular holes, separated in the middle by a long indentation. Any wire or component with wire leads can easily be plugged into any of the holes. Orient your breadboard so that the indentation is vertical. Each horizontal row is connected on the inside. Larger breadboards usually also have vertically connected columns, called buses, on the sides: 

Image Source: https://learn.sparkfun.com/tutorials/how-to-use-a-breadboard/anatomy-of-a-breadboard

Large circuits often have many components that need to connect to power and ground, so long power and ground rails are useful. Right now I'm using a smaller breadboard that doesn't have vertical power and ground rails, so I've designated the top left row as power and the top right row as ground. Whenever someone refers to "connecting" two components, that means that the components need to be in the same horizontal row, or have a wire with an end plugged into each of their rows. Adafruit sells a half-size breadboard for $5.

Light emitting diodes (LEDs) are tiny light bulbs that are easy to plug into breadboards. In Part 2 of this tutorial we will use LEDs as indicators to help show when our circuit and code are working. We will have the LED turn on when the photogate's beam is unbroken and turn of when the photogate's beam is blocked. An LED consists of a bulb with two wires, or leads, attached. One of the wires is a positive lead and the other is the negative lead. The leads belong in different parts of the circuit, so it is important to be able to tell them apart. The base of the LED bulb is not a perfect circle- one edge is flat. The lead closest to the flattened edge is the negative lead, and should be connected to ground. The other lead is positive, and should connect to a resistor to protect it from burning out. The resistor should then be connected to a digital or analog pin on the Arduino.

Modified from a diagram found at

An LED costs $0.75 from Adafruit, or $8 for a pack of 25.

resistor is technically anything that slows down the flow of current through a system. However, the term is typically used to refer to electrical components that are added to a circuit specifically to lower the current flow to levels that are safe for the other components. The "strength", or resistance, of a resistor is measured in ohms (Ω)- the more ohms a resistor is, the more it will slow down current. Resistors are color-coded to show how much resistance they have.
 
The circuits we are building in these tutorials call for 220Ω resistors, but if you plan to spend more time experimenting with Arduino, you may want to buy a variety pack- we bought one for the lab from E-Projects for $10. I have found that the LED and the photogate will work with resistors anywhere between 220Ω and 1000Ω, though the LED is brightest with a 220Ω resistor.

The final thing you will need to build this circuit is wire. The thickness of a wire is called its gauge (rhymes with cage), and 22 or 24 gauge wire is best for breadboards. Adafruit also sells a "Wire Bundle" of pre-cut and pre-stripped wires, which are convenient for fast prototyping, for about $6. However, when making the final circuit for a project, it is better to use wire that you can cut to the right length and solder. Wire for electronics comes coated in insulating plastic, and when you are not using wires from the "Wire Bundle", you will need to strip the plastic off the end of the wire with a wire stripper. Once the ends are stripped, the wire can be plugged into the breadboard just like any other component.

I'll end Part 1 of this tutorial with a couple tips for building circuits. To avoid confusing yourself, I recommend color-coding the wires. I'm using red for power, green for ground (black is traditional, but the wire bundle only has two black wires left), and blue for signal wires. My second tip is to make wires as short as possible. It's fine to use longer wires when you are first testing a circuit on a breadboard, but once you have figured out where the connections will go, replace these long wires with shorter ones. This makes the circuit neater and easier to troubleshoot. Components such as resistors and LEDs should also eventually have their leads trimmed to keep them close to the board.
Next week, I'll post Part 2 of the tutorial, and teach you how to build a basic photogate circuit.

4 comments:

  1. Thank you so much. I'm a newbie in this field. So your post is really really helpful. Thanks again :D

    ReplyDelete
  2. Hey Matina, is it okay if i use the image of the arduino in my bachelor thesis? I will obviously make sure to credit you.

    Thank you

    ReplyDelete
    Replies
    1. Sorry I didn't see this for a little while! You can use the image. Credit is actually due to the student who made it, Sarah Nichols.

      Delete