Alright, thought I would share one of the little projects I have done lately, with a little background on how I did it. I built a light sensor that broke down the data and displayed it on a multiplexing LED display. It could go from .0% to 100.0%. The percentage is just arbitrarily chosen by the resistance value used with a cadmium sulfide photo-resistor. Basically it senses light by changing its resistance. The more light that collides with the sensor the resistance drops. Depending on how you have it wired the voltage output on the circuit either rises or falls with light levels.

I chose to make mine have the voltage raise with light levels. The brains behind this sensor is an ATMEGA168 running with a 16mhz quartz crystal. The ATMEGA168 is a microcontroller. Basically it is a solid state computer with a very small hard drive and an even smaller amount of RAM. It has 16k of storage in flash that the program is loaded onto, 1k of EEPROM or Electronically Erasable Programmable Read Only Memory and 512bytes of RAM. The EEPROM and Flash is what’s called non volatile memory. When the device looses power the non volatile memory remembers what was last on it while the RAM will be erased every time the device looses power.

The ATMEGA has an internal Analog to Digital Converter(or ADC) that is essential for this project. What this does is it takes a variable voltage and assigns a number to it. It is capable of either 8 or 10 bit resolution. The microcontroller operates at five volts, This is important because it limits the range that the ADC can see. I bet your wondering what does measuring resolution in bits mean exactly? One bit is capable of seeing two settings, either 1 or 0. So it would see 2.6 volts the same as 5 volts. Two bits can see 0-3, three bits can see 0-7, four can see 0-15. Every time you add one bit to the resolution it doubles the range of numbers you can see. 8 bits can see 0-255 and 10 bits can see 0-1023. What this means is if you can measure 0-5 volts with 10 bits of resolution you would divide 5 by 1023. The resulting number is roughly .005, the smallest increment of voltage change you can detect. So at 0 volts the ADC will register 0 and at .005 volts the ADC will register a 1, then .01 volts will equal 2, .015=3, .02=4 and so forth. When you reach 5 volts it will register 1023. Basically this is just putting the data in a form that is more useful to a computer.

Computers see the world in a very black and white way, but humans see it in many shades of gray so to speak. Something can be kind of warm, warmer, kinda hot, hot, really hot and so forth. We operate in a way that is very alien to a computer. Computers like to see things as either on, or off. Things would be either cold, or hot with no in between. The ADC allows the computer to see things more like we would.

Now you know what the microcontroller is doing as it takes a reading off of the photo-resistor. What it does with this is determined in its code. Here is the code I used

I believe that is the up to date code with no errors. You would need to use this C code and compile it into something that an ATMEGA168 could use. The code would need to be changed depending on what you plan on running it on. The display that I am using is multiplexing 4 digit 7 segment. Here is a datasheet to give you a better idea of what I am talking about. I got this LED display from sparkfun in case you want to try something similar.

The code then takes the ADC number and divides it by 1.023. What this does is it just takes the 0-1023 range and converts it to 0-1000. I did this so that I could insert a decimal point between the last two digits. This gives a percentile range accurate to a tenth of a percent. So instead of 0-1000 its 0.0-100.0. This is just for ease of the human looking at the display. The microcontroller still things its 0-1000 the decimal point is just a light as far as it’s concerned.

Here is a short video clip just showing what this unit does.

– Edit –

I should also note that if you do this project use some good sized resistors on the pins you have acting as the ground on the display. Each pin is rated for 40mA but each segment on the LED display will draw 20mA and the microcontroller as a whole is rated at 200mA. So if you do not run large enough resistors you can do damage to your microcontroller. The display is running only one digit at a time, its just blinking them on and off so fast the camera and human eye can’t perceive it but that is still upwards to 140mA that each digit can draw.

Share this: Twitter

Facebook

Like this: Like Loading...

Posted in AVR

Tags: ATMEGA, AVR, Microcontroller