I wanted to design a circuit to generate a nice digital pulse (for input to a microcontroller or the Raspberry Pi) every time an LDR is quickly obscured from ambient light – like for instance, when you pass your hand over it. I didn’t want to use the popular solution of reading the analog values, and processing it on the microcontroller (averaging, finding peaks, etc.), because this will take up valuable processing time, and I want to avoid lag as much as possible, by offloading this work to hardware.
So here are 3 attempts that I made, and the results.
Arduino is fantastic as an intermediary between your computer and a raw electronic circuit. Using the serial interface, you can retrieve information from sensors attached to your Arduino. (You can also send information via the serial interface to actuate circuits and devices (LEDs, relays, servos, etc.) connected to your Arduino.) Once you have the data in your computer, you can do all sorts of things with it – analyze it, display it, or share it on the internet, for instance.
In this post, I will be reading and displaying analog data from a pair of LDRs connected to an Arduino. Here is the schematic:
Here is how you hook it up to the Arduino:
The Arduino sketch is very simple – it just reads the values from analog pins A0 and A1 (in the range [0, 1023]) and prints it to the serial port. Here is the code:
The serial port sends values in the format:
On the computer side, I need to read these values, and plot them as a function of time. I am using Python and the Matplotlib library for this. I wanted to display this as a scrolling graph that moves to the right as data keeps coming in. For that, I am using the Python deque class to keep and update a fixed number of data points for each time frame.
You can see the full implementation here:
And here is what the plot looks like. It scrolls to the right as data keeps coming in.
Say you have a microcontroller circuit that does something when it goes dark. To save power, you want to put the chip to sleep when the ambient light drops below a certain level. One way to do this is using an LDR and an op-amp comparator.
Here is the schematic:
In the above circuit, the reference voltage at the non-inverting terminal of the op-amp is VCC/2. When it’s dark, the LDR has a high resistance (over 20K), and the voltage at the inverting terminal (pin 3) is going to be less than VCC/2. Hence, the output of the op-amp will go to high when it is dark. When sufficient light falls on the LDR, its resistance falls, and the voltage at the inverting terminal (pin 2) exceeds VCC/2. At this point, the op-amp output goes low. We can control the threshold at which it goes from low to high by adjusting the potentiometer R1.
Here is what the circuit looks like on a breadboard. The supply is 5V regulated.
In this case, I am using the LM358 – a very popular general-purpose Op-Amp IC. This works fine for our purpose, but do note that there are dedicated comparator ICs with better switching characteristics for critical applications.
In my next post, I will describe how to hook this up to an ATmega168 and wake it up from power-save mode.