You are on page 1of 30

Line follower robot using 8051.

Line follower robots were one of the earliest automatic guided robots. They are able to follow a line marked on a contrasting background, usually a black line on a white surface or a white line on a black surface. Usually the line follower robot works on a closed loop feedback algorithm where the feedback from the line sensor is used by the controller for correcting the path of the robot. The sensors are usually LED/LDR, LED/Photodiode or LED/Phototransistor pairs and the controller is an electronic circuit which executes the desired feedback algorithm. Gear motors are used for driving the robotic wheels. The line follower robot presented here is designed to follow a black line on a white background. It has a pair of sensors (LED / LDR) and works on a simple align robot on center of the line algorithm. Actually you does not need a microcontroller for implementing such a simple robot. A set of comparators and a motor driver circuit will happily do the job. But I am using the microcontroller just to demonstrate the technology. Also this project serves as a platform for advanced line follower robots which works on complex algorithms. AT89S52 from Atmel is the microcontroller used here.

Sensor.
The sensor part consists of a set of LED / LDR pairs for the left side and right sides. These LED / LDR pairs detect the black line on the white surface on which the robot is supposed to roam. The LDR has an inverse relationship between its resistance and the light falling on it. When a particular LED / LDR pair is above the white surface the reflected light falls on the LDR and its resistance drops, conversely when the LED / LDR pair is above the black line, its resistance rises. This variation in resistance of the LDRs are used to asses the orientation of the line follower robot in the X-Y plane. The figure shown below depicts the sensor circuit.

In the circuit, resistors R1 and R2 limits the current through the illuminating LEDs D1 and D2. Resistors R3, R5 and R6, R8 forms a voltage divider network together with the corresponding LDRs. The output

of the sensor circuit is taken from the points labelled A and B in the circuit diagram.The table below shows the voltage at nodes A and B for the possible orientations of the sensor module.

Comparator circuit.
The job of the comparator circuit is to convert analog voltage output of the sensor into a digital format for the microcontroller to read. The comparator circuit is built around opamp IC LM324 (IC1). LM324 is a general purpose quad opamp which can be operated from a single supply. Out of the four comparators inside LM324, only two are used here. One for the left side and the other for the right side. Circuit diagram of the comparator section is shown in the figure below.

Preset resistor R10 and R11 are used to set the 1V reference for the left and right comparators respectively. Output from the left and right sensors (node A and B) are connected to the non inverting input on the left and right comparators. Output of the left comparator is connected to P1.o of the microcontroller and output of the right comparator is connected to P1.1 of the microcontroller. Both comparators are wired in non inverting mode and the table given below shows their output voltage with respect to the possible input voltage combinations.

Microcontroller (AT89S52).
The task of the microcontroller here is to control the left and right motors according to the feedback signals from the left and right comparators so that the robot remains on the correct path (the black line). The logic executed by the microcontroller for keeping the robot in track is illustrated in the table below.

Motor driver circuit.


The job of the motor driver circuit is to drive the motors according to the output signals from the microcontroller. The motor driver circuit is based on ULN2003A IC. ULN2003A is a high current (500mA), high voltage (50V) darlington array consisting of seven darlington pairs with common emitter and open collector. Out of the seven channels available in the IC,only two are used here. One for the left channel and one for the right channel. Schematic of the motor driver circuit is shown in the figure below. The operation of ULN2003 is very simple to explain. When a particular input line (say pin 1) is made high the corresponding output line (pin 16 goes low) and vice versa.

Capacitors C4 and C5 isolates the remaining parts of the circuit from the electric interference produced by the motor. The back emf voltage produced when motor is switched and the voltage spikes due to arcing of brushes mainly accounts for the above said electrical interference. These capacitors are very essential and without them you can expect sudden crashes from the microcontroller side.

Complete circuit diagram.

Switch S1, capacitor C3 and resistor R9 forms a debouncing reset circuit for the microcontroller. Capacitors C1, C2 and 12MHz crystal X1 are associated with the microcontrollers clock circuit. R12 and R13 are pull-up resistors. Remaining sections of the circuit were explained already.

Program.
ORG 000H MOV P1,#00000011B MOV P0,#00000000B BACK: MOV P0,#00000011B JB P1.0, LABEL1 ON CLR P0.0 SETB P0.1 ACALL WAIT1 SJMP BACK LABEL1: JB P1.1, LABEL2 ON // // // // // // // // // // origin sets port 1 sets port 0 starts both branches to as input port as output port motors LABEL1 if left sensor is

stops left motor runs right motor calls WAIT1 subroutine jumps back to the BACK loop branches to LABEL2 if right sensor is

SETB P0.0 CLR P0.1 ACALL WAIT2 SJMP BACK LABEL2: SJMP BACK WAIT1:JNB P1.0,WAIT1 rightward deviation RET WAIT2:JNB P1.1,WAIT2 leftward deviation RET END

// // // // // //

runs left motor stops right motor calls WAIT2 subroutine jumps back to the BACK loop jumps back to the BACK loop waits until robot is back from

// returns from WAIT1 subroutine // waits until robot is back from // returns from WAIT2 subroutine // end statement

About the program.


The first part of the program initializes Port 1 as input port and Port 0 as output part. After this, both motors are started so that the robot goes straight. Then the programs checks whether there is a deviation to right. If there is a deviation to right, the program stops left motor and runs right motor and waits until the robot comes back from the deviation. When the robot is back on line again, both motors are started. If there is no deviation to right, the program checks for a deviation to left. If there is a deviation to left, left motor is stopped and right motor is activated. This condition is maintained until the robot is back on track. When the robot is back on track again, both motors are started. Lastly if there is no deviation to left or right, both motors are kept ON.

Notes.

A 6V battery can be used for powering the circuit even though the power supply shown in the circuit diagram is 5V DC. For setting up the robot, place the robot on the line so that both the sensor pairs point on white and the black line goes in between them. Then adjust preset resistors R10 and R11 so that the LEDs D3 and D4 glows. Sensor LEDs D1 and D2 are ultra-bright green LEDs. OPAMP output LEDs D3 and D4 are general purpose, miniature, yellow LEDs. Sensor LDRs R4 and R7 are general purpose LDRs.

Alcohol Breathalyzer (AT89S51).

circuit

using

8051

microcontroller

This article is about a breathalyzer circuit using 8051 microcontroller which outputs the blood alcohol content (BAC) from the breath. The BAC is displayed in percentage on a 3 digit seven segment display. The microcontroller used if AT89S51 which belongs to the 8051 family and the alcohol sensor is MQ135 gas sensor from Futurelec.

MQ135 gas sensor.


MQ135 is a stable and sensitive gas sensor which can detect ammonia, carbon dioxide, alcohol, smoke, nitrogen dioxide etc. The sensor consists of a tin dioxide sensitive layer inside aluminium oxide micro tubes, measuring electrode and a heating element inside a tubular aluminium casing. The front end of the sensor is covered using a stainless steel net and the rear side holds the connection terminals. The ethyl alcohol present in the breath is oxidized into acetic acid while passing over the heating element. This ethyl alcohol falls on the tin dioxide sensing layer and as a result its resistance decreases. This resistance variation is converted into a suitable voltage variation using an external load resistor. The typical connection arrangement of an MQ135 alcohol sensor is shown below.

MQ135 alcohol sensor

MQ135 has different resistance values at different temperature and different concentration of gases. The manufacturer recommends to calibrate the sensor at 100ppm of ammonia or 50ppm of alcohol. The recommended value of the load resistor is between 10K to 47K.

Circuit diagram.

Breathalyzer using 8051

The voltage output of the alcohol sensor is converted into a digital format using the ADC0804 (IC1). The Vref/2 pin of the ADC is held at 1.28V using the voltage divider network made of R14 and R15. Vref/2 =1.28V means the step size of the ADC will be 10mV and the output of the ADC will increment by one bit for every 10mV increment in the analog input. Refer the datasheet of ADC0804 for a better grasp. Digital out of the ADC (D0 to D7) is interfaced to Port1 of the microcontroller. Control signals CS, RD, WR, INTR are obtained from the microcontrollers P3.7, P3.6, P3.5, P3.4 pins respectively. R9 and C1 are associated with the clock circuitry of the ADC0804. Capacitor C3 connected between Vin+ and Vin- of the ADC0804 filters of noise (if any) in the sensor output. If C3 is not used the digital output of the ADC will not be stable. This filter capacitor will surely induce some lag in the ADC response but it is not very relevant in this entry level application. The microcontroller performs required manipulations on the

ADC digital output in order to convert it into BAC % and displays it on the three digit seven segment display. Port0 of the microcontroller is interfaced to the multiplexed three digit seven segment display. The drive signals for the threes digits are obtained from the microcontrollers P3.0, P3.1, P3.2 pins respectively.

Program.
ORG 00H MOV P1,#11111111B MOV P0,#00000000B MOV P3,#00000000B MOV DPTR,#LUT MAIN: MOV R4,#250D CLR P3.7 SETB P3.6 CLR P3.5 SETB P3.5 WAIT: JB P3.4,WAIT CLR P3.7 CLR P3.6 MOV A,P1 MOV R5,A SUBB A,#86 JC NEXT SETB P3.3 CLR PSW.7 NEXT: MOV A,R5 SUBB A,#115D JNC LABEL MOV A,#00000000B CLR PSW.7 LABEL: MOV B,#5D MUL AB MOV B,#8D DIV AB MOV B,#10D DIV AB MOV R6,A MOV R7,B DLOOP:SETB P3.0 MOV P0,#01000000B

ACALL DELAY CLR P3.0 SETB P3.1 MOV A,R6 ACALL DISPLAY MOV P0,A ACALL DELAY CLR P3.1 SETB P3.2 MOV A,R7 ACALL DISPLAY MOV P0,A ACALL DELAY CLR P3.2 DJNZ R4,DLOOP SJMP MAIN DELAY: MOV R3,#255D LABEL1: DJNZ R3,LABEL1 RET DISPLAY: MOVC A,@A+DPTR CPL A RET LUT: DB 3FH DB 06H DB 5BH DB 4FH DB 66H DB 6DH DB 7DH DB 07H DB 7FH DB 6FH END

Notes.

The MQ135 gas sensor requires around 5 minutes of preheat before the first use. The MQ135 takes few minutes to retrace back to its normal condition after a positive test (alcohol present in the breath). If there is no alcohol in the breath the sensor output will swing back to its normal condition very fast.

Read these articles Interfacing seven segment display to 8051 microcontroller , Interfacing ADC to 8051 microcontroller before attempting this project. This breathalyzer circuit is just an entry level one and is not suitable for high end applications such as law enforcement or laboratory application. The logic for converting the digital output of ADC into BAC percentage was obtained using approximation techniques.

Ultrasonic range finder using 8051 .


A simple ultrasonic range finder using 8051 microcontroller is presented in this article. This ultrasonic rangefinder can measure distances up to 2.5 meters at an accuracy of 1 centi meter. AT89s51 microcontroller and the ultrasonic transducer module HC-SR04 forms the basis of this circuit. The ultrasonic module sends a signal to the object, then picks up its echo and outputs a wave form whose time period is proportional to the distance. The microcontroller accepts this signal, performs necessary processing and displays the corresponding distance on the 3 digit seven segment display. This circuit finds a lot of application in projects like automotive parking sensors, obstacle warning systems, terrain monitoring robots, industrial distance measurements etc.

HC-SR04 ultrasonic module.


HC-SR04 is an ultrasonic ranging module designed for embedded system projects like this. It has a resolution of 0.3cm and the ranging distance is from 2cm to 500cm. It operates from a 5V DC supply and the standby current is less than 2mA. The module transmits an ultrasonic signal, picks up its echo, measures the time elapsed between the two events and outputs a waveform whose high time is modulated by the measured time which is proportional to the distance. .The photograph of an HC-SR04 module is shown below.

HC-SR04 ultrasonic ranging module (Fig1)

The supporting circuits fabricated on the module makes it almost stand alone and what the programmer need to do is to send a trigger signal to it for initiating transmission and receive the echo signal from it for distance calculation. The HR-SR04 has four pins namely Vcc, Trigger, Echo, GND and they are explained in detail below. 1) VCC : 5V DC supply voltage is connected to this pin. 2) Trigger: The trigger signal for starting the transmission is given to this pin. The trigger signal must be a pulse with 10uS high time. When the module receives a valid trigger signal it issues 8 pulses of 40KHz ultrasonic sound from the transmitter. The echo of this sound is picked by the receiver. 3)Echo: At this pin, the module outputs a waveform with high time proportional to the distance. 4) GND: Ground is connected to this pin.
HC-SR04 timing diagram.

HC-SR04 Timing diagram

From the timing diagram, you can see that the 40KHz pulse train is transmitted just after the 10uS triggering pulse and the echo output is obtained after some more time. The next triggering pulse can be given only after the echo is faded away and this time period is called cycle period. The cycle period for HC-SR04 must not be below 50mS. According to datasheet, the distance can be calculated from the echo pulse width using the following equations. Distance in cm = echo pulse width in uS/58 Distance in inch = echo pulse width in uS/148

Ultrasonic range finder using 8051- Circuit diagram.

Ultrasonic range finder using 8051

The ultrasonic module is interfaced to the microcontroller through P3.0 and P3.1 pins. Port0 used for transmitting the 8 bit display data to the display and port pins P1.0, P1.1, P1.2 are used for transmitting display drive signals for the corresponding display units D1, D2, D3. Push button switch S1, capacitor C3 and resistor R9 forms a de-bouncing reset circuitry. Capacitors C1,C2 and crystal X1 are associated with the clock circuit.

Program.
ORG 00H MOV DPTR,#LUT MOV P1,#00000000B MOV P0,#00000000B CLR P3.0 SETB P3.1 MOV TMOD,#00100000B // // // // // // // origin moves the address of LUT to DPTR sets P1 as output port sets P0 as output port sets P3.0 as output for sending trigger sets P3.1 as input for receiving echo sets timer1 as mode 2 auto reload timer

MAIN: MOV TL1,#207D from MOV TH1,#207D MOV A,#00000000B SETB P3.0 ACALL DELAY1 CLR P3.0 HERE: JNB P3.1,HERE BACK: SETB TR1 HERE1: JNB TF1,HERE1 count) CLR TR1 CLR TF1 INC A JB P3.1,BACK MOV R4,A ACALL DLOOP SJMP MAIN

// loads the initial value to start counting // // // // // // // // // // // // // // // loads the reload value clears accumulator starts the trigger pulse gives 10uS width for the trigger pulse ends the trigger pulse loops here until echo is received starts the timer1 loops here until timer overflows (ie;48 stops the timer clears timer flag 1 increments A for every timer1 overflow jumps to BACK if echo is still available saves the value of A to R4 calls the display loop jumps to MAIN loop

DELAY1: MOV R6,#2D // 10uS delay LABEL1: DJNZ R6,LABEL1 RET DLOOP: MOV R5,#100D BACK1: MOV A,R4 MOV B,#100D DIV AB SETB P1.0 ACALL DISPLAY MOV P0,A to P0 ACALL DELAY ACALL DELAY MOV A,B MOV B,#10D DIV AB CLR P1.0 SETB P1.1 ACALL DISPLAY // // // // // // // loads R5 with 100D loads the value in R4 to A loads B with 100D isolates the first digit activates LED display unit D1 calls DISPLAY subroutine moves digit drive pattern for 1st digit

// 1mS delay // // // // // moves the remainder of 1st division to A loads B with 10D isolates the second digit deactivates LED display unit D1 activates LED display unit D2

MOV P0,A to P0 ACALL DELAY ACALL DELAY MOV A,B CLR P1.1 SETB P1.2 ACALL DISPLAY MOV P0,A digit to P0 ACALL DELAY ACALL DELAY CLR P1.2 DJNZ R5,BACK1 RET

// moves digit drive pattern for 2nd digit

// moves the remainder of 2nd division to A // deactivates LED display unit D2 // activates LED display unit D3 // moves the digit drive pattern for 3rd

// deactivates LED display unit D3 // repeats the display loop 100 times

DELAY: MOV R7,#250D LABEL2: DJNZ R7,LABEL2 RET DISPLAY: MOVC A,@A+DPTR content in A CPL A (see Note 1) RET LUT: DB 3FH DB 06H DB 5BH DB 4FH DB 66H DB 6DH DB 7DH DB 07H DB 7FH DB 6FH END

// 1mS delay

// gets the digit drive pattern for the // complements the digit drive pattern

// look up table (LUT) starts here

About the program.


The first part of the program sets the initial conditions. Port 0 and P0rt 1 are set as output ports for sending digit drive patterns and digit drive signals respectively. Port pin 3.0 is set

as an output pin for sending the trigger signal to the ultrasonic module for starting transmission and port pin 3.1 is set as an input pin for receiving the echo. TMOD register of the microcontroller is so loaded that the Timer 1 operates in mode2 8 bit auto-reload mode. Timer 0 of the microcontroller is not used here. In the next part of the program (loop MAIN) the TL1 and TH1 registers of Timer1 are loaded with the initial values. TL1 is loaded with the initial value to start counting from and TH1 is loaded with the reload value. This is how timer 1 in mode 2 works: When TR1 bit of the TCON register is set the TL1 starts counting from the initial value loaded into it and keeps counting untill roll over (ie; 255D). When roll over occurs, TF1 flag is set and TL1 is automatically loaded with the reload value stored in TH1 and the sequence is repeated until TR1 is made low by the program. The TF1 goes high at the first roll over and if you want it as an indicator for each roll over, you have to clear it using the program after each roll over. In the next part of the MAIN loop P3.0 is set high for 10uS and then cleared to make 10uS triggering pulse. The ultrasonic module issues a 40Khz pulse wave form after receiving this trigger and the program waits until a valid echo is received at P3.1. The pulse width of the echo signal is proportional to the distance to the obstacle and so the next job of the program is to measure the pulse width. Whenever there is a valid echo pulse at P3.1, the Timer1 starts and it counts from the initial value to 255 ie: 255-207= 48 counts. Then the counter restarts and accumulator increments by one for every restart. This sequence is repeated until the echo signal at P3.1 vanishes (ie; P3.1 goes low). Now the content in A will be equal to the number of Timer1 reloads which is in fact proportional to the distance. From the datasheet it is clear that 58uS echo pulse width indicates 1cM distance. When the processor is clocked by a 12MHz crystal, 58 counts of Timer1 indicates 1cM. That means 1 reload is equal to 1cM. But here we are letting the Timer1 to count only 48 times before reload and this is done in order to compensate for the time lags caused by the branching instructions used for checking the status of P3.0 and P3.1 pins. If this trick is not done, the individual time lags caused by the branching instructions will be cumilatively added to the observed pulse width and the range finder will show a reading higher than the original distance. Some trial and error was required for getting the correct Timer1 reload value and with the 207D (ie; 48 counts) used here the error was found to be less than half a centimeter which is quite fine in this context. The next part of the program does necessary mathematics on the current content in A and displays it as 3 digit readout on the display.

Notes.
1) The LUT used here was originally made for a common cathode seven segment display and here we are using common anode displays. The CPL A instruction will just complement the obtained digit drive pattern and make is suitable for the common anode scheme. If you have time ,then cook up an LUT for common anode scheme and replace the current one using it. By this you can avoid the extra CPL A instruction and it is the correct method. 2)The entire circuit can be powered from 5V DC.

3) Be careful while handling the Ultrasonic module. There are a lot of sensitive surface mount devices fabricated on its back side. 4)Go through these articles: Interfacing seven segment display to 8051, Delay using 8051 timer, Software delay routines using 8051.

Contactless digital tachometer using 8051.


A three digit contact less digital tachometer using 8051 microcontroller which can be used for measuring the revolutions/second of a rotating wheel, disc, shaft or anything like that is introduced in this project. The tachometer can measure up to a maximum of 255 rev/sec at an accuracy of 1 rev/sec. What you just need to do is to align the sensor close to the reflective strip (aluminium foil, white paper or some thing like that) glued on the rotating surface and the meter shows the rev/sec on the display. The circuit diagram of the digital tachometer is shown below.

Digital tachometer using 8051

The first section of the circuit is the optical pickup based on photo transistor Q4 and red LED D4. Every time the reflective stripe on the rotating object passes in front of the sensor assembly, the reflected light falls on the photo transistor which makes it conduct more and as a result its collector voltage drops towards zero. When viewed through an oscilloscope the collector waveform of the photo transistor Q4 (2N5777) would look like this:

Next part is the signal conditioning unit based on the opamp LM324 (IC1). Only one opamp inside the quad LM324 is used here and it is wired as a comparator with reference voltage set at 3.5V (using resistors R16 and R17). The job of this comparator unit is to convert the spiky collector wave form into a neat square pulse train so that it can be applied to the microcontroller. Every time the collector voltage of the photo transistor goes below 3.5V, the output of the comparator goes to negative saturation and every time the collector voltage of the photo transistor goes above 3.5V, the comparator output goes to positive saturation resulting in a waveform like this:

From the above two graphs you can see that the negative going edge of the waveform indicates the passage of the reflective patch across the sensor and that means one revolution. If you could some how measure the number of negative going edges occurring in one second, then thats the rev/sec of the rotating object and thats what the microcontroller does here. The 8051 microcontroller here does two jobs and they are: 1) Count the number of negative going pulses available at its T1 pin (pin15). 2) Do necessary mathematics and display the count on the 3 digit 7 segment display. For the counting purpose both the timers of 8051 (Timer0 and Timer1) are used. Timer 1 is configured as an 8 bit auto reload counter for registering the number of incoming zero going pulses and Timer0 is configured as a 16 bit timer which generate the necessary 1 second time span for the Timer1 to count.

Program.
ORG 000H MOV DPTR,#LUT // moves the addres of LUT to DPTR

MOV P1,#00000000B MOV P0,#00000000B MAIN: MOV R6,#14D SETB P3.5 MOV TMOD,#01100001B as Mode1 timer MOV TL1,#00000000B MOV TH1,#00000000B SETB TR1 BACK: MOV TH0,#00000000B MOV TL0,#00000000B SETB TR0 HERE: JNB TF0,HERE CLR TR0 CLR TF0 DJNZ R6,BACK CLR TR1 CLR TF0 CLR TF1 ACALL DLOOP the count SJMP MAIN DLOOP: MOV R5,#100D BACK1: MOV A,TL1 accumulator MOV B,#100D DIV AB SETB P1.0 ACALL DISPLAY pattern MOV P0,A ACALL DELAY ACALL DELAY MOV A,B MOV B,#10D DIV AB count CLR P1.0 SETB P1.1

// Sets P1 as an output port // Sets P0 as an output port

// Sets Timer1 as Mode2 counter & Timer0 //loads initial value to TL1 //loads initial value to TH1 // starts timer(counter) 1 //loads initial value to TH0 //loads initial value to TL0 //starts timer 0 // checks for Timer 0 roll over // stops Timer0 // clears Timer Flag 0 // // // // stops Timer(counter)1 clears Timer Flag 0 clears Timer Flag 1 Calls subroutine DLOOP for displaying

// jumps back to the main loop // loads the current count to the

// isolates the first digit of the count // converts the 1st digit to 7 seg // puts the pattern to Port 0 // 1mS delay

// isolates the secong digit of the

ACALL DISPLAY pattern MOV P0,A ACALL DELAY ACALL DELAY MOV A,B accumulator CLR P1.1 SETB P1.2 ACALL DISPLAY pattern MOV P0,A ACALL DELAY ACALL DELAY CLR P1.2 DJNZ R5,BACK1 times RET DELAY: MOV R7,#250D DEL1: DJNZ R7,DEL1 RET DISPLAY: MOVC A,@A+DPTR current value in A CPL A RET LUT: DB 3FH DB 06H DB 5BH DB 4FH DB 66H DB 6DH DB 7DH DB 07H DB 7FH DB 6FH END

// converts the 2nd digit to 7 seg

// moves the last digit of the count to

// converts the 3rd digit to 7 seg

// repeats the subroutine DLOOP 100

// 1mS delay

// gets 7 seg digit drive pattern for // (See Note 1)

// Look up table (LUT) starts here

Notes.

1) The LUT used here was made for a common cathode seven segment display (used in previous projects) and here we are using a common anode display. The instruction CPL A will just complement the digit drive pattern in accumulator so that it becomes suitable for the common anode display. This is done just because to save my time but not a text book method. The correct way is to make a dedicated LUT for common anode configuration and aviod the extra CPL A instruction. 2) LM324 is a quad opamp and only one opamp inside it is used here. I used LM324 just because that was the only single supply opamp with me at the time. You can use any single supply opamp that matches our supply voltage(5V). You can even use a dual supply opamp (like the popular 741) in single supply mode (+V pin connected to positive supply and -V pin connected to ground) but i wont recommend it unless you have an oscilloscope. Dual supply opamps configured in single supply mode will not give results like a dedicated single supply opamp in the same situation. 3) As we saw earlier the Timer 0 which generates the 1 second time span is configured in Mode 1 (16 bit timer). So the maximum it can count is 2^16 and that is 65536. In 8051 the crystal frequency is divided by 12 using an internal network before applying it as a clock for the timer. That means the timer will increment by one for every 1/12th of the crystal frequency. For an 8051 system clocked with a 12MHz crystal the time taken for one timer increment will be 1S (ie; 1/12MHz). So the maximum time delay that can be obtained using one session of the timer will be 65536S and it is looped 14 times to get the 1 second delay. Go through this article Delay using 8051 timer for a better grasp. 4) Also read this article Interfacing seven segment display to 8051 before attempting this project.

Voltmeter using 8051.


A simple 0-5V voltmeter using 8051 is shown in this article. This digital voltmeter has a sensitivity of 200mV which is a bit low but this project is meant for demonstrating how an ADC and seven segment display can be interfaced to 8051 to obtain a digital readout of the input voltage. A 31/2 digit high end voltmeter will be added soon. ADC0804 is the ADC and AT89S51 is the controller used in this project. Before attempting this project, go through these projects Interfacing ADC to 8051 and Interfacing seven segment display to 8051 which will give you a good idea on the basics.

Circuit diagram.

0-5 digital voltmeter using 8051

About the circuit.


In the circuit Vref/2 (pin9) of the ADC is left open and it means that the input voltage span can be o to 5V and the step size will be 5/255 = 19.6mV. The equation for the digital output of ADC0804 is Dout = Vin/Step size. In this circuit, for an input voltage of 1V the digital output will be 1/19.6mV = 51 and so the binary equivalent of 51 ie 00110011. Digital output of the ADC is interfaced to P1.0 of the microcontroller. Control signals for the ADC ie CS, RD, WR and INTR are available from the P3.7, P3.6, P3.5 and P3.4 pins of the microcontroller respectively. 2 digit multiplexed seven segment display is interfaced to Port0 of the microcontroller. Control signals for the display driver transistors Q1 and Q2 are obtained from P3.2 and P3.1 of the microcontroller. Push button switch S1, capacitor C2 and resistor R10 forms a debouncing reset circuitry.

Program.

ORG MOV MOV MOV MOV MAIN: SETB CLR SETB WAIT: CLR CLR MOV MOV DIV MOV MUL MOV DIV SETB ACALL MOV ACALL MOV ACALL MOV CLR SETB ACALL MOV ACALL CLR SJMP DELAY: DEL1: DEL2: DJNZ RET DISPLAY: MOVC MOV MOV DJNZ JB CLR

00H P1,#11111111B P0,#00000000B P3,#00000000B DPTR,#LABEL P3.7 P3.6 P3.5 P3.5 P3.4,WAIT P3.7 P3.6 A,P1 B,#10D AB B,#2D AB B,#10D AB P3.2 DISPLAY P0,A DELAY P0,#10000000B DELAY A,B P3.2 P3.1 DISPLAY P0,A DELAY P3.1 MAIN R3,#02H R2,#0FAH R2,DEL2 R3,DEL1 A,@A+DPTR

RET LABEL: DB DB DB DB DB DB DB DB DB END DB 3FH 06H 5BH 4FH 66H 6DH 7DH 07H 7FH 6FH

About the program.


At first the program controls the ADC to produce a digital output corresponding to the input voltage.This digital output is scanned through P1.0 and is loaded to accumulator. Then the value in the accumulator is divided by 10 to omit the last digit. For example, let the input voltage be 4V. Then the corresponding digital output of the ADC will be 204D (D stands for decimal) .After the the division by 10, the value left in the accumulator will be 20D. This 20D is then multiplied by 2D which results in 40D. The next target of the program is to manipulate this 40D and make a 4.0 readout on the display. For this the 40D is again divided by 10D . This results in 4 inside accumulator and 0 inside B register. Then the program gets the digit drive pattern for 4 using the lookup table , puts this pattern on Port 0 and activates Q1. After 1 ms delay 10000000B is loaded to P0 and this accounts for the dot. After a further 1ms delay Q1 is deactivated, content in B (ie 0) is moved to A, gets the correct digit drive pattern for 0 using the lookup table, puts this pattern on Port 0 and activates Q2. After a further 1ms delay Q2 is deactivated and the entire cycle is repeated.

ADC (Analog to digital converter) forms a very essential part in many embedded projects and this article is about interfacing an ADC to 8051 embedded controller. ADC 0804 is the ADC used here and before going through the interfacing procedure, we must neatly understand how the ADC 0804 works.

ADC 0804.
ADC0804 is an 8 bit successive approximation analogue to digital converter from National semiconductors. The features of ADC0804 are differential analogue voltage inputs, 0-5V input voltage range, no zero adjustment, built in clock generator, reference voltage can be externally adjusted to convert smaller analogue voltage span to 8 bit resolution etc. The pin out diagram of ADC0804 is shown in the figure below.

ADC0804 pinout

The voltage at Vref/2 (pin9) of ADC0804 can be externally adjusted to convert smaller input voltage spans to full 8 bit resolution. Vref/2 (pin9) left open means input voltage span is 0-5V and step size is 5/255=19.6V. Have a look at the table below for different Vref/2 voltages and corresponding analogue input voltage spans. Vref/2 (pin9) (volts) Left open 2 1.5 1.28 1.0 0.5 Input voltage span (volts) 05 04 03 0 2.56 02 01 Step size (mV) 5/255 = 19.6 4/255 = 15.69 3/255 = 11.76 2.56/255 = 10.04 2/255 = 7.84 1/255 = 3.92

Steps for converting the analogue input and reading the output from ADC0804.

Make CS=0 and send a low to high pulse to WR pin to start the conversion. Now keep checking the INTR pin. INTR will be 1 if conversion is not finished and INTR will be 0 if conversion is finished. If conversion is not finished (INTR=1) , poll until it is finished. If conversion is finished (INTR=0), go to the next step. Make CS=0 and send a high to low pulse to RD pin to read the data from the ADC.

Circuit diagram.

Interfacing ADC to 8051

The figure above shows the schematic for interfacing ADC0804 to 8051. The circuit initiates the ADC to convert a given analogue input , then accepts the corresponding digital data and displays it on the LED array connected at P0. For example, if the analogue input voltage Vin is 5V then all LEDs will glow indicating 11111111 in binary which is the equivalent of 255 in decimal. AT89s51 is the microcontroller used here. Data out pins (D0 to D7) of the ADC0804 are connected to the port pins P1.0 to P1.7 respectively. LEDs D1 to D8 are connected to the port pins P0.0 to P0.7 respectively. Resistors R1 to R8 are current limiting resistors. In simple words P1 of the microcontroller is the input port and P0 is the output port. Control signals for the ADC (INTR, WR, RD and CS) are available at port pins P3.4 to P3.7 respectively. Resistor R9 and capacitor C1 are associated with the internal clock circuitry of the ADC. Preset resistor R10 forms a voltage divider which can be used to apply a particular input analogue voltage to the ADC. Push button S1, resistor R11 and capacitor

C4 forms a debouncing reset mechanism. Crystal X1 and capacitors C2,C3 are associated with the clock circuitry of the microcontroller.

Program.
ORG 00H MOV P1,#11111111B // initiates P1 as the input port MAIN: CLR P3.7 // makes CS=0 SETB P3.6 // makes RD high CLR P3.5 // makes WR low SETB P3.5 // low to high pulse to WR for starting conversion WAIT: JB P3.4,WAIT // polls until INTR=0 CLR P3.7 // ensures CS=0 CLR P3.6 // high to low pulse to RD for reading the data from ADC MOV A,P1 // moves the digital data to accumulator CPL A // complements the digital data (*see the notes) MOV P0,A // outputs the data to P0 for the LEDs SJMP MAIN // jumps back to the MAIN program END

Notes.

The entire circuit can be powered from 5V DC. ADC 0804 has active low outputs and the instruction CPL A complements it t0 have a straight forward display. For example, if input is 5V then the output will be 11111111 and if CPL A was not used it would have been 00000000 which is rather awkward to see.

Thermometer using 8051.


This article is about a simple 0-100C digital thermometer with 1C resolution using 8051. The circuit is based on LM35 analog temperature sensor, ADC0804 and AT89S51 microcontroller. LM35 is an analogue temperature sensor IC which can measure a temperature range of -55 to 150C. Its output voltage varies 10mV per C change in temperature. For example, if the temperature is 32C, the output voltage will be 32 x 10mV = 320mV. ADC 0804 is used to convert the analogue output voltage of the LM35 to a proportional 8 bit digital value suitable for the microcontroller. The microcontroller accepts the output of ADC, performs necessary manipulations on it and displays it numerically on a 2 digit seven segment LED display. Out put of the LM35 is connected to the +Vin (pin 6) of the ADC0804. Resistor R13 and preset R14 is used to provide an external reference voltage of 1.28V to the Vref/2 pin ( pin 9) of the ADC0804 and with this reference voltage, the step size of the ADC will be 10mV and span will be 0-1 V. This means that for a 10mV input the digital out of ADC will be 1 (1

in decimal also), for 20mV it will be 10 (2 in decimal), for 30mV it will be 11 (3 in decimal) and so on. The microcontroller accepts this data and puts it on the seven segment display.

Circuit diagram.

Digital thermometer using 8051

Digital out of the ADC (D0 to D7) are connected to P1 (P1.0 to P1.7) of the microcontroller. This is the line through which the microcontroller accepts data from the ADC. The control pins CS, RD, WR and INTR are connected to P3.7, P3.6, P3.5 and P3.4 of the microcontroller. This is the data path through which the microcontroller sends chip select (CS), read (RD) write (WR) signals to the ADC and receives INTR signal from the ADC. Data lines (a to h) of the multiplexed seven segment display are interfaced to P0 (P0.0 to P0.7) of the microcontroller. Activation signals for the segment driver transistors Q1 and Q2 are available from P3.2 and P3.1 pins of the microcontroller. Before attempting this circuit go through these articles Voltmeter using 8051 , Interfacing ADC to 8051 ,Interfacing seven segment display to 8051.

Program.
ORG 00H MOV P1,#11111111B MOV P0,#00000000B MOV P3,#00000000B MOV DPTR,#LABEL MAIN: MOV R4,#250D CLR P3.7 SETB P3.6 CLR P3.5 SETB P3.5 conversion WAIT: JB P3.4,WAIT CLR P3.7 CLR P3.6 from ADC MOV A,P1 accumulator A MOV B,#10D DIV AB MOV R6,A MOV R7,B DLOOP:SETB P3.2 MOV A,R6 ACALL DISPLAY MOV P0,A ACALL DELAY CLR A MOV A,R7 CLR P3.2 SETB P3.1 ACALL DISPLAY MOV P0,A ACALL DELAY CLR A CLR P3.1 DJNZ R4,DLOOP SJMP MAIN DELAY: MOV R3,#255D // // // // // // // // // initializes P1 as initializes P0 as initializes P3 as loads the address loads register R4 makes Cs=0 makes RD high makes WR low low to high pulse input port output port output port of "LABEL" to DPTR with 250D

to WR for starting

// polls until INTR=0 // ensures CS=0 // high to low pulse to RD for reading the data // moves the digital output of ADC to // // // // // // // // // // // // // load B with 10D divides the content of A with that in B moves the quotient to R6 moves the remainder to R7 sets P3.2 which activates LED segment 1 moves the quotient to A calls DISPLAY subroutine moves the content of A to P0 calls the DELAY subroutine clears A moves the remainder to A deactivates LED segment 1 activates LED segment 2

// // // //

deactivates LED segment 2 repeats the loop "DLOOP" until R4=0 jumps back to the main loop produces around 0.8mS delay

LABEL1: DJNZ R3,LABEL1 RET DISPLAY: MOVC A,@A+DPTR // converts A's content to corresponding digit drive pattern RET LABEL: DB 3FH // LUT (look up table) starts here DB 06H DB 5BH DB 4FH DB 66H DB 6DH DB 7DH DB 07H DB 7FH DB 6FH END

You might also like