Posts Tagged electronics

8051-Programmable Counter Array

Hello people…its been quite some time since i ve posted anything here. Well let me fix that…. this is a post on the programmable counter array functionality in 8051. To be clear, the 8051 core as such does not have this functionality but if you have an 8051 board which is not too old, you should have it. Referring the data sheet might be helpful. For the description below i ll be using AT89c51ed2. But you can extend the ideas to other boards too.

A few words on programmable counter array…its there to expand the timing capabilities of 8051. It requires comparatively low software overhead and is more accurate. It has an array of 5 counter/capture modules. They can be programmed in any of the following modes:

  • Rising or falling edge timer
  • Software timer
  • High speed output
  • PWM

If you ve read my previous post on controlling the servo motor using 8051..i wrote about one way PWM can be achieved. Here i ll demonstrate a much simpler approach. I ll be using the module 0 here to generate a PWM wave. We ll set the frequency as 100kHz and the duty cycle to 20%. The output will be available on Pin1.3/CEX0. I ll be using the Keil simulator to see the output.

The modules that i mentioned above require a timer of their own to function properly, this is achieved by what is called a PCA timer. Its a common base timer for all the modules. This timer is itself sourced by :

  • Freq (clk-peripheral) / 2
  • Freq(clk-peripheral) / 6
  • Timer 0 overflow
  • Input on ECI pin (Pin 1.2)

A better way to say this is….lets assume that we have programmed it to use Timer 0 overflow as a source. This means every time timer0 overflows the PCA timer increments by 1.

Okay then lets get started with it.

Since the output frequency is 100kHz, Freq(clk-peripheral)/ 6  seems to be a good enough choice .To program that we need the CPS1 and the CPS0 bits in the CMOD register.

MOV 0D9H, #00H

Next we ll fix the contents of CCAPM0 register. This is where we tell the microcontroller that we are going to use it in the PWM and enable the comparison.

MOV 0DAH, #42H

The next step is loading the CH & CL registers with appropriate values such that CH/CL overflows at the rate of 100kHz.

The oscillator frequency here is 33MHz. The F(clk-peripheral) is (Oscillator / 2) in this case..i.e 16.5MHz. Further more the clock to PCA is F(clk-peripheral)/6….i.e. 2.75MHz. As a result the number of increments the CH/CL register should go through to get a frequency of 100kHz is 27.5 ~ 28. So CH can be initialized with 0xFF and CL with 0xE4. Everytime the timer overflows an interrupt is raised. Thus in the interrupt service routine reload the CH/CL registers with the above values.

Once we have taken care of the frequency, the next step is setting the duty cycle. How this works is,  we store a value in the CCAPL0 SFR. This value is compared with the value in CL. As long as the count in CL is less than the value in CCAPLo, the output remains LOW, it then toggles to HIGH when the CL count exceeds the value in CCAPL0.

Finally start the timer and enable the PCA interrupt. After debugging and running the program, the plot the output.

Here is the final code for 100kHz and 20 % duty cycle. I ve loaded the CH/CL with values other than what I mentioned above since 28 increments was giving me a frequency of around 80kHz. After experimenting a bit the value of 235 in CL worked out perfectly.

, , , , , , , , ,

Leave a comment

def Python meets Arduino:

I write this article as an introduction to how the Python language can be used to talk to the Arduino board. In essence, what I do here is simple serial communication.

There is a program that I ve written for the Arduino board that does two simple things:

  • Reads the serial input.
  • Constrains the input to an integer between 0 and 179 and writes it to the servo motor.

The point at which python comes into play is when we, as users, provide the input. Although the Arduino software has its own Serial monitor which can be used to accomplish the same task, I am using python just as an alternative (this can be done in any other language too…python is just the easiest one). It might so happen that later on the Serial monitor and its functionality  might not be enough. You might be writing a larger application of which the serial communication is only a small part. In which case Python to the rescue…

So here is the scenario in short..

There is a servo motor attached to one of the pwm digital output pins of the Arduino and i have the python interpreter running on my computer. If you don’t have python installed on your comp you can install it from You will also need to install the pySerial module if don’t already have it. Open the python interpreter and type “import serial”. If you get an error install it from here

Next I upload the following code to the board:

Serial available() : returns the number of characters still left to be read

One more thing to remember, in serial communication information is received in terms of character bytes. That means even if we give the input as the number 123. The microcontroller receives it as character ‘1’, character ‘2’ and character ‘3’ respectively

The next statement takes care of this problem. The expression ” incoming_byte – ‘0’ “, subtracts the character ‘0’ from the character received  giving us an integer for e.g. ‘1’ – ‘0’ = 1 (integer). and adds it to 10 * sum (as calculated in the previous iteration). The final sum is constrained between 0 & 179 and assigned to the variable “angle”. Finally, the value of “angle” is written to the Servo.

Once the program is uploaded, I move on to python. I open the interpreter and type the following commands which are quite self explanatory:

So…thats it. One of the simple ways in which python can be used with Arduino.

, , , , , , , ,


Servo motor, Serial COM and 8051

In this project I control the angular position of a servo motor by using the serial communication ability of the 8051 microcontroller. To state it a bit more simply ..I control the angle by which the shaft of the servo motor rotates by pressing keys on my laptop. This project might be helpful to you, if you are planning to build a robotic arm someday and want to control it using your computer.

List of things I am using here:

  • 8051 microcontroller board.
  • A Servo motor.
  • Connecting wires.
  • Computer (..obviously :P)

The connections are really very simple. Choose any of the 32 input/output pins available on your microcontroller board and connect it to signal wire of the servo motor.

A servo motor has just three wire. One for Vcc, one for GND and a signal wire. They are generally colour coded. On the motor which I have, the colour codes are as follows:

  • Red – Vcc
  • Brown – GND
  • Yellow – Signal

In case you have any confusion regarding the colour codes for your motor,  it is always a good idea to just google the model of your motor and clear your doubt.

Once you are done with these connections, thats it  .. now all that needs to be done is to program the microcontroller.

In case you have used an Arduino board before, you might know that there already exists a library for controlling the Servo motor which makes the task trivial. But in the case of 8051 the it is a bit more complicated. Its easy but some math needs to be done.

The position of the servo motor depends on the width of the pulses that it receives on the signal wire.And these pulses need to have a time period of approximately 20ms. After doing some research on the web i found the “on” time of the pules required for various positions(.. applicable to my servo).

  • 0.388 ms – 0 degrees
  • 1.25ms – 90 degrees
  • 2.14ms -180 degrees

Next we need to create a loop that runs for a fixed amount of time and depending upon what is the width of the pulses required we can run it multiple number of times. The following code accomplishes it.

To give an example of how i use this code, say I want to set the angular position of the servo to 0 degrees. In that case i need to provide the pulses with the width of 0.388 ms.So i do it this way.

  • Set the output 1
  • Call set_timer(8) 8 * 50 = 400usec = 0.4ms ~0.388ms
  • Set the output 0
  • Call set_timer(400 – 8) // because 400* 50us = 20000us = 20ms.

And since the calling function is itself inside a loop that goes on forever unless you receive a character serially, we are able to provide the pulses every 20ms on the signal line that sets the servo at angular position zero.

The following function receives the angle that needs to be written on the servo and calls the set_timer() function shown above from within.

0,90 & 180 are the constants denoting different angles.

RI = 0 when no data is received  on the serial line and RI = 1 when data has been received successfully. So once data is received, it breaks out from the loop and exits to return to the main() function, as the write_pos() was called from main.

Now in the main function we do all the initializations, like setting the baud rate etc needed for serial communication.

To decide what value needs to be put in TH1 for a particular baud rate there is the following formula if the mode is 1:

TH1 = 255 – ((Crystal freq / 384)/baudrate).

Thats it then..when you load this program onto the microcontroller you ll be able to control the position of servo motor using the keys ‘1’, ‘2’ and ‘3’ on your keyboard.Check out the video here

Here is the final code:

//Servo motor position control using serial    communication

#include <reg52.h>
#include <stdio.h>

void write_pos(int position);
void set_timer(int x);

sbit servo_signal1 = P1^0;

int main (void){

unsigned char ch;

SCON = 0x50; //
TMOD = 0X22; //
TH1 = 244;   //
TR1 = 1;     //
TI = 1;      //
RI = 0;      //

ch = SBUF;
RI = 0;

printf(“%c “,ch);
while (1){

case ‘1’:
write_pos(0); // Set Servo position to 0 degrees
case ‘2’:
write_pos(90); // Set Servo position to 90 degrees
case ‘3’:
write_pos(180); // Set Servo position to 180 degrees
while(!RI); // wait till you get a valid input
RI = 0;

ch = SBUF;
printf(“%c “,ch);



void write_pos(int position){
int msec;
case 0:
msec = 8;
servo_signal1 = 1;
servo_signal1 = 0;
set_timer(400 -msec);}
RI = 0;
case 90:
msec = 25;
servo_signal1 = 1;
servo_signal1 = 0;
set_timer(400 -msec);}
RI = 0;
case 180:
msec = 40;
servo_signal1 = 1;
servo_signal1 = 0;
set_timer(400 -msec);
RI = 0;

void set_timer(int x){

int i;
// To run the loop given below once it takes approx 50us
// Since I have already used the timer 1 for serial communication, I use
// the timer 0 to give PWM signal to the servo motor.


, , , , ,

Leave a comment

Blink LED using 8051

8051… this is historically one of the most important microcontroller the world has seen so far. To say it in one sentence..Its a piece of beauty.

the microcontroller p89v51rd2

What I present in this article is the first assembly language program that I ran on the 8051 microcontroller. If you have seen my “Blinking LED using Arduino”, we do a very similar thing here. However the difference is, blinking an LED using an Arduino board is just too easy and for the most part of it we are unaware of what is happens in the various registers. Although there is an easier way to program an 8051 using the C language, I intend to program it using the assembly language here simply because it gives me a better sense of control over the registers and also it is a fairly easy program to be implemented in assembly. Also programming in assembly makes you think a bit harder which can be fun if you want it to be.

Okay enough of the intro .. lets start.

The following components are required in this project:

  • An 8051 board. I am using the 89V51RD2 starter kit. I got it from here: 8051 Starter Kit
  • LEDs
  • A programmer
  • Programs like Keil and flashmagic to write the program and upload it to the microcontroller.
  • Connecting wires.

First things first, the objective. Our aim here is to connect  LEDs to one of the i/o ports and program the microcontroller such that the LEDs glow for 1 sec and turn off for the next second and they keep doing until the end of time (.. or as long as we supply power to the microcontroller)

Now.. the crystal that is used in my 8051 board oscillates at 11.0592MHz frequency. In other words there are 11.0592 million crystal pulses every second.

Next we look at the two timers (T0 & T1). Irrespective of which mode they are used in, the timers in the 8051 only keep incrementing and they increment by 1 every “machine cycle”. A machine cycle in 8051 requires 12 crystal pulses always. So although different instructions in the “Instruction set” require different number of machine cycles, a timer increments after every machine cycle.

So doing some math here a timer increments

11059200/12 = 921600 times every second

In other words it increases by 1 in 1.085 micro seconds. Here we will be using Timer 0 and will be using it in the 16 bit mode( Timer 0 and Timer 1 each are actually a combination of two 8 bit registers. Timer 0 -TH0 & TL0, Timer 1 – TH1 & TL1). Okay so 16 bits… which means it can count from 0 to (2^16) – 1 i.e from 0 to  65535, after which it will overflow, reset itself to 0 and start all over again. So lets check how much time does it take to do that:

65536 / 921600 = 0.07 sec

Well this is an odd value and it might be a bit tedious to work with such a number. So lets go the other way round. Lets check how many times does it increment in 0.05sec

0.05 * 921600 = 46080 increments.

Since it takes 1/20 th of a second to have 46080 increments, what we need to do is carry out 46080 increments of the timer 20 times to have a delay of 1 sec.

All right then now that we have an algorithm to measure time, now all we have to do is implement it in the timers.

This is done using two Special Function Registers ( SFRs), TCON and TMOD registers. Each one of them has eight bits and every bit has a particular function. The details of the TMOD timer are given below:

TMOD register

Timer modes

Next is the TCON register. In this register only two bits are useful for us at the moment and both of them can be accessed by specially assigned names.

TF0 – This bit sets when the timer 0 overflows.

TR0 – This bit must be set by us to enable the timer.

In our earlier derivation, we said that we need the timer to increment 46080 times  to introduce a delay of 0.05s. So we need to determine the starting point for the timer which is (65535 – 46080) = 19455( which in hexadecimal is 4BFFh) i.e. this is the value with which the timer needs to be initialized.

Now that we have all the bits and pieces, here is the algorithm for blinking an led every two seconds(“on” for 1 sec and “off” for the next second)

  • Set a flag bit ..say the bit at 20h and set the timer in mode1.
  • Store 20 (14 hex) in R0. This is the initialization step.
  • Initialize timer0 to 4BFF hex
  • Start the timer ( Set TR0)
  • Check the TF0 bit. Every time it overflows decrement R0 by 1.
  • If R0 is not zero jump back to the initialization process. Else  if it is zero depending upon the flag jump to instructions that turn on the led or turn off the led. Change the flag bit to the other state(if flag = 0 initially set it to 1 and vice verse)
  • Jump back to the point of initialization.

Here is the final code that I came up with and it appears to be working all right:

I must confess though, that the time delay is not exactly 1 sec. There is an error of a few milliseconds but even then its a pretty good result. Now all that needs to be done is to convert this asm file to a hex file and upload it onto the microcontroller.

There you have it.. blinking LED using 8051. Here is the video Video

, , , , , ,

Leave a comment

Android controlled Arduino bot

This is a project which me and a friend of mine ( attempted long time back.

It is a robot which is controlled by a cell phone.

It makes use of the Arduino and the android platform. It’s a simple bot which has an arduino board, powered by the Atmega328 microcontroller, as the brain. However the microcontroller receives its command from an Android phone.

The inspiration for the project was to acquire the ability to affect the physical environment around us using a device to which everyone has access to ….Cell phone.

The following components were used in making it:

  • Arduino boards
  • RF Transmitter/Receiver
  • L293D motor driver
  • Motors
  • Laptop
  • Android phone

So….this is how it works..

The first stage of this project is to have a graphical user interface with which the people can interact. As the robot, at this stage, only needs to maneuver in two dimensions, the GUI is not as impressive as it would need to be for more advanced applications. Currently it is just a group of 5 buttons namely…front, back, right, left and stop.

The real action is however the one that cannot be seen. This was the tough part actually. Since none of us was well versed with java and Android programming, it took a fair amount of searching and learning in order to get the communication set up. Right now only the bare basics of communications have been included in the project.

When the application is run, it first checks to see if the Bluetooth on the phone is enabled. If not it asks permission to enable it.

Next the list of the devices with which the phone had previously paired with is presented. Since the app is in a very crude stage, the ability of searching for new devices has not been included.

Once the device is selected it establishes a link with it. Here the other device with which the phone communicates is my laptop. The laptop acts as a server and has a python script running which bridges the link between the Bluetooth and the USB serial port.

Once the link is established, the user can press one off the buttons to maneuver the bot. Behind every button click there is a listener which then takes a byte code representing that button and sends it to the server.

The server is a Python script running on the laptop. It is nothing but a tunnel which directs the input from the bluetooth to the serial port.

The server then forwards this number to the serial port.

The serial port has an arduino board attached to it which is further connected to the RF transmitter.

The transmitter was in turn attached to another arduino board. To work with RF communication we used the VirtualWire library which is very well documented and made the job really easy.

The transmitter then forwards this code to the receiver on the bot. On getting the data from the RF receiver the microcontroller calls upon the appropriate functions which set the inputs of the Motor driver ic L293D to HIGH & LOW values correspondingly to maneuver the robot.

As you might have noticed, we had to take the longer route here. Since we dont have a bluetooth module for direct contact with the arduino, we had to use the laptop as an intermediate (with the module the laptop wont be required at all).

You can check out the video of what we ended up with at: Here ‘s the video

, , ,

1 Comment

Intensity control of an LED using LDR and Arduino board

This is another simple project that you might want to try out using your Arduino board.

So what is exactly our goal here?

As the title says, we are going to control the intensity of an LED(Light Emitting Diode) using an LDR (Light Dependent Resistor).
But more importantly we are going to look at how we take in an analog input, make sense of that input, and output what appears to be analog signal ( …but it ‘s not !!..its actually PWM) using  an Arduino board.

Some Theory

Okay it might be possible that there were a few terms in the above paragraph that might be new to you. In case you have completely understood it, you may skip ahead but in case you haven’t this might clear it up a bit.


It stands for “Light Dependent Resistor”. It ‘s a type of resistor whose value changes with intensity or the amount of light falling on it. As the amount of light falling on it increases the its resistance decreases and the other way round. Understanding why it happens will require going into the depths of the material with which it is made and this isn’t really an article for that. However there are many articles which will solve the question but for now you only need to remember that it ‘s resistance value changes with the amount of light falling on it.

Analog to Digital Converter:

It takes in an analog value( in this case the voltage across the LDR) and converts it to a 10 bit digital. Since we have 10 bits, the output of the ADC ranges from 0-1023 (i.e. 1024 values 2^10).  Every voltage between 0 – 5V is mapped to a value between 0-1023. Why till 1023? The reason is the resolution of our ADC. Here we represent every value with a combination of 10 bits. As the number of bits used to represent a value increases, the output value gets much more accurate. The following figure gives an idea of what happens in an ADC using 2 bit resolution.

Value         Bit Representation

0 – 1.25             00

1.25-2.5            01

2.5-3.75            10

3.75-5                11


Pulse Width Modulation is a technique in which the width of the pulses can be varied according to the message signal. In this case the led is connected to a PWM output pin of the arduino board and using the analogWrite() function we make the width of the pulses on the pin vary. Varying the width of the pulses can be seen as varying the average voltage on the pin. So although we are only changing the amount of time for which the led is on or off, due to the persistence of vision it is as if we are varying the voltage across the led which causes it to fade and brighten up.

How do we get there?

Okay so lets get started. We are going to require the following items:

  • Resistors
  • Light Emitting Diode
  • Light Dependent Resistor
  • Arduino board
  • Connecting wires
  • Breadboard

The connections are as follows:

  • Connect one end of a resistor ( I have a 1k one…you can use any other value if you prefer.. just make sure it is not too high or too low) to the 5V pin of the arduino board. Connect the other end of the resistor to one end of the LDR. The other end of the LDR should be connected to the ground (GND).
  • Take a wire from the junction of the resistor & the LDR connected earlier and connect it to the Analog Input 5 on the arduino board.
  • Next take a wire from any of the PWM pins on your board and connect it to the Anode of the LED. Connect the cathode of the LED to one end of a resistor with the other end connected to the GND.

Those are all the connections we have to make.

Next we need to write a program to make it work.

The program is quite self explanatory

Some things to note are

  • When I uploaded the program to the board I found that the value in the variable “value” varied over a range of 990 – 1010. This is a very small range in comparison to 0 – 1023. So I constrained it between 990 – 1010 before mapping it to 0 – 255. If I had not done this  the value written to the ledPin would have varied over a much smaller range and this would not have been noticeable in the varying brightness of the led.
  • I have also put in the Serial.println() function. It ‘s a good idea to use some of these when you want to check the values being read or written. Helps in debugging.

    The circuit in sufficient light

Now when I cover the circuit the led begins to glow. Its brightness increases as I continue enclosing the LDR more.Until the point..

In the dark

So…there you go controlling the intensity of the LED using an LDR.

You can check out the video here: Here ‘s the Video

Credits google for the first two pics.

, , , , ,

Leave a comment