Home   |  Schematics |  Products |  Tutorials  |  Datasheets  |  Robotics   |   Download    |   Link Exchange


Direct Current
Alternating Current
Digital Electronics
PC Architecture
Electronics Dictionary
Resources

Experiment
Calculator/Converters
Radio
Newsletter
Associations and Societies
Component Manufacturers

 

Circuits


Microcontroller Advanced Kit - Light Sensor Project

This tutorial shows how to set up a microcontroller based system that converts a signal from a light sensor to a 6 bit digital value. This value can be used by the microcontroller, perhaps for a robotic controller, or as in this tutorial, sent to the PC. It uses the AT89C2051 microcontroller to collect data and send it to the PC.  A MAX232CPE chip is used to convert the signals from and to RS232 levels for sending and receiving through the serial port. The 2051 microcontroller has a built in analog comparator that is used to make a simple analog to digital converter to convert the light sensor output to a digital value.

This tutorial is similar to the Data Collection tutorial.

Refer to the diagram below to build the circuit. The Data Collection Tutorial has more detailed instructions on building the circuit (except for the resistor network).

The Light Sensor

Light sensors are one of the most common types of sensors. They are used in night lights, street lights, alarms, toys, cameras, etc. We are using a CDS (Cadmium Sulfide) Photocell to detect light. The resistance of the sensor varies based on the amount of light that hits it.  The resistance can vary from 300K in the dark to 1K in the light. Our goal is to convert this into a digital value. We have to convert the variable resistance into a voltage and then the voltage into a digital value. To convert the resistance into a voltage, we use a second resistor Rb. Then assuming that no current goes into pin 13, you can find V1. To find V1 you can use Ohms Law. With Vcc = 5, Ohms Law gives you

(5 - V1) / Rpc = V1 / Rb

where Rpc is the resistance of the sensor (photocell).

Then you can solve for V1 = 5*Rb / (Rpc + Rb)

Then the maximum voltage is when Rpc is at its minimum, 1K. Then V1max = 5*Rb / (1k + Rb)
The minimum voltage is when Rpc = 300K. V1min = 5*Rb / (300k + Rb).

Using the equations for V1 max and min you can determine that 5.1K is a good value for Rb. 5.1K gives you a wide voltage range from minimum to maximum. 5.1K works well for general light to dark situations. If you are only interested in bright environments (are you making an outside robot?) then use a larger value of Rb to shift the light sensitivity range towards bright lights (Perhaps 50K). Or if you are interested in dark environments (are you making a robotic vampire dog that barks at the moon and hides from bright lights?) then use a smaller value of Rb (perhaps 510).

Now we have a sensor voltage, V1, that varies from about 0.1 volts to 4.2 volts. If you want to take the easy route you can use the hardware set up in the Data Collection tutorial to convert this voltage to an eight bit digital value.

Analog to Digital Conversion using the 2051

The Data Collection Tutorial shows how to use a ready made Analog to Digital converter chip to get data from an analog source. This tutorial shows a different method. This method is not as accurate as the ADC0804 but it is less expensive, uses less power,  and is easily modified to suit specific needs.

This method uses the built in analog comparator on the 2051 (not a normal feature of 8051 based chips). The voltage generated by the sensor circuit is connected to the negative input of the comparator (P1.1) and we will generate a voltage to connect to the positive input of the comparator (P1.0). The output of the comparator goes to P3.6. P3.6 is not an external pin on the 2051. It can only be accessed by the internal software. If the voltage at P1.0 is higher than P1.1 then P3.6 will be a 1. If the voltage at P1.0 is lower than P1.1 then P3.6 will be a 0.

By using the other 6 Port 1 pins (P1.2 through P1.7) we can generate a voltage using a resistor network connected to those pins. By changing the values of the Port 1 pins we will get as close as possible to matching the voltage from the sensor circuit. Then we will have a 6 bit digital value that is a reflection of the sensor voltage at P1.1.

Each of the 6 Port 1 pins is connected to V0 through a resistor. Setting a pin to 0 or 1 subtracts from the voltage at V0 or doesn't. The value of the resistor determines how much voltage is subtracted. If all 6 pins are set to 1 then no current is flowing through the resistor network and V0 = 5V. The small resistor on P1.7 (240 ohms) can subtract the most voltage. When we set it to 0 current flows through Ra and the voltage at V0 goes down. The exact amount depends on the value of Ra. The resistors are chosen so they are roughly twice the value of the resistor connected to the next higher pin. (Ideally they would be exactly double the other value but it is difficult to get resistors that have exactly the right values.) By doubling the resistance, the pin can subtract half as much voltage. When you get to P1.2 with the 10K resistor, it only has a small effect on the voltage at V0 when you set P1.2 to 0 or 1.

The actual voltage at V0 is determined by the resister Ra. To find a good value for Ra look at what happens when our digital output  is about at the half way point 011111. P1.7 is the only pin that is drawing current. Starting at Vcc, the current goes through Ra and then through the 240 ohm resistor to ground (P1.7 = 0). To make the voltage at V0 equal to 2.5 volts (half of Vcc), make Ra 240 ohms. But since we know the sensor voltage V1 only goes up to 4.2 volts you may want to make the halfway point by 2.1 volts. Use 330 ohms for Ra to get 2.1 volts for the halfway digital output of 011111. 

Now we can control the voltage at V0 fairly accurately with P1.2 through P1.7. To make a small change in voltage, change the lower pins and to make a large change in voltage change the higher pins. By starting with P1.7 through P1.2 set to 000000 (P1.7 is on the left and P1.2 is on the right) and counting up to 111111 you can get 64 different voltages!

To find the right digital output to create the right voltage to match the voltage at P1.1 (V1), we start at 000000 and count up until the comparator output at P3.6 switches to 1 to tell us our generated voltage is higher than the sensor voltage. Then we can "track" the voltage by adjusting the value up and down depending on the output of the comparator. Since the comparator only tells us high or low (it can not tell you if you have an exact match) then one possibly annoying aspect of this approach is that the P1.2 bit is constantly switching from 0 to 1 to 0 to 1... as the comparator output tells us we are low, then high, then low.  To avoid having to watch the 6 bit value oscillate (also called jitter) we just use the top 5 digits as our answer. Look at the documentation in the software for the 2051 in light.asm for more details on the tracking routine.

The details of communicating with the PC are covered in the Data Collection Tutorial. For this project we are sending the upper 5 digit value (P1.3 through P1.7) to the PC. This can be displayed on the screen using the sample light program. After downloading, double click it to extract the files and then run setup.exe to install it.

The Software

The basic process of compiling an assembly language program and loading it into the microcontroller was covered in the first microcontroller project. The 2051 assembly language program for this project is light.asm.  You will need a device programmer such as the PG302 to reprogram the 2051.

Make sure the power is off to the circuit you have built.  Connect the circuit to the PC's serial port, Comm1. Connect the power to the breadboard.  The circuit should send a continuous stream of values to the PC.  To see the values on the PC, try this sample light program. After downloading, double click it to extract the files and then run setup.exe to install it. The source code for the sample program (written in VB 5.0) is on the CD included with the kit.

The parts for this project are included in the Sensor Kit. The Sensor Kit also includes the parts needed to do the temperature sensor project and the data collection project. The kit includes:

     1 - AT89C2051-24PC Microcontroller (unprogrammed)
     1 - 11.0592 MHz Crystal
     2 - 33pF Capacitors
     1 - 150pF Capacitor
     1 - 10 uF Capacitor
     1 - 220 uF Capacitor
     1 - 8.2k Resistor
     5 - 240 Ohm Resistors
     5 - 510 Ohm Resistors
     5 - 1k Resistors
     5 - 2.2k Resistors
     5 - 5.1k Resistors
     5 - 10k Resistors
     5 - 15k Resistors
     1 - MAX232
     1 - ADC0804 (Analog to Digital Converter IC)
     5 - 1 uF capacitors
     1 - DB9 connector
     1 - CDS Photocell Light Sensor
     1 - DS1620 Temperature Sensor
     Jumper Wires
 





Home  Products  Tutorials   Schematics   Robotics   Resources   Radio Stuff    Career    Download   Link Exchange

HTML Sitemap   XML Sitemap


Terms & Conditions  Privacy Policy and Disclaimer