We recently had customer inquiring about connecting one of our Photo Interrupt Boards directly to a computer.
We though about this for a few minutes and decided the best way to do this would be to use a FTDI to Serial breakout board. The FTDI presents itself as a serial port when connected to a computer.
Using an FTDI would be the perfect solution as the FTDI can supply power to the board and it can monitor at least 1 signal line – CTS (Clear To Send). CTS is a signal line used by a device connected to the FTDI, the device uses the line to signal the computer that it can send data to it (this is known as hardware flow control). For our purposes we use it to signal an interrupt.
The FTDI Breakout comes in 5V and 3.3V – either of which would be suitable to the task.
We wired up the system, using this connection wire, as follows: GND of the FTDI to GND of the photo interrupt , +3.3V/+5V of the FTDI to +5V of the photo interrupt and finally we connected the signal line of the photo interrupt to the CTS pin of the FTDI.
In order to monitor this from a computer you connect the FTDI to a USB cable in this case a mini USB cable you might need to install a driver for the FTDI device which can be obtained at ftdichip.com. When the device is installed it will show up as a serial port (COMxx). For the purpose of keeping this tutorial short we will not go into determining which COM port you need to use.
Now for the code, We threw together this quick sample in C# as it requires the least number of lines of code to achieve our purpose.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
using System; using System.IO.Ports; using System.Threading; namespace ftdi_photointerrupt { class Program { static int count=0; static void Main(string[] args) { // initialize serial port - ours is on COM22 SerialPort serialPort=new SerialPort(){PortName="COM22"}; // set up a delegate for the PinChanged Interrupt serialPort.PinChanged += serialPort_PinChanged; // open Serial port serialPort.Open(); // run in a loop waiting quit program when keyboard pressed while (!Console.KeyAvailable) Console.Write("\r{0} ", count);; } // this is the CTS interrupt static void serialPort_PinChanged(object sender, SerialPinChangedEventArgs e) { // please note that this will be called every time the CTS changes // i.e. on transition from low to high and high to low. // if you are only interested in a specific transition query the // port for its current CTS state. if (e.EventType == SerialPinChange.CtsChanged) count++; } } } |
As you can see the code is pretty self-explanatory – our FTDI in on COM port 22 so we initialize the serial port for ‘COM22’. We don’t really care about the port speed as we are not going to be transferring any data, we just want to know the state of CTS.
We then set up a function to be called whenever CTS changes state i.e. when the interrupt controller gets interrupted.
And finally we open the com port and wait, we do however print out the count continuously.
Whenever the beam of the interrupt controller is broken the ‘serialPort_PinChanged’ function will be called. All we do is increment the count variable which of course will be reflected in the console window.
Now that you can connect your photo interrupt to your computer you can count almost anything that interrupts the beam i.e. water drops, etc.
If you have any ideas for future tutorials please send your suggestions to support@karlssonrobotics.com.