Today I decided to write about one interesting project that I was engaged in during my free time.
The essence of the device is simple - there is a matrix of photodiodes (in this case, 4, but it can be more) that registers the light from some source that can move. Naturally, the amount of light falling on each photodiode separately is different.
The device must determine the location in space of the light source that is moving. This is the main goal. That is, it is necessary to programmatically solve the problem of multidimensional correlation between the intensity vector and the vector of the location of the light source.
')

The general scheme is shown in the figure above. In our case, only 4 photodiodes. The signal from the photodiodes is amplified and fed to the ATMega16 microcontroller. The microcontroller forms a data packet and sends it at a frequency of 1 Hz via USART (COM port). On the computer side, a program written in Lazarus (FreePascal) works, which reads data from the port and analyzes it using the free neural network library, then outputs the result about the location of the light source.
It was a brief description, and now the details.
1) Connecting photodiodes
Here is a simple diagram of the connection of the photodiode to the operational amplifier, the converter circuit of the small current into voltage can be found in any book on circuitry.

Incident light causes a photocurrent, the circuit is linear (before saturation), in contrast to the scheme with offset. The current almost does not flow into the inverting input, and therefore the output voltage is defined as U = I * R1.
A very good article about photodiodes and amplifiers was written by an employee of Texas Instruments Philip CD HOBBS “Amplifiers for Photodiodes on Operational Amplifiers”. I recommend to all interested.
I used high speed PIN photodiodes, BPW34. They do not have a very large viewing angle - I used what was at hand. Almost any photodiodes, a matter of taste, will work here.
The red line requires an AD820 op amp. A field-effect transistor (FET) amplifier has an advantage over a bipolar low leakage current, so this is very important in current-voltage converter circuits. The amplifier also has a Rail-to-Rail output, i.e., the output voltage sweep can approach very closely in the power rails.
I recommend to use after the output of the low-pass filter amplifier (low pass filter), and select the desired cut-off frequency so that there is less noise.
2) ATMEGA16 microcontroller
As I wrote above, the microcontroller is needed in order to digitize signals and transfer them to the PC port.
It uses the oldest MAX232ACPE converter for the COM port. Now I use controllers with USB hardware, but a year ago, the circuit with MAX232 seemed to me very cool, and I was very happy when I finally figured out with it.
Those who do not have a board with COM ports will have to either assemble themselves on the FT232RL or buy a USB-USART converter, which is now in bulk on the Internet.

The first thing you need to organize a stable power supply for the microcontroller (MK). On the power supply, you should always put as close as possible to the legs of the MK a ceramic capacitor with a capacity of 0.1 uF. In the figure between VCC and GND.
Then you need to take care of the clock signal.
There is a quartz resonator at 8MHz (believe me, when I started, I also thought that it was so little). To increase the stability, picofarad capacitors are installed as shown in the diagram. For each frequency you need your own nominal value, you need to look at the datasheet, the official passport-documentation for each IC (integrated circuit).
In order for the MC to work without accidental drops, you must connect a Vcc pull-up resistor to the RESET via a pull-up resistor.
The PA0..7 analog inputs are the ports to which we send signals from amplifiers.
As the reference voltage for the ADC take Vcc, that's not at all tricky.
Ports RX, TX are used to send and receive data.
TTL logic and RS232 logic are very different, and can not work directly, so we use a converter, the connection diagram is shown on the left. All capacitors shown in the converter wiring diagram are ceramic and have a rating of 0.1 uF.
3) Development environment and used libraries
I did this project on the Lazarus IDE compiler FreePascal, in the process of writing I used several components and libraries.
- CportLib COM Port Library
- Famous FANN Library
To work with neural networks, I chose the free FANN library. I think that most people know how neural network algorithms work, but just in case, I will repeat it with my example.

Here the neural network must first learn with the teacher.
The meaning of learning is that the network must adjust the coefficients of the matrices of the layers in such a way as to minimize the difference between the output vector and the training vector.
Each task is unique in some sense, and therefore there is no theory that would tell how many neurons are enough to solve the problem, which transfer function should be used, and so on.
That's all,
the next time I find time, I will write a sequel in which such parts as:
- Data acquisition from MK
- Neural network training
- Data analysis using neural network
The next part will be completely software.